Lists
Functions
If Statements
Loops
Basics
100

How do we create a list in Python? Provide an example.

x = [2, "hi"] or any other list is fine

100

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.

100

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.

100

What are the two types of loops?

While loop and For loop

100

How would we print "Hello World" in Python?

print("Hello World")
200

What would this code print out?

numbers = ['zero', 'one', 'two']

numbers[1]

'one'
200

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):

200

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.

200

What type of loop in Python is most commonly used to iterate over a sequence (e.g., a list or string)?

For loop

200

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.

300

What would this code return?

numbers = ['zero', 'one', 'two']
numbers[0] = 'zilch'
print(numbers)

['zilch', 'one', 'two']

300

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).

300

How can we check is variable a is equal to variable b using if statements?

if a == b:

300

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])


300

What data type is used to represent whole numbers in Python?

Integers

400

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!")

400

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.

400

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.

400

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!")

400

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())

500

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]
500

Write a function that calculates the area of a triangle given the inputs base and height

def area(h, b):
    return h * b / 2


500

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!")

500

What are the purposes and effects of the 'break' and 'continue' keywords in Python?

  • The break keyword is used to exit a loop prematurely. When encountered, it terminates the loop and continues with the code that follows the loop.
  • The continue keyword, on the other hand, is used to skip the current iteration of a loop and move to the next one. It does not exit the loop but simply bypasses the remaining code within the current iteration.
500

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}.")