Functions and Conditional Statements

Functions and Conditional Statements
Josh Brake
1
Review
• What is a variable?
• How do I print things to the screen?
• What are some common operators in Python?
• What is the difference between = and ==?
2
Functions
To define a function in Python we need to write something like:
def functionName ( args ):
# insert stuff for function to do
return ( optional )
There are several important components to writing functions and other structures.
• def - this keyword tells the interpreter that the next item will be the function name
• functionName - this can be anything but must not have spaces
• args - the arguments for your function. These define the names of the local variables.
• whitespace/indent - this is important! Python doesn’t use braces like many other programming languages to enclose the contents of functions of statements
• return (optional) - command which determines what your function spits back out
Also make sure not to forget the parentheses and colon! One other important concept to talk about here
is global vs. local variables. Local variables are those defined within a function. They can’t be accessed
outside the function. Global variables are variables assigned outside the function and can be accessed both
outside and inside the function.
2.1
Common Function: Print
One of the most common functions we use is print. Here we will learn some more about how to use some of
its features.
2.1.1
String Formatting
One thing we can do with print that we didn’t talk about yet is formatting. This allows us to specify a type
of variable to print in a string without actually including the variable. We do this by creating ‘blanks’ in
our string and then telling print how to fill them in. Some common formatting options for strings are:
• %s - string
• %f - fixed point
1
• %d - decimal
• %b - binary
• %c - character
An example how to do this is to like this:
print " Try to print a decimal (% d ) and string (%) " % (10 , " test string " )
2.1.2
Escape Characters
We also have to be careful that we don’t put special Python characters inside a string. One example of this
is the apostrophe mark. If we want to include this in a string we have to use a ‘\’ before it so that it is
interpreted as part of the string and not the symbol to declare the end of the string. It is also useful to know
that strings are basically an array of characters.
2.1.3
Strings as Arrays
Therefore we can access any character in the string in the following way: print ‘cats’[0]. Indexing starts at
0. Can you guess what this would print?
2.1.4
String Methods
Four common string methods are: len(), lower(), upper(), str() String concatenation is done with +.
3
Conditional Statements
The main conditional statements are if, elif, else.
We can use these instructions to create statements which are only executed if certain conditions are true.
Let’s think about the case where we create the following program.
Here is an example of a conditional statement:
def m anipulateNumbers (x , y ):
if x > y :
ans = x + y
elif x < y :
ans = x - y
else :
ans = " error "
# Another way to account for all possible inputs .
# What happens if we input a string to this function ?
# Will it work ?
# elif x == y :
#
ans = x * y
return ans
print manipulateNumbers (1 ,2)
print manipulateNumbers (2 ,1)
# print manipulateNumbers (2 ,2)
# This will give us problems unless we make sure to account for it !
# To fix it we better make sure we account for the case x == y .
print manipulateNumbers (2 ,2)
2
4
Dot Notation
One powerful notation in Python to make things easier for us is the dot notation. We can use this to call
specific functions or methods that belong to the objects we create. For example, the len() function is a method
of the class string. This means we can either write len(stringVarName) or just write stringVarName.len().
You’ll use this in your homework when you do the codeacademy examples with another common module
in Python, datetime. This module allows us to access many different classes (blueprints for objects) and
methods (functions).
5
Trying it out
• Create a function which takes two inputs and adds the numbers together.
• Create a function to determine the tip you should pay for a meal. It should have input arguments of
total meal price, tip percentage. It should print to the screen how much your tip should be.
• Modify the tip program so that it will split the bill n ways.
• Now what about if you create a function which will convert your quality of service to a tip percentage
and then divide the check.
• Create a function which will print out a decimal number we give it in binary.
3