Skip to main content


Top 14 Python - Fun with Control and repeat, boolean, Python If...Else, elif, operators, ternary


Hello Everyone,
In this section we will learn how to control the flow of execution of statements in a program by using "if" , "elif" & "else" statements.
Learn how to repeat code blocks by using iteration - (meaning - the repetition of a process) tools in python such as "for" and "while" loops.

Lets get on with the code:-

Boolean Algebra (True/False) [T/F]
Understand 'and' truth tables logic
***********
T + T = T
T + F = F
F + F = F
F + T = F
***********

1) True / False (boolean)
A simple truth function (boolean logic + function) using 'and'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#_1 a simple truth function
>>> def the_truth():
 print(True and True)
 print(True and False)
 print(False and False)
 print(False and True)
 print()

 
>>> the_truth()
True
False
False
False

2) Replacing it with variables (t = True, f = False)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> def the_truth():
 print(t and t)
 print(t and f)
 print(f and f)
 print(f and t)
 print()

 
>>> the_truth()
True
False
False
False

3) A simple truth function (boolean logic + function + tuple)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#_creating boolean logic + function + tuple
>>> def the_truth():
 print()
 print("True and True","=",True and True)
 print("True and False","=",True and False)
 print("False and False","=",False and False)
 print("False and True","=",False and True)
 print()

 
>>> the_truth()

True and True = True
True and False = False
False and True = False
False and False = False

>>> 

4) If you want in short form like say T / F

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> x = True
>>> x = str("T")
>>> y = False
>>> y = str("F")
>>> 
>>> def the_truth():
 print()
 print(x,"+",x,"=",x and x)
 print(x,"+",y,"=",x and y)
 print(y,"+",x,"=",y and x)
 print(y,"+",y,"=",y and y)
 print()

 
>>> the_truth()

T + T = T
T + F = F
F + T = F
F + F = F

>>> 

Understand 'or' truth tables logic
***********
T + T = T
T + F = T
F + F = F
F + T = T
***********

5) A simple truth function (boolean logic + function) using 'or'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> def the_truth():
 print(True or True)
 print(True or False)
 print(False or False)
 print(False or True)
 print()

 
>>> the_truth()
True
True
False
True

Understand 'xor' truth tables logic
***********
T + T = F
T + F = T
F + F = F
F + T = T
***********

6) A simple truth function (boolean logic + function) using 'or'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> def the_truth():
 print(True ^ True)
 print(True ^ False)
 print(False ^ False)
 print(False ^ True)
 print()

 
>>> the_truth()
False
True
False
True

7) Compound statement or combining them

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> print(True or False or False)
True
>>> print(True and False or True)
True
>>> print(True or False and True)
True
>>> print(not False &(True and True) or False)
True
>>> print(True ^ False and False or False)
False
>>> 

8) Simple if / else statement

1
2
3
4
5
6
7
8
>>> x, y, z = 5, 10, 15
>>> if x < y and z > y:
 print(x)
else:
 print(y)

5
>>> 

9) The above statement could be rewritten as follows

1
2
3
4
5
6
7
8
>>> x, y, z = 5, 10, 15
>>> if x < z > y:
 print(x)
else:
 print(y)

5
>>> 

10) Nesting "if" inside another "if"

1
2
3
4
5
6
7
8
>>> if x < y:
 if z > y:
  print(x)
else:
 print(y)

5
>>> 

11) Removing "else" and nesting another "if"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> if x < y:
 if z > y:
  print(x)
 print(y, '\n')
 if not x > y:
  print ('inverse!','\n')

  
5
10 

inverse! 

>>> 

12) Simple elif statement (if can change multiple conditionals together)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
>>> if var == 1:
 print(var + 10)
elif var == 3:
 print(var + 20)
elif var == 5:
 print(var + 30)
elif var == 7:
 print(var + 50)
elif var == 9:
 print(var + 70)
elif var == 10:
 print(var + 100)
else:
 print('I think it\'s not in here')
 print()

110
>>> 

13) Using other comparison operators such as <=, >=, !=, etc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
>>> #using (<=)
>>> if var <= 1:
 print(var + 10)
elif var <= 3:
 print(var + 20)
elif var <= 5:
 print(var + 30)
elif var <= 7:
 print(var + 50)
elif var <= 9:
 print(var + 70)
elif var <= 10:
 print(var + 100)
else:
 print('I think it\'s not in here')
 print()

 
110
>>>

>>> #using (>=)
>>> if var >= 1:
 print(var + 10)
elif var >= 3:
 print(var + 20)
elif var >= 5:
 print(var + 30)
elif var >= 7:
 print(var + 50)
elif var >= 9:
 print(var + 70)
elif var >= 10:
 print(var + 100)
else:
 print('I think it\'s not in here')
 print()

 
20
>>>

>>> #using (!=)
>>> if var != 1:
 print(var + 10)
elif var != 3:
 print(var + 20)
elif var != 5:
 print(var + 30)
elif var != 7:
 print(var + 50)
elif var != 9:
 print(var + 70)
elif var != 10:
 print(var + 100)
else:
 print('I think it\'s not in here')
 print()

 
20
>>>

14) Ternary statement (allows for assignment of variable based on expression

1
2
3
4
5
6
>>> test_variable = 1
>>> result = 10 if test_variable else ''
>>> print(result, '\n')
10 

>>> 

Hope you like the tutorial.

Python is simple and fun to play with and yet very powerful. 🔥


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)

Top 11 Essential Python Tips and Tricks





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram