How do we create a list in Python? Provide an example.
x = [2, "hi"] or any other list is fine
What is a function in Python, and why is it useful?
A function is a reusable block of code that performs a specific task. Functions are useful for organizing code and making it easier to read and maintain.
What is the primary purpose of an if statement in Python?
The primary purpose of an if statement is to perform conditional execution of code based on a given condition.
What are the two types of loops?
While loop and For loop
How would we print "Hello World" in Python?
What would this code print out?
numbers = ['zero', 'one', 'two']
numbers[1]
How do you define a function in Python?
Functions are defined using the def keyword, followed by the function name and parameters. For example: def my_function(parameter1, parameter2):
What is the purpose of the else clause in an if...else statement, and when is it executed?
The else clause provides an alternative code block to execute when the if condition is not met. It is executed when the if condition is False.
What type of loop in Python is most commonly used to iterate over a sequence (e.g., a list or string)?
For loop
How do you assign a value to a variable in Python?
You use the assignment operator (=). For example, x = 10 assigns the value 10 to the variable x.
What would this code return?
numbers = ['zero', 'one', 'two']
numbers[0] = 'zilch'
print(numbers)
['zilch', 'one', 'two']
How can you call (run) a function in Python?
You can call a function by using its name followed by parentheses and providing any required arguments. For example: my_function(arg1, arg2).
How can we check is variable a is equal to variable b using if statements?
if a == b:
How can we use a loop to iterate over every element in this list: x = [3, 6, 1, 8, 2]
for element in x: print(element)
OR
for i in range(len(x)): print(x[i])
What data type is used to represent whole numbers in Python?
Integers
How can you check if a specific item is in a list?
You can use the in operator to check if an item is in a list.
e.g, if "x" in list1: print("x is in the list!")
What is the purpose of a function's "return" statement in Python?
The return statement is used to send a value back to the calling code. It specifies the function's output or result.
What do the logical operators and, or, not do in Python?
When does "and" return true?
When does "or" return true?
When does "not" return true?
The and operator returns True if both operands are True.
The or operator returns True if at least one of the operands is True.
The not operator returns the opposite of the operand's boolean value. If the operand is True, not returns False, and vice versa.
Using a while loop write code where we count up starting from 1 and going up until 10, and then print Blastoff!
count = 1
while count != 11:
print(count)
count += 1
print("Blastoff!")
Input() always returns the user input as a string.
How can you convert user input to an integer in Python, assuming the user enters a valid integer?
Use the int() function to convert the input to an integer: int(input())
How can you reverse the order of a list in Python?
To reverse a list in-place, you can use the reverse() method. To create a new reversed list, you can use slicing with a step of -1.
newlist = oldlist[::-1]Write a function that calculates the area of a triangle given the inputs base and height
def area(h, b):
return h * b / 2
Write a program which reads an integer from input using pancakes = int(input()). If pancakes is more than 3, print out Yum! and if pancakes is not more than 3, print out Still hungry! instead.
pancakes = int(input())
if pancakes > 3:
print("Yum!")
else:
print("Still hungry!")
What are the purposes and effects of the 'break' and 'continue' keywords in Python?
Write a code that asks the user for their name and their age. Print a message greeting them
and telling them in which year they will turn 100.
name = input("What is your name? ")
age = int(input("How old are you? "))
turn100 = 2023 + (100 - age)
print(f"Hello, {name}! You will turn 100 in the year {turn100}.")