Boolean expressions are expressions that are either true or false. There are 6 boolean expression operators, equals (==), not equals (!=), greater than (>), less than (<), greater than or equal (>=), less than or equal (<=).
For example: 5 == 5 - True, 5 != 5 - False
Semantical operators connecting boolean expressions. There are 3 of them, and, or, and not. Their meaning is similar to english.
For example: x > 10 and x < 20 is true only if x has a value between 10 and 20 (greater than 10 AND smaller than 20), excluding those.
Conditional statements allow us to execute statements depending if a condition is true or false. The simplest form is using as if-statement.
For example: if x > 10: print x
Alternative execution defines what will be executed if your conditional execution does not.
For example:
if x % 2 == 0:
print x, "is even"
else:
print x, "is odd"
You can read while statements like in english. In essense, you tell python that while a boolean expression is true, do something.
For example:
x = 10
while x > 0:
print x
x = x - 1
print "Blastoff!"
For loops are different from the while statements, as they take care of our need to loop through multiple elements in a variable. Think of strings, as defined in the last section. A string is a collection of characters, therefore a variable that holds a string actually holds many characters. The for loop works by asking Python to give us each individual elements in a variable.
For example:
x = "abcdefghijklmnopqrstuvwxyz"
for letter in x:
print letter
fruit = "banana"
count = 0
for char in fruit:
if char == 'a':
count = count + 1
print count