Functions
Functions are bodies of reusable code for performing specific processes or tasks. They help us do more work with less code.
Function syntax
def my_function(parameters):
'''
Docstring.
Summarize the function's behavior and explain its arguments and
return values.
'''
code block
return value
The return is to tell us that this is the value that we want to come out of the function. Instead of printing, return lets us store this value in a variable.
return vs. print
We can think of it like this: A return statement is like your sister going to the market and bringing you back a bag of potatoes. A print statement is like your sister going to the market, coming home, and telling you what kind of potatoes were for sale.
With the return statement, you have some potatoes to cook. With the print statement, you just know what potatoes are available, but you don’t have any potatoes.
Logical Scaffolding
It is useful to use comments to create a logical scaffolding before writing any code in the body of the function.
Functions vs Methods
Functions and methods are very similar, but there are a few key differences.
Methods are a specific type of function. They are functions that belong to a class. This means that we can use them—or “call” them—by using dot notation.
Method example:
my_string = “The eagles filled the sky.”
my_string.split()
The split method is a function that belongs to the string class. It splits strings on their whitespaces.
Standalone functions do not belong to a particular class and can often be used on multiple classes.
Function example:
sum([6, 3])
9
Some Built-in functions in Python:
- enumerate()
- isinstance()
- dict()
- type()
- len()
- set()
- zip()
Python Operators
Comparators
In Python, we can use comparison operators to compare values. When a comparison is made, Python returns a Boolean result—True or False. Python uses the following comparators:
Operation | Operator |
greater than | > |
greater than or equal to | >= |
less than | < |
less than or equal to | <= |
not equal to | != |
equal to | == |
Logical operators
Python also has three logical operators that can be combined with comparators to create more complex statements. These operators are:
- and: evaluates to True only if both statements are true.
- or: evaluates to True if one or both of the statements are true .
- not: reverses the evaluation. If the statement evaluates to True, returns False; if the statement evaluates to False, returns True.
Examples:
x = 3
my_list = [3, 4, 6, 10]
print(x < 3 and x != 0)
print(x >= len(my_list) or x == min(my_list))
print(x not in my_list)
False
True
False
Arithmetic operators
Python is also capable of performing mathematical operations using a set of built-in operators. These arithmetic operators are:
Operation | Operator | Example |
Addition | + | [IN] 5 + 2 [OUT] 7 |
Subtraction | – | [IN] 5 – 2 [OUT] 3 |
Multiplication | * | [IN] 5 * 2 [OUT] 10 |
Division | / | [IN] 5 / 2 [OUT] 2.5 |
Modulo (the remainder of a division) | % | [IN] 5 % 2 [OUT] 1 |
Exponentiation | ** | [IN] 5 ** 2 [OUT] 25 |
Floor division (the number of times the denominator can fully go into the numerator) | // | [IN] 5 // 2 [OUT] 2 |
There are many other mathematical operations that can be performed in Python using functions from special libraries.
Conditional Statements
Conditional statements are an essential part of programming. They allow us to control the flow of information based on certain conditions. In Python, if, elif, and else statements are used to implement conditional statements.
The basic syntax of if-elif-else statements in Python is as follows:
if condition1:
# block of code to execute if the condition evaluates to True
elif condition2:
# block of code to execute if condition1 evaluates to False
# and condition2 evaluates to True
else:
# block of code to execute if BOTH condition1 and condition2
# evaluate to False
A sample to demonstrate:
x = 8
if x > 5:
print('x is greater than five')
elif x < 5:
print('x is less than five')
else:
print('x is equal to five')
x is greater than five
One more sample:
# Define a function that uses modulo to check if a number is even.
def is_even(number):
if number % 2 == 0:
return True
return False
is_even(14)
True
Notice that we don’t need to add else statement here. It works only because it returns the value inside the if statement.