This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def basic_calculator(): | |
num1 = float(input(" Enter First Number: ")) | |
op = input("Enter Operator: ") | |
num2 = float(input("Enter Second Number: ")) | |
if op == "+": | |
print(num1 + num2) | |
elif op == "-": | |
print(num1 - num2) | |
elif op == "/": | |
print(num1 / num2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calc_age(current_year, birth_year): | |
age = current_year - birth_year | |
return age | |
my_age = calc_age(2020, 2001) | |
print(f"I am {my_age} years old.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def repeat_song(repeats, num_repeats=1): | |
return repeats*num_repeats | |
merrily = repeat_song("Merrily ", 4) | |
lyrics = repeat_song("Row ", 3) + "Your Boat. Gently down the stream. " + merrily + "Life is about a dream!" | |
song = repeat_song(lyrics) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_mass = 22680 | |
train_acceleration = 10 | |
train_distance = 100 | |
bomb_mass = 1 | |
def f_to_c(f_temp, c_temp): | |
c_temp = (f_temp - 32) * 5/9 | |
return c_temp | |
f100_in_celsius = (f_to_c(100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num1 = float(input("Enter first num for AVG: ")) | |
num2 = float(input("Enter second num for AVG: ")) | |
def avg_num(num1, num2): | |
return (num1 + num2) / 2 | |
print(avg_num(num1, num2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dog_years(name, age): | |
# Returned function parameters (name/age) and added string to RETURN. | |
return name+", you are "+str(age*7)+" years old in dog years" | |
# When you print/call the function and give it values, | |
# it will print name + age INCLUDING string from RETURN. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (boolean operators) | |
# if statements = combining with function parameters (gpa/class_credits) | |
# and operator = combining True/False expressions (eg; My car is broken "and" it is raining) | |
# not operator = Reverse the result, returns False if the result is true (Can be combined with "and") | |
# for last "if" statement, you can also use "else" because that would print the final possible outcome of the function. | |
def graduation_requirements(gpa, class_credits): | |
if (gpa >= 2.0) and (class_credits >= 120): | |
return "You meet the requirements to graduate!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# else if statements (elif) | |
# we use "else-if" because using "if" statements for all of the statements would result in ALL IN RANGE being PRINTED! | |
def grade_converter(gpa): | |
if gpa >= 4.0: | |
return "A" | |
elif gpa >= 3.0: | |
return "B" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# try and except statements (Make errors more readable or hide them) | |
def raises_value_error(): | |
try: | |
# Find errors with code in between "try" and "except" statements | |
raise 0 / 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def movie_review(rating): | |
if rating <= 5: | |
return "Avoid at all costs!" | |
elif 5 < rating < 9: # This shows us an in between comparison | |
return "This one was fun." | |
elif rating >= 9: | |
return "Outstanding!" |
OlderNewer