WEEK 2 :: PYTHON CRASH COURSE

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are the values passed into functions as input called?

Parameters. A parameter, also sometimes called an argument, is a value passed into a function for use within the function.

This code is supposed to display "2 + 2 = 4" on the screen, but there is an error. Find the error in the code and fix it, so that the output is correct.

a = 2 b = a+a print("2 + 2 = " + str(b) )

What do you call a combination of numbers, symbols, or other values that produce a result when evaluated?

an expression

Figure out what's the relationship between the strings "cat" and "Cat" by replacing the plus sign with comparison operators.

print("cat" > "Cat") "cat" is larger than "Cat"(In Python uppercase letters are alphabetically sorted before lowercase letters.)

Why does this code raise an error: print("1234"+5678)

Because Python doesn't know how to add a number to a string.(Python can add a number to a number or a string to a string, but it doesn't know how to add a number to a string.)

What is the purpose of the def keyword?

Use to define a new function. When defining a new function, we must use the def keyword followed by the function name and properly indented body.

Is "A dog" smaller or larger than "A mouse"? Is 9999+8888 smaller or larger than 100*100? Replace the plus sign in the following code to let Python check it for you and then answer.

"A dog" smaller or larger than "A mouse" (TRUE) 9999+8888 smaller or larger than 100*100 (TRUE)

What's the output of this code if number equals 10? if number > 11: print(0) elif number != 10: print(1) elif number >= 20 or number < 12: print(2) else: print(3)

2

What's the value of this Python expression: (2**2) == 4?

True. The conditional operator == checks if two values are equal. The result of that operation is a boolean: either True or False.

Use the get_seconds function to work out the amount of seconds in 2 hours and 30 minutes, then add this number to the amount of seconds in 45 minutes and 15 seconds. Then print the result.

def get_seconds(hours, minutes, seconds): return 3600*hours + 60*minutes + seconds amount_a = get_seconds(2, 30, 0) amount_b = get_seconds(0, 45, 15) result = amount_a + amount_b print(result)

Complete the script by filling in the missing parts. The function receives a name, then returns a greeting based on whether or not that name is "Taylor".

def greeting(name): if name == "Taylor": return "Welcome back Taylor!" else: return "Hello there, " + name print(greeting("Taylor")) print(greeting("John"))

In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed.

# REPLACE THIS STARTER CODE WITH YOUR FUNCTION def month_days(month, days): print (month +" has " + str(days) + " days.") month_days ("June", 30) month_days("July", 31)

This function compares two numbers and returns them in increasing order. Fill in the blanks, so the print statement displays the result of the function call in order. Hint: if a function returns multiple values, don't forget to store these values in multiple variables

# This function compares two numbers and returns them # in increasing order. def order_numbers(number1, number2): if number2 > number1: return number1, number2 else: return number2, number1 # 1) Fill in the blanks so the print statement displays the result # of the function call (smaller, bigger)= order_numbers(100, 99) print(smaller, bigger)

In this scenario, two friends are eating dinner at a restaurant. The bill comes in the amount of 47.28 dollars. The friends decide to split the bill evenly between them, after adding 15% tip for the service. Calculate the tip, the total amount to pay, and each friend's share, then output a message saying "Each person needs to pay: " followed by the resulting number.

bill = 47.28 tip = bill * (15/100) total = bill + tip share = total/2 print("Each person needs to pay: " + str(share))

This function converts miles to kilometers (km). Complete the function to return the result of the conversion Call the function to convert the trip distance from miles to kilometers Fill in the blank to print the result of the conversion Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the result

def convert_distance(miles): km = miles * 1.6 # approximately 1.6 km in 1 mile return km my_trip_miles = convert_distance(55) # 2) Convert my_trip_miles to kilometers by calling the function above my_trip_km = my_trip_miles # 3) Fill in the blank to print the result of the conversion print("The distance in kilometers is " + str(my_trip_km)) # 4) Calculate the round-trip in kilometers by doubling the result, # and fill in the blank to print the result print("The round-trip in kilometers is " + str(my_trip_miles* 2))

The is_positive function should return True if the number received is positive, otherwise it returns None. Can you fill in the gaps to make that happen?

def is_positive(number): if number > 0: return True

The is_positive function should return True if the number received is positive and False if it isn't. Can you fill in the gaps to make that happen?

def is_positive(number): if number > 0: return True else: return False

Let's revisit our lucky_number function. We want to change it, so that instead of printing the message, it returns the message. This way, the calling line can print the message, or do something else with it if needed. Fill in the blanks to complete the code to make it work.

def lucky_number(name): number = len(name) * 9 return "Hello " + name + ". Your lucky number is " + str(number) ___ print(lucky_number("Kay")) print(lucky_number("Cameron")) The function now returns the message, for the calling line to use it in any way it wants to.

The number_group function should return "Positive" if the number received is positive, "Negative" if it's negative, and "Zero" if it's 0. Can you fill in the gaps to make that happen?

def number_group(number): if number > 0: return "Positive" elif number == 0: return "Zero" else: return "Negative"

Flesh out the body of the print_seconds function so that it prints the total amount of seconds given the hours, minutes, and seconds function parameters. Remember that there are 3600 seconds in an hour and 60 seconds in a minute.

def print_seconds(hours, minutes, seconds): print(hours * 3600 + minutes * 60 + seconds) print_seconds(1,2,3)

This function to calculate the area of a rectangle is not very readable. Can you refactor it, and then call the function to calculate the area with base of 5 and height of 6? Tip: a function that calculates the area of a rectangle should probably be called rectangle_area, and if it's receiving base and height, that's what the parameters should be called.

def rectangle_area (base, height): area = base * height print ("The area is " + str(area)) rectangle_area (5, 6)

This code is supposed to take two numbers, divide one by another so that the result is equal to 1, and display the result on the screen. Unfortunately, there is an error in the code. Find the error and fix it, so that the output is correct.

numerator = 10 denominator = 10 result = numerator / denominator print(result)

Practice writing some expressions and conversions yourself. In this scenario, we have a directory with 5 files in it. Each file has a different size: 2048, 4357, 97658, 125, and 8. Fill in the blanks to calculate the average file size by having Python add all the values for you, and then set the files variable to the number of files. Finally, output a message saying "The average size is: " followed by the resulting number. Remember to use the str() function to convert the number into a string.

total = 2048 + 4357 + 97658 + 125 + 8 files = 5 average = total / files print("The average size is " + str(average))

Combine the variables to display the sentence "How do you like Python so far?"

word1 = "How" word2 = "do" word3 = "you" word4 = "like" word5 = "Python" word6 = "so" word7 = "far?" print(str(word1) + " " + str(word2) + " " + str(word3) + " " + str(word4) + " " + str(word5) + " " + str(word6) + " " + str(word7))


Kaugnay na mga set ng pag-aaral

PrepU: Chapter 19: Nursing Management of Pregnancy at Risk: Pregnancy-Related Complications

View Set

Managerial Accounting Chapter 2 Key Terms, Homework

View Set

Reproductive System ATI-Med/Surg

View Set

SIE/Top Off Prep: SIE Test Question Review

View Set

IV Calculations (Pump or Flow rates)

View Set