Python Chap4
Python Chap4
Control Structure
If ... Else
• An "if statement" is written by using the if keyword.
• Indentation
• Python relies on indentation (whitespace at the beginning of a line) to define scope
in the code. Other programming languages often use curly-brackets for this purpose.
If ... Else
• Elif
• The elif keyword is Python's way of saying "if the previous conditions were not true,
then try this condition".
• Else
• The else keyword catches anything which isn't caught by the preceding conditions.
If ... Else
• Short Hand If
• If you have only one statement to execute, you can put it on the same line as the if
statement.
• You can also have multiple else statements on the same line:
If ... Else
• And
• The and keyword is a logical operator, and is used to combine conditional statements:
• Or
• The or keyword is a logical operator, and is used to combine conditional statements:
If ... Else
• Not
• The not keyword is a logical operator, and is used to reverse the result of the
conditional statement:
• Nested If
• You can have if statements inside if statements, this is called nested if statements.
• This is less like the for keyword in other programming languages, and works more like
an iterator method as found in other object-orientated programming languages.
• With the for loop we can execute a set of statements, once for each item in a list, tuple,
set etc.
• The for loop does not require an indexing variable to set beforehand.
• Looping Through a String
• Even strings are iterable objects, they contain a sequence of characters:
For Loops
• The break Statement
• With the break statement we can stop the loop before it has looped through all the
items:
• Note: The else block will NOT be executed if the loop is stopped by a break
statement.
For Loops
• Nested Loops
• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for each iteration of the "outer loop":
• Calling a Function
• To call a function, use the function name followed by parenthesis:
• Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses. You can add
as many arguments as you want, just separate them with a comma.
Functions
• Arguments
• Parameters or Arguments?
• The terms parameter and argument can be used for the same thing: information that are
passed into a function.
• From a function's perspective:
• A parameter is the variable listed inside the parentheses in the function definition.
• An argument is the value that is sent to the function when it is called.
• Number of Arguments
• By default, a function must be called with the correct number of arguments. Meaning that if your
function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.
Functions
• Arbitrary Arguments, *args
• If you do not know how many arguments that will be passed into your function, add
a * before the parameter name in the function definition.
• This way the function will receive a tuple of arguments, and can access the items
accordingly:
• Keyword Arguments
• You can also send arguments with the key = value syntax.
• This way the order of the arguments does not matter.
• Return Values
• To let a function return a value, use the return statement:
Functions
• The pass Statement
• function definitions cannot be empty, but if you for some reason have a function
definition with no content, put in the pass statement to avoid getting an error.
• Positional-Only Arguments
• You can specify that a function can have ONLY positional arguments, or ONLY
keyword arguments.
• To specify that a function can have only positional arguments, add , / after the
arguments:
• Without the , / you are actually allowed to use keyword arguments even if the
function expects positional arguments:
Functions
• But when adding the , / you will get an error if you try to send a keyword argument:
• Keyword-Only Arguments
• To specify that a function can have only keyword arguments, add *, before the
arguments:
• Without the *, you are allowed to use positional arguments even if the function
expects keyword arguments:
• But when adding the *, / you will get an error if you try to send a positional
argument:
Functions
• Combine Positional-Only and Keyword-Only
• You can combine the two argument types in the same function.
• Any argument before the / , are positional-only, and any argument after the *, are
keyword-only.
• Recursion
• Recursion is a common mathematical and programming concept. It means that a
function calls itself. This has the benefit of meaning that you can loop through data
to reach a result.
Lambda
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but can only have one
expression.
• Syntax:
• Use that function definition to make a function that always doubles the number you
send in:
• Or, use the same function definition to make a function that always triples the
number you send in: