Multi-if/else -
Sometimes we want to check multiple conditions and do different things depending on which condition is met. One way to do this is with the multi-if/else. Here, we use a series of "elif" blocks between the if and else to check for additional conditions. Importantly, note that only one of these code blocks will be executed!
In [ ]:
choice = raw_input("Choose option 1, 2, or 3: ") #prompts user to input something on the command line, saves it in a variable. see below!
if (choice == "1"):
print "You have chosen option 1: cake"
elif (choice == "2"):
print "You have chosen option 2: ice cream"
elif (choice == "3"):
print "You have chosen option 3: broccoli"
else:
print "Invalid input."
Nested if/else:
You can put pretty much anything inside the code blocks of an if/else statement, including additional if/else statements! Just make sure that all the indentation is correct.
In [ ]:
test = raw_input("What is 1+1? ") #prompts user to input something on the command line, saves it in a variable. see below!
if (test == "2"):
print "Correct!"
test2 = raw_input("What is 2314*32626? ")
if (test2 == "75496564"):
print "Correct! You passed all my tests!"
else:
print "Sorry, that's wrong."
else:
print "Sorry, that's wrong.
There are actually several different ways to import modules that can make them easier to work with. Here are some examples.
You can import more than one module at a time:
import math, random
You can give a module an alias and use that in your code (good when module name is long):
import random as rnd
rnd.randint(1,100)
You can import individual functions from a module (note that if you do this, you must call the function WITHOUT prefixing it):
from math import log10
log10(100)
You can import all functions as above if you use * (so then you can use all functions without prefixing with the module name):
from math import *
sqrt(64)
Note that this last variation is generally not considered to be a good programming practice because it can lead to a lot of confusion in large scripts with many modules in use (someone reading your script who is not familiar with the modules might have a very hard time figuring out where all these functions are coming from!). Use it sparingly, if at all.
The official Python documentation can be found here: https://docs.python.org/2/index.html
These docs contain a description of every function, module, data type, etc found in standard Python. These docs will often be the first hit when you Google something about Python, so it's worthwhile to go over quickly how to read a typical doc entry.
Here's an example doc entry:
Many functions have both required and optional parameters. These are indicated as above. The meaning of each parameter is usually described in the text below the function.
Here's a trickier example:
This function has one required parameter (iterable) and three optional parameters. However, in order to use the last optional parameter (reverse), you must use all the previous optional parameters as well.
So for example, here are all the possibilities that are allowed for this function:
sorted(iterable)
sorted(iterable, cmp)
sorted(iterable, cmp, key)
sorted(iterable, cmp, key, reverse)
These would not be allowed:
sorted(cmp)
sorted(iterable, key)
sorted(iterable, reverse, cmp, key)
(Of course, when you actually use these functions, you sub in your own variables or values instead of the formal parameter names!)