import random
random.randint(0, 3)
random.randint(0, 3)
print random.randint(0, 3)
print random.randint(0, 3)
print random.randint(0, 3)
# In comments:
# What does randint do?
# What do the values 0 and 3 do?
# Try changing those numbers, rerun the program, and write down what changed.
# What is the difference between random.randint(0,3) and print(random.randint(0,3))?
print
is a built-in function– it takes an input and produces some output when you use it.
Why use functions?
→ Reusable Code
If you find yourself doing something more than once, make it a function.
In other words…
def name_of_function(this):
"""What it do???"""
print "It prints " + this + "!"
Take a look at this one…
def power(base, exponent):
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
power(37, 4)
Write a for loop to print only the odd numbers from this list
list = [3,4,7,13,54,32,653,256,1,41,65,83,92,31]
`
Now, iterate and turn your loop into a function called find_odds
that takes an input and prints the odd numbers in ANY list.
def find_odds(input):
`
To exceed standards, create functions to ADD all of the odd numbers in a list. Then create a function to add all of the even numbers in a list. Test your functions using a randomly generated list of numbers.
def odd_sum(input):
def even_sum(input):
import random
my_randoms = random.sample(range(100), 15)
Meeting Standards (3)
find_odds
takes a list, prints odds.
def find_odds(input):
Exceeding Standards (4)
odd_sum
adds up all odd numberseven_sum
adds up all even numbers