V

@Videonerd03

Function basics

Python
3 months ago
def calc_pizza_area(): pi_val = 3.14159265 #the function call to calc_pizza_area() jumps execution to the functions statements pizza_diameter = 12.0 pizza_radius = pizza_diameter / 2.0 pizza_area = pi_val * pizza_radius * pizza_radius return pizza_area

While loop

Python
4 months ago
# What is the output of the following code i1_limit = int(input()) i1 = 1 while i1 < i1_limit: i2 = 3 while i2 <= 9: print(f"{i1} {i2}" , end=" ") i2 += 3

Largest cities

Python
4 months ago
# Dictionary of cities # Keys are city names(strings) #Values are distances (integers) #iterating for city in cities: iterates over the keys(city names) cities = { 'Montreal': 958, # 958 > 0 True --> Best = 'Montreal', distance = 958 'Chicago': 5259,

scoring

Python
4 months ago
# print scores in order from highest to lowest. # Note: List pre-sorted from lowest to highest scores = [75, 77, 80, 85, 90, 95, 99] for scr in reversed(scores): print(scr, end='')

num_neg

Python
4 months ago
# Assign num_neg with the number of below-freezing Celsius tempatures in the list tempatures = [30, 20 , 2, -5, -15, -8, -1, 0, 5, 35] num_neg = 0 for temp in tempatures: if temp < 0: num_neg += 1

While loop that counts iterations: Savings interest program

Python
4 months ago
# program that calculates savings and interest initial_savings = 100000 interest_rate = 0.05 print(f'initial savings of ${initial_savings}') print(f' at {interest_rate*100:.0f}% yearly interest. \n') years = int(input('Enter years: ')) print()

Iron Lung Script

Python
4 months ago
#Feature toggles USE_COLOR = True TITLE_MODE = "once" USE_BG = False # === Title (grunge banner: IRON LUNG) === TITLE_GRUNGE = r""" ██╗██████╗ ██████╗ ██████╗ ███╗ ██╗ ██╗ ██╗ ██╗██╗ ██╗ ███╗ ██╗ ██████╗ ██║██╔══██╗██╔═══██╗██╔═══██╗████╗ ██║ ██║ ██║ ██║██║ ██║ ████╗ ██║██╔════╝

Face-printing program

Python
4 months ago
nose = '0' #looks a little like a nose # Get first character for eyes and mouth user_input = input ("Enter a character ( 'q' for quit): ") user_value = user_input[0] #Loop until user enter sentinel value while user_value != 'q': print(f' {user_value} {user_value} ') #print eyes print(f {nose} ') #print nose

Boolean Operators: Detect specific values

Python
4 months ago
# Write an expression using membership operators that prints "Special number" if special_num is one of the special numbers stored in the list special_list = [-99, 0, 44]. #Sample output with input: 17 special_list = [-99, 0, 44] special_num = int(input()) if special_num in special_list: print('Special number') else:

num_napkins

Python
4 months ago
#Integer num_napkins is read from input representing the number of napkins. Output: #'Medium package', if there are 60 - 90 napkins inclusive. #'Large package', if there are 130 - 180 napkins inclusive. num_napkins = int(input()) if 60 <= num_napkins <= 90: print('Mediunm package')

Different Tax Bracket

Python
4 months ago
#The 34% tax bracket applies to income_input in the range 44000 - 78000 inclusive. #Write an if statement that outputs 'Different tax bracket' if the input income_input is not in this range. Otherwise, output '34% tax bracket'. # Your goal is to print "Different tax bracket" when the value is outside the inclusive range [44000, 78000] # The outside range means: # less than 44000 OR greater than 78000 # Directly check outside (simple and explicit) income_input = int(input())

user_age

Python
4 months ago
# integers user_age and hours_of_sleep are read from input. # if user_age is greater than or equal to 6, then assign hours_of_sleep with hours_of_sleep plus 1 user_age = int(input()) hours_of_sleep = int(input()) if user_age >= 6: hours_of_sleep += 1 print (f'hours_of_sleep: {hours_of_sleep}')

Operator chaining

Python
4 months ago
# Variable user_grade is read from input. Use Operator chaining to complete the if-else expression as follows: # If the value of user_grade is between 9 and 12 (both inclusive), then 'in high school' is output # Otherwise 'not in high school' is output # Ex 1: if the input is 10, then the output is: # in high school # Ex 2: if the input is 8, then the output is: # not in high school # Note 0 <x < 5 evaluates to true if x is between the ranges 0 and 5 (both non-inclu

Range for each branch

Python
4 months ago
# Type the range for each branch # Type ranges as 25-29, or type 30+ for all numbers 30 and larger if num_sales < 10: elif num_sales < 20: # 2nd Branch range: 10-19 elif num_sales < 30: # 3rd Branch range: 20-29 else: # 4th Branch range: 30+ # if execution reaches here, the previous expression of num_sales < 30 must have been false num_sales is 30 or greater. No upper limit is specified

User Age

Python
4 months ago
user_age = int(input('Enter your age: ')) if user_age < 16: print('Too Young.') insurance_price = 0 elif user_age < 25: insurance_price = 4800 elif user_age < 40:

User_Val

Python
4 months ago
# user_val read from input as a floating-point value. Using format specification, output the following: # user_val in fixed-point notation with three places of precision # user_val in exponent notation user_val = float(input()) # this one is asking you to print the same number into two different formats using format specifications (the stuff after : inside {} in an f-string) # Fixed-point notation with 3 digits after the decimal

List countries_list

Python
4 months ago
# List countries_list is created with the first three countries read from input. # Remove country 'Norway' from countries_list. Then, read an additional country from input and append the country to countries_list #Reads three values from input into countries_list countries_list = [input(), input(), input()] countries_list.remove('Norway') new_country = input() countries_list.append(new_country)

States_list

Python
4 months ago
#list states_list is created with two states read from input. # Then, list names_list is created with two names read from input. On one line , Output: # names_list's first element # 'is from' # states_list's second element # a period ('.') states_list = [input(), input()]

Adding and removing list elements

Python
4 months ago
my_list = [10, 'bw'] print(my_list) # append() adds an element to the end of the list my_list.append('abc') print(f'After append: {my_list}') #pop removes the element at the given index from the list # 'bw', which is at index 1, is removed and 'abc' is now at index 1 my_list.pop(1)

school python lists

Python
4 months ago
students = [ { "name": "Amaya Sanders", "course": "Computer Science", "location": "Seattle WA ", "university": "Pacific Northwest University", "graduated": True, "graduation_date": "May 15, 2024", "employed_in_field": True },