Podcast
Questions and Answers
What will be the output of the following code?
print(1+1)
What will be the output of the following code?
print(1+1)
2
What will be the output of the following code?
print(str(1)+str(1))
What will be the output of the following code?
print(str(1)+str(1))
11
What will be the output of the following code?
print("Hello"*2)
What will be the output of the following code?
print("Hello"*2)
HelloHello
What does the following code print?
foo = 20 bar = foo*2 print (bar)
What does the following code print?
foo = 20 bar = foo*2 print (bar)
Is this code equivalent to the code above?
foo = 20 print (foo*2)
Is this code equivalent to the code above?
foo = 20 print (foo*2)
Is there anything wrong with this code?
foo = 20 print(int(foo)*2)
Is there anything wrong with this code?
foo = 20 print(int(foo)*2)
Is there anything wrong with the code below? Will it run? Will the answer be logically correct?
# double the user's input myInput = input("Please input a number here: ") print (myInput*2)
Is there anything wrong with the code below? Will it run? Will the answer be logically correct?
# double the user's input myInput = input("Please input a number here: ") print (myInput*2)
What is the output of the following code?
a = 25 b = False if (a < 25) or (not b): b = not b elif (a >= 25) and b: a /= 5 else: a += 7 b = False print(f"{a}: {b}")
What is the output of the following code?
a = 25 b = False if (a < 25) or (not b): b = not b elif (a >= 25) and b: a /= 5 else: a += 7 b = False print(f"{a}: {b}")
Are the following two functions ______?
Are the following two functions ______?
What is the difference between the two functions below?
def baz(x): return 2*x def qux(x): print(2*x)
What is the difference between the two functions below?
def baz(x): return 2*x def qux(x): print(2*x)
Using the same function definitions as above, identify the problem in the following code. Will it run? Will the results be what you expect? Why or why not?
a = baz (3) b = qux (3) print(a) print(b)
Using the same function definitions as above, identify the problem in the following code. Will it run? Will the results be what you expect? Why or why not?
a = baz (3) b = qux (3) print(a) print(b)
Identify the problem in the following piece of code.
def myFunc(x): int(input("Input any number.")) x = if x > 10: return True return False
Identify the problem in the following piece of code.
def myFunc(x): int(input("Input any number.")) x = if x > 10: return True return False
Write a function that takes a numeric value x (int), as well as an integer n, and uses a looping structure to return the exponent x^n.
Write a function that takes a numeric value x (int), as well as an integer n, and uses a looping structure to return the exponent x^n.
Write a function that takes a list as an argument to find the sum of all elements in the list using a looping structure.
Write a function that takes a list as an argument to find the sum of all elements in the list using a looping structure.
Write a function that takes a list as an argument and returns a new list that removes all duplicates in the list.
Write a function that takes a list as an argument and returns a new list that removes all duplicates in the list.
Trace through the code manually to predict what will happen when the code is executed. Then, convert the code snippet into Python code to verify your answers.
a = [] for i in range(50): a.append(i) print(a)
Trace through the code manually to predict what will happen when the code is executed. Then, convert the code snippet into Python code to verify your answers.
a = [] for i in range(50): a.append(i) print(a)
What is the output of the code snippet for i in range(5): a.pop(i) print(f"after pop", a)
What is the output of the code snippet for i in range(5): a.pop(i) print(f"after pop", a)
What is the output of the code snippet for i in range(5): a.remove(a[i]) print(f"after remove a[i]", a)
What is the output of the code snippet for i in range(5): a.remove(a[i]) print(f"after remove a[i]", a)
What is the output of the code snippet b=a[:] for i in range(5): a.remove(b[i]) print(f"after remove b[i]",a)
What is the output of the code snippet b=a[:] for i in range(5): a.remove(b[i]) print(f"after remove b[i]",a)
Write a function that returns a N-by-N 2-D list of randomly generated numbers between 0 and 100. Take n as an argument to your function.
Write a function that returns a N-by-N 2-D list of randomly generated numbers between 0 and 100. Take n as an argument to your function.
Then, write a function to print this 2-D list, line by line, as a string. The function must be counter-controlled. The functions must take two arguments: grid (your 2-D list) and n (the size of your list.)
Then, write a function to print this 2-D list, line by line, as a string. The function must be counter-controlled. The functions must take two arguments: grid (your 2-D list) and n (the size of your list.)
Consider the following code:
myDict = {} for i in range (1,5): myDict[i] = i*2 for key in myDict: print(f"{key}: {myDict[key]}")
Bob says: I believe this code will print out "1:2, 2:4, 3:6, 4:8, 5:10", in that order, all on new lines. However, Alice disagrees. Who is correct? If Alice is correct, what are the reasons Bob is wrong?
Consider the following code:
myDict = {} for i in range (1,5): myDict[i] = i*2 for key in myDict: print(f"{key}: {myDict[key]}")
Bob says: I believe this code will print out "1:2, 2:4, 3:6, 4:8, 5:10", in that order, all on new lines. However, Alice disagrees. Who is correct? If Alice is correct, what are the reasons Bob is wrong?
Assume that the userCart variable is a list of strings representing the items in the user's cart. For example, ["banana", "apple", "bacon", "apple"] is a valid shopping cart. Write a function, receipt(userCart), to calculate the total cost of a user's cart and print a nicely formatted receipt.
Assume that the userCart variable is a list of strings representing the items in the user's cart. For example, ["banana", "apple", "bacon", "apple"] is a valid shopping cart. Write a function, receipt(userCart), to calculate the total cost of a user's cart and print a nicely formatted receipt.
We will calculate the sum of a list of numbers recursively. However, we will trace the steps to solve this by hand. You may also implement the code if you wish. Complete the following trace: sum([5,6,2,8,9])
We will calculate the sum of a list of numbers recursively. However, we will trace the steps to solve this by hand. You may also implement the code if you wish. Complete the following trace: sum([5,6,2,8,9])
Now, we will do the same for the product of a list of numbers. prod([10,9,6,4,2]) # Complete this line as well
Now, we will do the same for the product of a list of numbers. prod([10,9,6,4,2]) # Complete this line as well
We define n!=n*(n-1)(n-2)...21. 1. What is the base case? 2. What is the recursive case? 3. Write the code to define the recursive function fact(n).
We define n!=n*(n-1)(n-2)...21. 1. What is the base case? 2. What is the recursive case? 3. Write the code to define the recursive function fact(n).
What will happen when this function is run?
def myFunc(x): return x + myFunc(x-1)
What will happen when this function is run?
def myFunc(x): return x + myFunc(x-1)
What will happen when this function is run?
def myFunc(x): if x == 0: return x print(myFunc(x-1))
What will happen when this function is run?
def myFunc(x): if x == 0: return x print(myFunc(x-1))
What will each line of the following code print?
print(1+1)
What will each line of the following code print?
print(1+1)
What will each line of the following code print?
print(str(1)+str(1))
What will each line of the following code print?
print(str(1)+str(1))
What will each line of the following code print?
print("Hello"*2)
What will each line of the following code print?
print("Hello"*2)
Are the following two functions equivalent?
def foo(x): myVar = 2*x return myVar def bar(x): return 2*x
Are the following two functions equivalent?
def foo(x): myVar = 2*x return myVar def bar(x): return 2*x
Flashcards
String Concatenation
String Concatenation
The +
operator concatenates strings, not performing arithmetic addition.
Converting Integer to String
Converting Integer to String
The str()
function converts an integer to a string, allowing concatenation with other strings.
String Replication
String Replication
Multiplying a string by an integer repeats the string that many times.
Variables in Programming
Variables in Programming
Signup and view all the flashcards
Assignment Operator
Assignment Operator
Signup and view all the flashcards
User Input
User Input
Signup and view all the flashcards
Converting String to Integer
Converting String to Integer
Signup and view all the flashcards
Conditional Statement
Conditional Statement
Signup and view all the flashcards
Elif Statement
Elif Statement
Signup and view all the flashcards
Else Statement
Else Statement
Signup and view all the flashcards
Not Operator
Not Operator
Signup and view all the flashcards
For Loop
For Loop
Signup and view all the flashcards
Functions
Functions
Signup and view all the flashcards
Function Parameters
Function Parameters
Signup and view all the flashcards
Return Statement
Return Statement
Signup and view all the flashcards
Lists
Lists
Signup and view all the flashcards
Dictionaries
Dictionaries
Signup and view all the flashcards
Recursion
Recursion
Signup and view all the flashcards
Classes
Classes
Signup and view all the flashcards
Objects
Objects
Signup and view all the flashcards
Object Attributes
Object Attributes
Signup and view all the flashcards
Object Methods
Object Methods
Signup and view all the flashcards
Files
Files
Signup and view all the flashcards
File Modes
File Modes
Signup and view all the flashcards
Debugging
Debugging
Signup and view all the flashcards
Try-Except Block
Try-Except Block
Signup and view all the flashcards
Searching Algorithms
Searching Algorithms
Signup and view all the flashcards
Sorting Algorithms
Sorting Algorithms
Signup and view all the flashcards
Binary Search
Binary Search
Signup and view all the flashcards
Study Notes
Data Types
- Code will print the results of arithmetic and string operations
print(1 + 1)
will print 2print(str(1) + str(1))
will print "11"print(str(1) + 1)
will result in an error because you can't add a string to an integerprint("Hello" * 2)
will print "HelloHello"print("Hello" + 3)
will result in an error because you can't add a string to an integer
Variables
- Code will calculate and print the value of
bar
. foo
is assigned the integer value 20bar
will store the result of multiplyingfoo
by 2. Thus bar would be 40- Code will print the value stored in
bar
, which is 40 - Alternative code that
print(foo * 2)
does the same operation without the extra variable bar - Third code, print(int(foo) *2) , does the same as
foo * 2
User Input
- Code takes user input and doubles it.
- Code attempts to multiply string (myInput) by 2.
- String multiplication does not apply directly, leading to an error
- Input must be cast to an integer.
Branching Control Structures
- Code uses
if
,elif
, andelse
statements to conditionally execute code. a
is assigned the value 25b
is assigned the value False.- Code will print the value of
a
andb
determined from conditional statement if-else tree - if
a < 25
ornot b
is true, thenb
is set tonot b
, or true. - if
a >= 25
andb
is true, thena
is divided by 5. - otherwise
a
is increased by 7. - Values are printed (a ,b)
Looping Control Structures - Fizz Buzz
- Code prints numbers from 1 to 100
- Replaces multiples of 3 with "fizz"
- Replaces multiples of 5 with "buzz"
- Replaces multiples of 3 and 5 with "fizzbuzz"
- Output matches the example in each case
Functions
foo
andbar
functions are equivalent because they do the same calculation.baz
returns a value, whilequx
prints the result. The functions are not equivalent.- Code assigns value 6 to a and prints to console a,
qux(3)
prints 6 for b to the console myFunc(x)
Code will print an error or fail to complete. It asks for input to assign to x
Data Structures - Lists
SumList
function calculates the sum of elements in a list using a loop.UniqueList
function creates a new list that removes duplicate elements.- Code creates a list of the numbers 0 through 49
Data Structures - Dictionaries
- Code creates a dictionary,
myDict
. - Dictionary keys are numbers 1 through 4.
- Values are twice the key.
- Code prints each key-value pair .
- Bob is incorrect .Output is "1: 2", "2: 4", "3: 6", "4: 8"
Recursion
- Function calculates the initial number of peaches
- Recursively calls itself with one fewer day to determine how many peaches have been accumulated at the previous day.
- Calculates the number of peaches by multiplying the peaches at the previous day.
- This recursive formula can be used to create a closed form solution, determining the initial day's peach count
Objects & Classes
- Class describes a generic animal
- Attributes are
species
,size
,sound
, andisHungry
. __init__
method handles initialization;getSize
,makeNoise
, andfeed
methods handle specific animal actions.
File I/O
'r'
mode opens a file for reading'w'
mode opens a file for writing (truncating the file if it exists)'a'
opens a file for appending
Debugging
try-except
blocks allow handling potential errorstry
block executes the possibly problematic codeexcept
block executes only if the error occurs
Searching
- Two types of search algorithms (sequential, binary search) discussed.
- Binary search is usually faster for sorted lists.
- It will not work on an unsorted list
Sorting
- Merge sort is often faster for larger lists than selection or bubble sort due to more efficient divide and conquer approach.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.