Control structures & Functions
In this lecture
Control structures
◦ If elif family
◦ For
◦ While
Functions
Python for Data Science 2
Control Structures in Python
Execute certain commands only
when certain condition(s) is (are)
satisfied (if-then-else)
Executecertain commands
repeatedly and use a certain logic to
stop the iteration (for, while loops)
Python for Data Science 3
If else family of constructs
If, If else and If-elif - else are a
family of constructs where:
◦ A condition is first checked, if it is
satisfied then operations are
performed
◦ If condition is not satisfied, code exits
construct or moves on to other
options
Python for Data Science 4
If else family of constructs
Task Command
• If construct: • if expression:
statements
• If – else • If expression:
construct: statements
else:
statements
• If – elif - else • If expression1:
construct statements
elif expression2:
statements
else:
statements
Python for Data Science 5
For loop
Execute certain commands Task Command
repeatedly and use a certain for for iter in sequence:
logic to stop the iteration (for statements
loop)
Execute multiple commands
repeatedly as per the specified
logic (nested for loop)
Python for Data Science 6
while loop
A while loop is used when a
set of commands are to be
executed depending on a
specific condition
Task Command
while while (condition is satisfied):
statements
Python for Data Science 7
Example: if else and for loops
• We will create 3 bins from the ‘Price’
variable using If Else and For Loops
• The binned values will be stored as classes
in a new column, ‘Price Class’
• Hence, inserting a new column
Python for Data Science 8
Example: if else and for loops
• A for loop is implemented and the observations are
separated into three categories:
o Price
• up to 8450
• between 8450 and 11950
• greater than 11950
• The classes have been stored in a new column ‘Price Class’
Python for Data Science 9
Example: while loop
• A while loop is used whenever you want to execute
statements until a specific condition is violated
• Here a while loop is used over the length of the
column ‘Price_Class’ and an if else loop is used to bin
the values and store it as classes
Python for Data Science 10
Example: while loop
• Series.value_counts() returns series
containing count of unique values
Python for Data Science 11
Functions in Python
• A function accepts input arguments and produces
an output by executing valid commands present in
the function
• Function name and file names need not be the def function_name(parameters):
same statements
• A file can have one or more function definitions
• Functions are created using the command def and
a colon with the statements to be executed
indented as a block
• Since statements are not demarcated explicitly, It
is essential to follow correct indentation practises
Python for Data Science 12
Example: functions
• Converting the Age variable from months to
years by defining a function
• The converted values will be stored in a
new column, ‘Age_Converted’
• Hence, inserting a new column
Python for Data Science 13
Example: functions
• Here, a function c_convert has been defined
• The function takes arguments and returns one value
Python for Data Science 14
Function with multiple inputs and outputs
Function with multiple inputs and
outputs
• Functions in Python takes
multiple input objects but return
only one object as output
• However lists, tuples or
dictionaries can be used to
return multiple outputs as
required
Python for Data Science 15
Example: function with
multiple inputs and outputs
• Converting the Age variable from months
to years and getting kilometers (KM) run
per month
• The converted values of kilometer will be
stored in a new column,‘km_per_month’
• Hence, inserting a new column
Python for Data Science 16
Example: function with multiple
inputs and outputs
• A multiple input multiple output function c_convert
has been defined
• The function takes in two inputs
• The output is returned in the form of a list
Python for Data Science 17
Example: function with multiple inputs and outputs
• Here, Age and KM columns of the data set are input to the
function
• The outputs are assigned to ‘Age_Converted’ and
‘km_per_month’
Python for Data Science 18
Summary
Control structures
◦ If elif family
◦ For
◦ While
Functions
Python for Data Science 19
THANK YOU