In Python, the layout of the source code is very important : the different blocks are NOT delimited by braces or parenthesis, but by the indentation.
The recommended Python code layout, and the recommended coding style in general, are defined in PEP 8. A Python specialized editor should check and help to apply them :
if, elif, else¶...will execute a block of code if the expression between if|elif and : is true.
a = 2
b = 3
if a > b:
print('a>b')
elif a == b:
print('a==b')
else:
print('a<b')
a<b
An expression is true if its value is:
TrueAn expression is false if its value is:
False or NoneTry those ones:
condition = False
if condition:
print("true")
else:
print("false")
false
condition = [ None ]
if condition:
print("true")
else:
print("false")
true
Values comparison operators:
==, !=<, <=, >, >=a=1 ; b=3 ; print(a<b)
a="hello" ; b = "world" ; print(a>b) # alphabetical order
True False
print(["a", 2, "b"] == [1, 2, 3])
False
print([a, 2, b] == [1, 2, 3])
False
a=[1, 2] ; b=[1, 2] ; print(a == b)
True
Objects comparison operators:
is : this is the same object # DANGEROUS ! (unlesss with None)in : the element is member of the collectiona=[1, 2] ; b=[1, 2] ; print(a is b)
False
condition = None
if condition is None :
print("no condition")
else:
print("condition:", condition)
no condition
2 in [1, 2, 3]
True
Logical operators: not, and, or.
a=12
print(a>10 and a<20)
print(10<a<20)
True True
while¶...repeat a block of code, as long as the expression between while and : is true..
a = 0
maxval = 4
while a < maxval:
print(a)
a += 1
0 1 2 3
The keywork continue jumps to next iteration.
a = 0
maxval = 4
while a < maxval:
if a == 2:
a += 1
continue
print(a)
a += 1
0 1 3
The keywork break jumps outside of the while.
a = 0
maxval = 4
while a < maxval:
if a == 2:
break
print(a)
a += 1
0 1
for¶The statement for can repeat a block of code for all the elements of any structure which is iterable: list, tuple, dict, str, ...
for i in ['a', 'b', 'c']:
print(i)
a b c
The strings (type str) are iterable sequences of characters:
for i in "abc":
print(i)
a b c
When iterating over a dictionary (type dict), the iterating value receive the keys of the dictionary:
dictionary = { 'a': 5, 'b': 6, 'c': 7 }
for key in dictionary:
print(key, dictionary[key])
a 5 b 6 c 7
The keywords continue and break are also usable in a for loop.
When iterating on a sequence of integers, we often use the range()function:
for i in range(5):
print(i)
0 1 2 3 4
If you want to access the elements of a sequence through their ranks, you can combine len() and range():
values = 'a', 'b', 'c'
for rank in range(len(values)):
print(rank, values[rank])
0 a 1 b 2 c
It may be easier to use the enumerate() function, which returns some pairs (tuple of two elements) with the rank and the value:
values = 'a', 'b', 'c'
for rank, value in enumerate(values):
print(rank, value)
0 a 1 b 2 c
What do you think will happen when using enumerate() with a dict ?
dictionary = { 'a': 5, 'b': 6, 'c': 7 }
for value1, value2 in enumerate(dictionary):
print(value1, value2)
0 a 1 b 2 c
If you want to iterate over the pairs items() method from the dictionary object:
dictionary = { 'a': 5, 'b': 6, 'c': 7 }
for value1, value2 in dictionary.items():
print(value1, value2)
a 5 b 6 c 7