Python Statements List Like Professional!
Complete Python Bootcamp From Zero to Hero in Python (Statements, Loop)
How to Write a Python Statements List?
Python VS Other Languages
Other Languages if (a>b){ a = 2; b = 4; }
Python if a>b: a = 2 b = 4
Python Identification
Other Languages if (x) if(y) code-statement; else another-code-statement;
Python if x: if y: code-statement else: another-code-statement
How to Use Python Statements List?
Python “if, else”
Multiple Branches
Python Statements List Loop
for loop number list
for loop if else statement
for loop sums
for loop string letter
for loop tuple
for loop list tuples
for loop key, values and items
Python “while loop”
break: Breaks out of the current closest enclosing loop.
continue: Goes to the top of the closest enclosing loop.
pass: Does nothing at all.
The general format of a while loop is: while test: code statements else: final code statements
Python “break, continue, pass”
The three statements are defined by: while test: code statement if test: break if test: continue else:
It is possible to create an infinitely running loop with while statements. For example: # WARNING: DO NOT RUN THIS CODE!!!! while True: print("I'm stuck in an infinite loop!")
Using “pass” in loops to avoid syntax error
range
enumerate with tuple unpaking
list enumerate, zip, list zip, for zip format
Python “in” operator
Python “min and max”
Python random, import, shuffle, randint
Get specific letter in string
Square numbers in range and turn into list
Check even numbers in a range
Convert Celsius to Fahrenheit
Contents
1. Introduction
1.2 Indentation
2. if, elif, else Statements
2.1 Example if, else
3. For Loops
3.1 Loop number list
3.3 Loop sums
3.5 Loop tuple
3.6 Loop List Tuples
3.7 Loop key, values and items
4. While Loops
4.1 While loop
4.2 Using break, continue, pass
4.3 Using Pass in loops to avoid syntax error
5. Useful Operators
5.1 Range
5.2 Enumerate
5.3 Zip
5.4 in operator
5.5 min and max
5.6 random
6. List Comprehensions
6.1 Grab every letter in string
6.2 Square numbers in range and turn into list
6.3 Check even numbers in a range
6.4 Convert Celsius to Fahrenheit
1.1 Python VS Other Languages
Other Languages
if (a>b){
a = 2;
b = 4;
}
—————————————————————–
Python
if a>b:
a = 2
b = 4
—————————————————————–
1.2 Indentation
Other Languages
if (x)
if(y)
code-statement;
else
another-code-statement;
—————————————————————–
Python:
if x:
if y:
code-statement
else:
another-code-statement
—————————————————————–
2.1 Example if, else
if True:
print(‘It was true!’)
# output: It was true!
—————————————————————–
x = False
if x:
print(‘x was True!’)
else:
print(‘I will be printed in any case where x is not true’)
# output: I will be printed in any case where x is not true
—————————————————————–
2.2 Multiple Branches
if 3 > 2:
print(‘TRUE!’)
# output: TRUE!
—————————————————————–
hungry = True
if hungry:
print(‘HUNGRY!’)
else:
print(‘NOT HUNGRY!’)
# output: HUNGRY!
—————————————————————–
loc = ‘Bank’
if loc == ‘Auto Shop’:
print(‘Welcome to the Auto Shop!’)
elif loc == ‘Bank’:
print(‘Welcome to the bank!’)
elif loc == ‘Store’:
print(‘Welcome to the store!’)
else:
print(‘Where are you?’)
# output: Welcome to the bank!
—————————————————————–
person = ‘Al Ardosa’
if person == ‘Al Ardosa’:
print(‘Welcome Al Ardosa!’)
else:
print(“Welcome, what’s your name?”)
# output: Welcome Al Ardosa!
—————————————————————–
person = ‘Al Ardosa’
if person == ‘Sammy’:
print(‘Welcome Sammy!’)
elif person == ‘Al Ardosa’:
print(‘Welcome Al Ardosa!’)
else:
print(“Welcome, what’s your name?”)
# output: Welcome Al Ardosa!
—————————————————————–
3.1 For loop number list
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for mynum in mylist:
print(mynum)
# output:
1
2
3
4
5
6
7
8
9
10
—————————————————————–
3.2 For loop if else statement
# print only the even numbers from that list!
# % is modulo to get the remainder only
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for mynum in mylist:
if mynum % 2 == 0:
print(‘Even Number’, mynum)
else:
print(f’Odd number: {mynum}’)
# output:
Odd number: 1
Even Number 2
Odd number: 3
Even Number 4
Odd number: 5
Even Number 6
Odd number: 7
Even Number 8
Odd number: 9
Even Number 10
—————————————————————–
3.3 For loop sums
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Start sum at zero
mysum = 0
for mynum in mylist:
mysum = mysum + mynum
print(mysum)
# output: 55
—————————————————————–
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Start sum at zero
mysum = 0
for mynum in mylist:
# you can also use +=
mysum += mynum
print(mysum)
# output: 55
—————————————————————–
3.4 For loop string letter
for myletter in ‘This is a string.’:
print(myletter)
# output:
T
h
i
s
i
s
a
s
t
r
i
n
g
.
—————————————————————–
for _ in ‘Hey!’:
print(‘Cool!’)
# output:
Cool!
Cool!
Cool!
Cool!
—————————————————————–
3.5 For loop tuple
tups = (1, 2, 3, 4, 5)
for t in tups:
print(t)
# output:
1
2
3
4
5
—————————————————————–
3.6 For loop list tuples
mylist = [(2, 4), (6, 8), (10, 12)]
for tups in mylist:
print(tups)
# output:
(2, 4)
(6, 8)
(10, 12)
—————————————————————–
mylist = [(2, 4), (6, 8), (10, 12)]
# Now with unpacking!
for (t1, t2) in mylist:
print(t1)
# output:
2
6
10
—————————————————————–
3.7 For loop key, values and items
d = {‘k1’: 1, ‘k2’: 2, ‘k3’: 3}
for item in d:
print(item)
print(‘—————–‘)
print(d.items())
print(‘—————–‘)
# Dictionary unpacking
for key, value in d.items():
print(key)
print(value)
print(‘—————–‘)
print(list(d.keys()))
print(‘—————–‘)
print(sorted(d.values()))
# output:
k1
k2
k3
—————–
dict_items([(‘k1’, 1), (‘k2’, 2), (‘k3’, 3)])
—————–
k1
1
k2
2
k3
3
—————–
[‘k1’, ‘k2’, ‘k3’]
—————–
[1, 2, 3]
—————————————————————–
4.1 While loop
break: Breaks out of the current closest enclosing loop.
continue: Goes to the top of the closest enclosing loop.
pass: Does nothing at all.
—————————————————————–
The general format of a while loop is:
while test:
code statements
else:
final code statements
—————————————————————–
x = 0
while x < 5:
print(‘x is currently: ‘, x)
print(‘ x is still less than 5, adding 1 to x’)
x += 1
else:
print(‘All Done!’)
#output:
x is currently: 0
x is still less than 5, adding 1 to x
x is currently: 1
x is still less than 5, adding 1 to x
x is currently: 2
x is still less than 5, adding 1 to x
x is currently: 3
x is still less than 5, adding 1 to x
x is currently: 4
x is still less than 5, adding 1 to x
All Done!
—————————————————————–
4.2 using break, continue, pass
The three statements are defined by:
while test:
code statement
if test:
break
if test:
continue
else:
—————————————————————–
x = 0
while x < 5:
print(f’x is currently: {x}’)
print(‘ x is still less than 5, adding 1 to x’)
x += 1
if x == 3:
print(‘x==3’)
else:
print(‘continuing…’)
continue
# output:
x is currently: 0
x is still less than 5, adding 1 to x
continuing…
x is currently: 1
x is still less than 5, adding 1 to x
continuing…
x is currently: 2
x is still less than 5, adding 1 to x
x==3
x is currently: 3
x is still less than 5, adding 1 to x
continuing…
x is currently: 4
x is still less than 5, adding 1 to x
continuing…
—————————————————————–
# put in a break once x ==3 and see if the result makes sense:
x = 0
while x < 10:
print(f’x is currently: {x}’)
print(‘ x is still less than 10, adding 1 to x’)
x += 1
if x == 3:
print(‘Breaking because x==3’)
break
else:
print(‘continuing…’)
continue
# output:
x is currently: 0
x is still less than 10, adding 1 to x
continuing…
x is currently: 1
x is still less than 10, adding 1 to x
continuing…
x is currently: 2
x is still less than 10, adding 1 to x
Breaking because x==3
—————————————————————–
It is possible to create an infinitely running loop with while statements. For example:
# DO NOT RUN THIS CODE!!!!
while True:
print(“I’m stuck in an infinite loop!”)
—————————————————————–
4.3 Using Pass in loops to avoid syntax error
x = [1, 2, 3]
for item in x:
# my comment
pass
print(‘end of script’)
—————————————————————–
5.1 Range
print(range(0,11))
#output: range(0,11)
# 11 is not included, up to but not including 11
print(list(range(0,11)))
#output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
—————————————————————–
# Third parameter is step size!
# step size just means how big of a jump/leap/step you
# take from the starting number to get to the next number.
print(list(range(0,11,2)))
# output: [0, 2, 4, 6, 8, 10]
—————————————————————–
print(list(range(0,101,10)))
#output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
—————————————————————–
5.2 Enumerate
index_count = 0
for letter in ‘abcde’:
print(“At index {} the letter is {}”.format(index_count,letter))
index_count += 1
#output:
At index 0 the letter is a
At index 1 the letter is b
At index 2 the letter is c
At index 3 the letter is d
At index 4 the letter is e
—————————————————————–
# tuple unpacking!
for i,letter in enumerate(‘abcde’):
print(“At index {} the letter is {}”.format(i,letter))
#output:
At index 0 the letter is a
At index 1 the letter is b
At index 2 the letter is c
At index 3 the letter is d
At index 4 the letter is e
—————————————————————–
5.3 zip
print(list(enumerate(‘abcde’)))
#output:
[(0, ‘a’), (1, ‘b’), (2, ‘c’), (3, ‘d’), (4, ‘e’)]
—————————————————————–
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
print(zip(mylist1, mylist2))
#output:
<zip object at 0x0000000002821D08>
—————————————————————–
print(list(zip(mylist1,mylist2)))
#output:
[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’), (5, ‘e’)]
—————————————————————–
for item1, item2 in zip(mylist1, mylist2):
print(‘For this tuple, first item was {} and second item was {}’.format(item1, item2))
#output:
For this tuple, first item was 1 and second item was a
For this tuple, first item was 2 and second item was b
For this tuple, first item was 3 and second item was c
For this tuple, first item was 4 and second item was d
For this tuple, first item was 5 and second item was e
—————————————————————–
5.4 in operator
print(‘x’ in [‘x’, ‘y’, ‘z’])
#output: True
—————————————————————–
print(‘w’ in [‘x’, ‘y’, ‘z’])
#output: False
—————————————————————–
5.5 min and max
mylist = [10, 20, 30, 40, 100]
print(min(mylist))
# output: 10
—————————————————————–
print(max((mylist)))
# output: 100
—————————————————————–
5.6 random
mylist = [10, 20, 30, 40, 100]
from random import shuffle
shuffle(mylist)
print(mylist)
#output: [40, 20, 10, 30, 100]
—————————————————————–
from random import randint
print(randint(0, 100))
#output: 88
—————————————————————–
6.1 Grab every letter in string
lc = [x for x in ‘word’]
print(lc)
#output: [‘w’, ‘o’, ‘r’, ‘d’]
—————————————————————–
6.2 Square numbers in range and turn into list
lc = [x ** 2 for x in range(0, 11)]
print(lc)
# output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
—————————————————————–
6.3 Check even numbers in a range
lc = [x for x in range(11) if x % 2 == 0]
print(lc)
# output: [0, 2, 4, 6, 8, 10]
or
lc = [x if x % 2 == 0 else ‘ODD’ for x in range(0, 11)]
print(lc)
# output: [0, ‘ODD’, 2, ‘ODD’, 4, ‘ODD’, 6, ‘ODD’, 8, ‘ODD’, 10]
—————————————————————–
6.4 Convert Celsius to Fahrenheit
celsius = [0, 10, 20.1, 34.5]
fahrenheit = [((9 / 5) * temp + 32) for temp in celsius]
print(fahrenheit)
# output: [32.0, 50.0, 68.18, 94.1]
—————————————————————–
lc = [x ** 2 for x in [x ** 2 for x in range(11)]]
print(lc)
# output: [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]