Podcast
Questions and Answers
What will sumUp()
return if the user enters 4?
What will sumUp()
return if the user enters 4?
- 10 (correct)
- 15
- 4
- 24
The multUp(n)
function calculates the _______ of all integers from 1 to n. Fill in the blank.
The multUp(n)
function calculates the _______ of all integers from 1 to n. Fill in the blank.
product
If L = [5, 10, 15, 20]
and n = 5
, what will sel_multUp(L, n)
return?
If L = [5, 10, 15, 20]
and n = 5
, what will sel_multUp(L, n)
return?
- 25
- 15000 (correct)
- 50
- 0
The provided my_len_string(S)
function correctly calculates the length of a string S using the built-in len()
function.
The provided my_len_string(S)
function correctly calculates the length of a string S using the built-in len()
function.
What does the range_test()
function print?
What does the range_test()
function print?
What value does function mystery1(2, 5)
return?
What value does function mystery1(2, 5)
return?
In the temperature()
function provided, what will be printed to the console if the user enters 50?
In the temperature()
function provided, what will be printed to the console if the user enters 50?
In the mystery3()
function, what values will be printed to the console?
In the mystery3()
function, what values will be printed to the console?
In the OddEven(n)
function, if n
is divisible by 2 then the number is ______.
In the OddEven(n)
function, if n
is divisible by 2 then the number is ______.
Match each code snippet with its intended output when executed.
Match each code snippet with its intended output when executed.
Flashcards
"""Write a function called sumUp() which prompts
the user to enter an integer number n and returns
the sum of integers from 1 to n inclusive. You can
assume n is greater than or equal to 0.
"""Write a function called sumUp() which prompts the user to enter an integer number n and returns the sum of integers from 1 to n inclusive. You can assume n is greater than or equal to 0.
def sumUp(): num =int(input("Enter an integer number:")) sum =0 for i in range (1,num+1): sum=sum+i return sum
"""Write a function called multUp(n) which returns the
multiplication of all integers from 1 to n inclusive.
You can assume n is greater than 0.
"""Write a function called multUp(n) which returns the multiplication of all integers from 1 to n inclusive. You can assume n is greater than 0.
def multUp(n): mult=1 for i in range(1,n+1): mult=mult*i return mult
"""Write a function called sel_multUp(L, n) where
L is a list of integers and n is an integer. This function
returns the product of all integers in the list which are
divisible by the integer n with zero remainder.
Assume all numbers in the list are integers and each
is greater than or equal to zero. You can also assume
there is at least one number in the list which,
when divided by n, has zero remainder.
"""Write a function called sel_multUp(L, n) where L is a list of integers and n is an integer. This function returns the product of all integers in the list which are divisible by the integer n with zero remainder. Assume all numbers in the list are integers and each is greater than or equal to zero. You can also assume there is at least one number in the list which, when divided by n, has zero remainder.
def sel_multUp(L, n): mult=1 for i in L: if i% n == 0: mult= mult * i return mult
"""Write a function called my_len_string(S) which will
return the length of the string, S. Please refer to the
sample output given below for the correct required
format of the output. Please do not use the built-in
Python function len(L).
"""Write a function called my_len_string(S) which will return the length of the string, S. Please refer to the sample output given below for the correct required format of the output. Please do not use the built-in Python function len(L).
Signup and view all the flashcards
def range_test():
x = list(range(2, 10, 3))
y = list(range(15, 2, -1))
z = list(range(4))
print(x)
print(y)
print(z)
def range_test(): x = list(range(2, 10, 3)) y = list(range(15, 2, -1)) z = list(range(4)) print(x) print(y) print(z)
Signup and view all the flashcards
def temperature():
x = float(input("Enter temperature: "))
if x >= 80:
print("Let's go to the Jersey Shore")
elif 60 <= x < 80:
print("Cookout Time!")
elif 40 <= x < 60:
print("Getting cool")
elif 20 <= x < 40:
print("Time to go skiing??")
elif x == 15:
print("Its 15 degrees")
else:
print("Anyone ready for a trip to Florida?")
what is the output if the temp is 39?
def temperature(): x = float(input("Enter temperature: ")) if x >= 80: print("Let's go to the Jersey Shore") elif 60 <= x < 80: print("Cookout Time!") elif 40 <= x < 60: print("Getting cool") elif 20 <= x < 40: print("Time to go skiing??") elif x == 15: print("Its 15 degrees") else: print("Anyone ready for a trip to Florida?")
what is the output if the temp is 39?
Signup and view all the flashcards
def mystery3():
"""Determine the output of this function"""
x = 0
y = 3
for i in range(0, 11, 2):
print(x * y)
x = x+1
def mystery3(): """Determine the output of this function""" x = 0 y = 3 for i in range(0, 11, 2): print(x * y) x = x+1
Signup and view all the flashcards
"""Basic Python program to check is the number n
is even or odd."""
"""Basic Python program to check is the number n is even or odd."""
Signup and view all the flashcards
"""The following three Python programs check to see
if you understand the operation of a for loop with a
string, list, and the range function. Try each of these
programs and play and have fun."""
"""The following three Python programs check to see if you understand the operation of a for loop with a string, list, and the range function. Try each of these programs and play and have fun."""
Signup and view all the flashcards
"""The following Python program checks your understanding
of the input statement, int and float, and formating using the
print statement."""
"""The following Python program checks your understanding of the input statement, int and float, and formating using the print statement."""
Signup and view all the flashcards
"""As a reminder, def main() is automatically run and requires
main() at the end. """
"""As a reminder, def main() is automatically run and requires main() at the end. """
Signup and view all the flashcards
Study Notes
Summing Integers Function
- A function called
sumUp()
prompts the user for an integern
and calculates the sum of integers from 1 ton
inclusive. - It assumes that
n
is greater than or equal to 0. - For example, if the input is 5, the function returns 15 (1+2+3+4+5).
- If the input is 10, the function returns 55.
- If the input is 0, the function returns 0.
- The function initializes a variable
sum
to 0. - It iterates through numbers from 1 to
n
(inclusive), adding each number to thesum
.
Multiplying Integers Function
- A function called
multUp(n)
calculates the multiplication of all integers from 1 ton
inclusive. - It assumes that
n
is greater than 0. - For example,
multUp(3)
returns 6 (1 * 2 * 3). multUp(6)
returns 720.multUp(1)
returns 1.- The function initializes a variable
mult
to 1. - It iterates from 1 to
n
(inclusive), multiplyingmult
by each number.
Selective Multiplication Function
- A function called
sel_multUp(L, n)
takes a list of integersL
and an integern
as input. - It returns the product of all integers in
L
that are divisible byn
with no remainder. - It assumes all numbers in
L
are integers greater than or equal to zero. - It also assumes that there is at least one number in
L
divisible byn
. - For example, given the list
L = [9, 4, 3, 17, 2]
andn = 3
, the function returns 27 (9 * 3). - Given
L = [6, 4, 12, 7]
andn = 4
, it returns 48 (4 * 12). - The function initializes a variable
mult
to 1. - It iterates through each number
i
in the listL
. - If
i
is divisible byn
(i.e.,i % n == 0
), it multipliesmult
byi
.
String Length Function
- A function called
my_len_string(S)
calculates the length of the stringS
. - For the string "Go Ducks!", would return "The length of the string Go Ducks! is 9".
- For the string "MIS-110", would return "The length of the string MIS-110 is 7".
- The function initializes a variable
count
to 0. - It iterates through each character
i
in the stringS
(This is an error in the source text, it should iterate through L). - It increments count for each character.
- Finally, print "The length of the string", S, "is", count.
Range Test Function
- The function
range_test()
demonstrates the use of therange()
function. x = list(range(2, 10, 3))
creates a list of numbers starting from 2, up to (but not including) 10, with a step of 3, resulting in[2, 5, 8]
.y = list(range(15, 2, -1))
creates a list starting from 15, down to (but not including) 2, with a step of -1, resulting in[15, 14, 13, 12, ..., 3]
.z = list(range(4))
creates a list starting from 0, up to (but not including) 4, with a default step of 1, resulting in[0, 1, 2, 3]
.- The outputs of x, y, and z are then printed.
Mystery Function
- The function
mystery1(x, y)
performs a series of calculations based on the inputsx
andy
. - If
x = 2
andy = 5
:a = x * y
results in 10.b = y // x
results in 2.c = x % y
results in 2.d = (x + y) % 4
results in 3.answer = (a // d) * (b % (c + 1))
which is(10 // 3) * (2 % 3)
which equals 3 * 2 = 6
- The function returns the value of answer, which is 6.
Temperature Function
- The function
temperature()
prompts the user to enter a temperature. - If the temperature is 80 or above, it prints "Let's go to the Jersey Shore".
- If it's between 60 and 80 (exclusive), it prints "Cookout Time!".
- If it's between 40 and 60 (exclusive), it prints "Getting cool".
- If it's between 20 and 40 (exclusive), it prints "Time to go skiing??".
- If it's exactly 15, it prints "Its 15 degrees".
- Otherwise, it prints "Anyone ready for a trip to Florida?".
- If the input is 39, the output is "Time to go skiing??".
Another Mystery Function
- The
mystery3()
function initializesx
to 0 andy
to 3. - It then iterates through the range from 0 to to 11 (exclusive), incrementing by 2 each time.
- Inside the loop, it prints the product of
x
andy
and then incrementsx
by 1 - The loop will go through these steps:
- i = 0: prints x * y (0 * 3 = 0), x becomes 1
- i = 2: prints x * y (1 * 3 = 3), x becomes 2
- i = 4: prints x * y (2 * 3 = 6), x becomes 3
- i = 6: prints x * y (3 * 3 = 9), x becomes 4
- i = 8: prints x * y (4 * 3 = 12), x becomes 5
- i = 10: prints x * y (5 * 3 = 15), x becomes 6
Mystery Function with Rounding
- The
mystery(n, k)
function calculatesA
as the integer division ofn
byk
(n//k
). - It calculates
B
as the remainder ofn
divided byk
(n%k
). - It calculates the sum of numbers from 0 to 9.
- It then calculates x as
A + B + sum
. - Finally, it returns
x
rounded to 3 decimal places.
Odd or Even Function
- The
OddEven(n)
function checks if a numbern
is even or odd. - If
n
is divisible by 2 (i.e.n % 2 == 0
), it prints "Number is even.". - Otherwise, it prints "Number is odd.".
Loop Functions: String, List, Range
LoopString(S)
iterates through each characteri
in the stringS
and prints each character on a new line.LoopList(L)
iterates through each elementi
in the listL
and prints each element on the same line, separated by a space.LoopRange(n)
iterates through the numbers from 0 ton-1
and prints each number on the same line, separated by a space.
Input and Print Function
- The
InputPrint()
function prompts the user for their name, age, and favorite float. - It then prints a formatted string with the collected information, rounding the float to 2 decimal places.
Simple Sum Function
- The
simpleSum(x, y)
function calculates the sum of two numbersx
andy
. - It prints the sum.
- It can optionally return the sum.
Main Function
- The
main()
function prompts the user to enter two numbers. - It then prints the sum of the two numbers
- The statement
main()
executes the code within the main Function
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.