print()
math.ceil()
def
return
def greeting(): print("Have a nice day!\n")
def function_name():
def is_even(x): return x % 2 == 0
149
greetings() if is_even(149): print("This shouldn't happen") else: print("149 is an odd number")
if is_even(149): print("This shouldn't happen") else: print("149 is an odd number")
None
def analyze(number): if number == 0: return "Zero!" if number > 0: return "Positive" return "Negative" if __name__ == "__main__": print("an example", analyze(-1))
def sales(grocery_store, item_on_sale, cost): print(grocery_store + " is selling " + item_on_sale + " for " + cost) if __name__ == "__main__": sales("The Farmer’s Market", "bananas", "$1")
def check_leap_year(year): if year % 4 == 0: return str(year) + " is a leap year." else: return str(year) + " is not a leap year." if __name__ == "__main__": year_to_check = 2024 print(f'{check_leap_year(year_to_check)}')
def model_two(word): ans = word * len(word) print(ans) def main(): print("Starting main...") w = input("Enter a word: ") model_two(w) print("All done!") main()
Visualize Execution
Next
model_two()
times
len(word)
main()
def model_three(word): ans = word * len(word) return ans def main(): print("Starting main...") w = input("Enter a word: ") result = model_three(w) print(result) print("All done!") main()
model_three()
def calculate(w, x, y): a = x b = w + 1 return a + b + w print(calculate(1, 2, 0))
Which of the following contains a function call?
(1) type(4.5) (2) def add_one(x): return x + 1 (3) area(2, 9) (4) print("Hello")
def multi(x, y): return x * y if __name__ == "__main__": result = multi(5, 7) print(result) result = multi(5, "yes") print(result)
def two_things(): str = "Sharon" number = 14 return str, number # Driver code to test above method print(two_things()) name, value = two_things() print(name, value)
[]
list()
lst = [1, 1.23, True, 'hello', None] print(lst) >>> [1, 1.23, True, 'hello', None]
lst = list((1, 1.23, True, 'hello', None)) # notice the double round brackets print(lst) print(len(list)) print(lst[0]) >>> [1, 1.23, True, 'hello', None] 5 1
def steps_to_feet(num_steps): feet_per_step = 3 feet = num_steps * feet_per_step return feet def steps_to_calories(num_steps): steps_per_minute = 70.0 calories_per_minute_walking = 3.5 minutes = num_steps / steps_per_minute calories = minutes * calories_per_minute_walking return calories if __name__ == "__main__": steps = int(input('Enter number of steps walked: ')) feet = steps_to_feet(steps) print(f'Feet: {feet}') calories = steps_to_calories(steps) print(f'Calories: {calories}')
def compute_square(num_to_square): return num_to_square * num_to_square if __name__ == "__main__": c2 = compute_square(7) + compute_square(9) print(f'7 squared plus 9 squared is {c2}')
- look back at previous examples
def change(x): x = 456 x = 123 change(x) print(x)
global
value1 = 10 value2 = 20 def add_values(one, two): return one+two def main(): sum = add_values(value1, value2) print(sum) main()
OR
value1 = 10 value2 = 20 def add_values(): return value1 + value2 def main(): sum = add_values() print(sum) main()
counter = 0 def increment(num): counter = num # is counter local or gloabl? def main(): increment(4) print(counter) main()
counter = 0 def increment(num): global counter counter += num def main(): increment(4) print(counter) main()
global counter
Line 3 : Redefining name 'counter' from outer scope (line 1) It looks like the local variable is hiding a global variable with the same name. Most likely there is nothing wrong with this. I just wanted to remind you that you can't access the global variable like this.
counter = 0 def increment(counter): counter = counter + 1 print(counter) def main(): increment(4) print(counter) main()
def deposit(acct, amount): """Deposit money into an account. Args: acct (int): account number amount (float): money to deposit Returns: float: updated account balance """
"""
Args:
Returns:
help()
centimeters
feet
CM_PER_INCH = 2.54 INCHES_PER_FOOT = 12 def height_US_to_cm(feet_p, inches_p): total_inches = feet_p * INCHES_PER_FOOT + inches_p cm = total_inches * CM_PER_INCH return cm feet = int(input("how many feet? ")) inches = int(input("how many inches? ")) centimeters = height_US_to_cm(feet, inches) print(f'Centimeters: {centimeters:.2f}') # print(feet_p)
def woot(x): print(x) yar(x + 1) print(x) yar(x + 2) def yar(y): if y > 5: print(y * 2) else: foo(y + 1) def foo(z): print(z - 1) yar(z + 1) woot(3)