Welcome to our second note in our Python learning process. In this note we will talk about variables, statements, expressions, operators, comments, and functions. These are the very basic building blocks of you programs whatever its final size
The assignment statement creates new variables and gives them values
>» message = “What’s up, Doc?”
>» n = 17
>» pi = 3.14159
Python is a dynamically typed language. Which means that variables’ types don’t have to be defined before the variables use. The python interpreter figure out what type a variable is when you first assign it a value.
and
continue
else
for
import
not
raise
assert
def
except
from
in
or
return
break
def
exec
global
is
pass
try
class
elif
finally
if
lambda
while
A statement is an instruction that the Python interpreter can execute. A Python script is a sequence of statements. The results appear one at a time as the statements execute.
For example, the script
>» print 1
>» 1
>» x = 2
>» print x
>» 2
An expression is a combination of values, variables, and operators. If you type an expression on the command line, the interpreter evaluates it and displays the result:
>» 1 + 1
2
A boolean expression is an expression that is either true or false. Boolean expressions are expressions that uses logical operators. There are three logical operators: and, or, not. The operands of logical operands should be boolean expressions. In Python there is no boolean data type instead 0, ‘’, [], (), {}, and None are false in a boolean context; everything else is true. The absence of boolean data types results in the following behavior of logical operators:
>» ‘a’ and ‘b’
‘b’
>» ’ ’ and ‘b’
’ '
>» ‘a’ and ‘b’ and ‘c’
‘c’
>» ‘a’ or ‘b’
‘a’
>» ’ ’ or ‘b’
‘b’
>» ’ ’ or [] or {}
{}
>» not (1 and 0)
True
>» not (1 or 0)
False
Operators are special symbols that represent computations like addition and multiplication. The values the operator uses are called operands.
The symbols +, -, and /, and the use of parenthesis for grouping, mean in
Python what they mean in mathematics. The asterisk (*) is the symbol for multiplication, and ** is the symbol for exponentiation.
When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. The following is operators from highest precedence to lower precedence:
Operators with the same precedence are evaluated from left to right.
Comments start with #.
Here are some of the built-in functions available in Python:
>» int(“32”)
32
>» int(“Hello”)
ValueError: invalid literal for int(): Hello
>» float(32)
32.0
>» float(“3.14159”)
3.14159
>» str(32)
‘32’
>» str(3.14149)
‘3.14149’
>» decibel = math.log10 (17.0)
>» angle = 1.5
>» height = math.sin(angle)
>» degrees = 45
>» angle = degrees * 2 * math.pi / 360.0
>» math.sin(angle)
0.707106781187
>» math.sqrt(2) / 2.0
0.707106781187
There are many other built-in functions, we mentioned these two types just as examples.
A function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition.
The syntax for a function definition is:
def FUNCTION_NAME( LIST OF PARAMETERS ):
STATEMENTS
# just to show the function scope and structure
def sample_func():
Print “we are in the function body scope”
Print “we still in the function scope”
Print “we are out of the function scope”
# another function with parameters
def sum(x, y):
print x + y
>» sample_func()
>» sum(1,2)
3
def sum(x,y):
return x +y
def print_sum(x,y):
print sum(x,y)
print_sum(2,3)
5
def sum(x,y):
Z = x +y
print Z # will generate a runtime error
def divide(a,b):
q = a/b
r = a - q*b
return q,r
>» x,y = divide(42,5) # x = 8, y = 2
>» x
8
>» y
2
In this note we talked about variables, statements, expressions, operators, comments, and functions. These are the very basic building blocks of you programs whatever its final size. We will continue our learning in the upcoming notes.