AnalyticsDojo

Introduction to Python - Conditional Statements and Loops

rpi.analyticsdojo.com

Overview

  • (Need to know) Indentation in Python
  • What are conditional statements? Why do we need them?
  • If statements in Python
  • Why, Why not Loops?
  • Loops in Python
  • Exercises

Tabs vs Spaces

(Worth Watching)

Indentation in Python

  • Indentation has a very specific role in Python and is important!
  • It is used as alternative to parenthesis, and getting it wrong will cause errors.
  • Python 3 allows 4 spaces or tabs, but not both. [But in working in Jupyter seems to work fine.]
  • In Python, spaces are preferred according to the PEP 8 style guide.

What are conditional statements? Why do we need them?

if Statements

  • Enables logical branching and recoding of data.
  • BUT, if statements can result in long code branches, repeated code.
  • Best to keep if statements short.
  • Keep in mind the Zen of Python when writing if statements.

Conditional Statements and Indentation

  • The syntax for control structures in Python use colons and indentation.
  • Beware that indentation affects flow.
  • if statemenet enable logic.
  • elif give additional conditions.
  • else gives what to do if other conditions are not met.
y = 5
x = 3
if x > 0:
    print ('x is strictly positive')
    print (x)    
print ('Finished.', x, y)
x

x is strictly positive
3
Finished. 3 5
3
x = 1
y = 0
if x > 0:
    print ('x is greater than 0')
    if y > 0:
        print ('& y is also greater than 0')
    elif y<0:
        print ('& y is  0')
    else:
        print ('& y is equal 0')
print ("x: ",x)
print ('Finished.')

x is greater than 0
& y is equal 0
x:  1
Finished.
x > 0

True
x != 5 or 

True
x=5
x

5

Python Logit and Conditions

  • Less than <
  • Greater than >
  • Less than or equal ≤ <=
  • Greater than or equal >=
  • Equals ==
  • Not equal !=
  • and can be used to put multiple logical expressions together.
  • or can be used to put multiple logical expressions together.
x = -1
y = 1
if x >= 0 and y >= 0:
    print ('x and y are greater than 0 or 0')
elif  x >= 0 or y >= 0:
    if x > 0:
        print ('x is greater than 0')
    else:
        print ('y is greater than 0')
else:
    print ('neither x nor y greater than 0')


y is greater than 0

Python Conditionals (Alt Syntax)

  • Clean syntax doesn’t get complex branching
  • Python ternary conditional operator (falseValue, trueValue)[<logicalTest>]
  • Lambda Functions as if statement.
x=0
z = 5 if x > 0 else 0
print(z)

0
# This is a form of if statement called ternary conditional operator 
x=1
#The first value is the value if the conditional is false
z=(0, 5)[x>0]
print(z)

5

Why, Why Not Loops?

Why, Why Not Loops?

  • Iterate over arrays or lists easily. for or while loops can be nested.
  • BUT, in many cases for loops don’t scale well and are slower than alternate methods involving functions.
  • BUT, don’t worry about prematurely optimizing code.
  • Often if you are doing a loop, there is a function that is faster. You might not care for small data applications.
  • Keep in mind the Zen of Python when writing for statements.
#Here we are iterating on lists.
sum=0
for ad in [1, 2, 3]: 
    sum+=ad    #This is short hand for sum = sum+ad
    print(sum) 
    
for country in ['England', 'Spain', 'India']:
    print(country)

1
3
6
England
Spain
India
x=[0,1,2]
y=['a','b','c']
#Nested for loops
for a in x:
    for b in y:
        print(a,b)


0 a
0 b
0 c
1 a
1 b
1 c
2 a
2 b
2 c

The for Loop

  • Can accept a range(start, stop, step) or range(stop) object
  • Can break out of it with a break command
z=range(5)
z

range(0, 5)
#Range is a built in function that can be passed to a for loop
#https://docs.python.org/3/library/functions.html#func-range
#Range accepts a number and a (start/stop/step) like the arrange command.
z=range(5)
print(z, type(z))
#Range
for i in z:
    print('Printing ten')
for i in range(5):
    print('Print five more')
for i in range(5,20,2):
    print('i: %d is the value' % (i)) 
    print(f'i:{i}  is the value' ) #This is an alternate way of embedding values in text.

range(0, 5) <class 'range'>
Printing ten
Printing ten
Printing ten
Printing ten
Printing ten
Print five more
Print five more
Print five more
Print five more
Print five more
i: 5 is the value
i:5  is the value
i: 7 is the value
i:7  is the value
i: 9 is the value
i:9  is the value
i: 11 is the value
i:11  is the value
i: 13 is the value
i:13  is the value
i: 15 is the value
i:15  is the value
i: 17 is the value
i:17  is the value
i: 19 is the value
i:19  is the value
#Sometimes you need to break out of a loop
for x in range(3):
    print('x:',x)
    if x == 2:
        break


x: 0
x: 1
x: 2

List, Set, and Dict Comprehension (Fancy for Loops)

  • Python has a special way of compressing list building to a single line.
  • Set Comprehension is very similar, but with the { bracket.
  • Can incorporate conditionals.
  • S
#This is the long way of building lists.
L = []
for n in range(10):
    L.append(n ** 2)
L

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#With list comprehension.  
L=[n ** 2 for n in range(10)]
L

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#Any actions are on left side, any conditionals on right side 
[i for i in range(20) if i % 3 == 0]

[0, 3, 6, 9, 12, 15, 18]

Multiple Interators

  • Iterating on multiple values
[(i, j) for i in range(2) for j in range(3)]

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]

Set Comprehension

  • Remember sets must have unique values.
#We can change it to a setby just changing the brackets.
{n**2 for n in range(6)}

{0, 1, 4, 9, 16, 25}

Dict Comprehension

  • Remember sets must have unique values.
#We can change it to a dictionary by just changing the brackets and adding a colon.
{n:n**2 for n in range(6)}

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

While Loops

  • Performs a loop while a conditional is True.
  • Doesn’t auto-increment.
# While loop is a very interesting 
x = 1
sum=0
while x<10:
    print ("Printing x= %d sum= %d" % (x, sum)) #Note this alternate way of specufiy
    x += 1
    sum+=10

Printing x= 1 sum= 0
Printing x= 2 sum= 10
Printing x= 3 sum= 20
Printing x= 4 sum= 30
Printing x= 5 sum= 40
Printing x= 6 sum= 50
Printing x= 7 sum= 60
Printing x= 8 sum= 70
Printing x= 9 sum= 80

Recoding Variables/Creating Features with for/if

  • Often we want to recode data applying some type of conditional statement to each value of a series, list, or column of a data frame.
  • Regular Expressions can be useful in recoding
#Titanic Preview Women and Children first

gender=['Female', 'Female','Female', 'Male', 'Male', 'Male' ]
age=[75, 45, 15, 1, 45, 4 ]
name = ['Ms. Sally White', 'Mrs. Susan King', 'Amanda Russ', 'Rev. John Smith' ]
survived=[]
for i in range(len(gender)):
#This is encoding a simple model that women survived.  
    if gender[i]=='Female':
        survived.append('Survived')
    else:
        survived.append('Died')      
print(survived)
        
#BUT, we won't typically be using this type of recoding, so we aren't going to do a lot of exercises on it.     

['Survived', 'Survived', 'Survived', 'Died', 'Died', 'Died']

Copyright AnalyticsDojo 2016. This work is licensed under the Creative Commons Attribution 4.0 International license agreement. Adopted from materials Copyright Steve Phelps 2014