23 KiB
23 KiB
In [ ]:
def name_of_function(arg1,arg2):
'''
This is where the function's Document String (docstring) goes.
When you call help() on your function it will be printed out.
'''
# Do stuff here
# Return desired resultIn [ ]:
def say_hello():
print('hello')In [ ]:
say_hello()In [ ]:
say_helloIn [ ]:
def greeting(name):
print(f'Hello {name}')In [ ]:
greeting('Jose')In [ ]:
def add_num(num1,num2):
return num1+num2In [ ]:
add_num(4,5)In [ ]:
# Can also save as variable due to return
result = add_num(4,5)In [ ]:
print(result)In [ ]:
add_num('one','two')In [ ]:
def print_result(a,b):
print(a+b)In [ ]:
def return_result(a,b):
return a+bIn [ ]:
print_result(10,5)In [ ]:
# You won't see any output if you run this in a .py script
return_result(10,5)In [ ]:
my_result = print_result(20,20)In [ ]:
my_resultIn [ ]:
type(my_result)In [ ]:
my_result = return_result(20,20)In [ ]:
my_resultIn [ ]:
my_result + my_resultIn [ ]:
2 % 2In [ ]:
20 % 2In [ ]:
21 % 2In [ ]:
20 % 2 == 0In [ ]:
21 % 2 == 0In [ ]:
def even_check(number):
return number % 2 == 0In [ ]:
even_check(20)In [ ]:
even_check(21)In [ ]:
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# Otherwise we don't do anything
else:
passIn [ ]:
check_even_list([1,2,3])In [ ]:
check_even_list([1,1,1])In [ ]:
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# This is WRONG! This returns False at the very first odd number!
# It doesn't end up checking the other numbers in the list!
else:
return FalseIn [ ]:
# UH OH! It is returning False after hitting the first 1
check_even_list([1,2,3])In [ ]:
def check_even_list(num_list):
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we return True
if number % 2 == 0:
return True
# Don't do anything if its not even
else:
pass
# Notice the indentation! This ensures we run through the entire for loop
return FalseIn [ ]:
check_even_list([1,2,3])In [ ]:
check_even_list([1,3,5])In [ ]:
def check_even_list(num_list):
even_numbers = []
# Go through each number
for number in num_list:
# Once we get a "hit" on an even number, we append the even number
if number % 2 == 0:
even_numbers.append(number)
# Don't do anything if its not even
else:
pass
# Notice the indentation! This ensures we run through the entire for loop
return even_numbersIn [ ]:
check_even_list([1,2,3,4,5,6])In [ ]:
check_even_list([1,3,5])In [ ]:
stock_prices = [('AAPL',200),('GOOG',300),('MSFT',400)]In [ ]:
for item in stock_prices:
print(item)In [ ]:
for stock,price in stock_prices:
print(stock)In [ ]:
for stock,price in stock_prices:
print(price)In [ ]:
work_hours = [('Abby',100),('Billy',400),('Cassie',800)]In [ ]:
def employee_check(work_hours):
# Set some max value to intially beat, like zero hours
current_max = 0
# Set some empty value before the loop
employee_of_month = ''
for employee,hours in work_hours:
if hours > current_max:
current_max = hours
employee_of_month = employee
else:
pass
# Notice the indentation here
return (employee_of_month,current_max)In [ ]:
employee_check(work_hours)In [ ]:
example = [1,2,3,4,5]In [ ]:
from random import shuffleIn [ ]:
# Note shuffle is in-place
shuffle(example)In [ ]:
exampleIn [ ]:
mylist = [' ','O',' ']In [ ]:
def shuffle_list(mylist):
# Take in list, and returned shuffle versioned
shuffle(mylist)
return mylistIn [ ]:
mylist In [ ]:
shuffle_list(mylist)In [ ]:
def player_guess():
guess = ''
while guess not in ['0','1','2']:
# Recall input() returns a string
guess = input("Pick a number: 0, 1, or 2: ")
return int(guess) In [ ]:
player_guess()In [ ]:
def check_guess(mylist,guess):
if mylist[guess] == 'O':
print('Correct Guess!')
else:
print('Wrong! Better luck next time')
print(mylist)In [ ]:
# Initial List
mylist = [' ','O',' ']
# Shuffle It
mixedup_list = shuffle_list(mylist)
# Get User's Guess
guess = player_guess()
# Check User's Guess
#------------------------
# Notice how this function takes in the input
# based on the output of other functions!
check_guess(mixedup_list,guess)