Program Structure#

  • A quick overview of control sturctures in Python

If- structure#

  • Format

if test:
    DoThis()
elseif test:
    DoThat()
else:
    pass

Types of Test#

# comparisons
a > b
a == b
a != b

# membership
a in b
a not in b

# len check
len(x) > 5

# boolean value
fileopen
not fileopen

# Validation
x.isalpha()
x.isdigit()

# Calculation
(a*b) > 100.0
## use braces to force the calculation

# Combining Tests
and, or, not

For Statement#

names = ['Julia','Jane','Tom','Dickson','Harry']
tnum = range(1,5)
for p in names:
    for tn in tnum:
        print('%s has passed %f test' % (p, tn))
    
Julia has passed 1.000000 test
Julia has passed 2.000000 test
Julia has passed 3.000000 test
Julia has passed 4.000000 test
Jane has passed 1.000000 test
Jane has passed 2.000000 test
Jane has passed 3.000000 test
Jane has passed 4.000000 test
Tom has passed 1.000000 test
Tom has passed 2.000000 test
Tom has passed 3.000000 test
Tom has passed 4.000000 test
Dickson has passed 1.000000 test
Dickson has passed 2.000000 test
Dickson has passed 3.000000 test
Dickson has passed 4.000000 test
Harry has passed 1.000000 test
Harry has passed 2.000000 test
Harry has passed 3.000000 test
Harry has passed 4.000000 test
import itertools

for (p, tn) in zip(names, tnum):
    print('%s has passed %.0f test(s)' % (p, tn))
Julia has passed 1 test(s)
Jane has passed 2 test(s)
Tom has passed 3 test(s)
Dickson has passed 4 test(s)

Tip

Using tqdmin for-loop can give us a progress bar in the iteration process.

from tqdm import tqdm 
for i in tqdm(range(10000)): 
sleep(0.01) 

While Statement#

n=5
while n > 0:
    print(n)
    n-=1
5
4
3
2
1
n=5

while not n < 0:
    print(n)
    n-=1
5
4
3
2
1
0

Break Statement#

def doCommand(s):
    print('Hay %s! You are in the system now!!' % s)

while True:
    command=input('Enter your name:')
    if command=='exit':
        break
    else:
        doCommand(command)
print('bye')
Enter Command:exit
bye
while True:
    command=input('Enter your name:')
    if len(command)==0:
        continue
    elif command=='exit':
        print('Goodbye!')
        break
    else:
        doCommand(command)
print('bye')
Enter your name:exit
Goodbye!
bye

List Comprehensions#

  • A list comprehension is a way of dynamically creating a list elements in an elegant shorthand.

[expr for element in iterable if condition]
squares = [i**2 for i in range(1,10)]
print(squares)

squares2 = [i**2 for i in range(1,10) if not i % 2]
print(squares2)

text = 'This is a sample sentence long string.'

print([i.upper() for i in text if i.find('a')])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[4, 16, 36, 64]
['T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', ' ', 'S', 'M', 'P', 'L', 'E', ' ', 'S', 'E', 'N', 'T', 'E', 'N', 'C', 'E', ' ', 'L', 'O', 'N', 'G', ' ', 'S', 'T', 'R', 'I', 'N', 'G', '.']

Functions#

def FUNCTION_NAME:
    PROCDURES
    
    reutrn RETURN_VALUES