What method would you use to add an item to the end of a list?
append()
What is a tuple in Python?
immutable sequence of items
What keyword is used to define a function in Python?
def
Write a simple for loop that prints numbers from 1 to 5.
for x in range (1,6):
print (x)
Which data type is used to store multiple items in a single variable?
list or tuple
What function would you use to find the largest number in a list?
max()
How do you create a tuple with one item?
tuple_name = (item,)
How do you call a function named my_function?
my_function()
Write a simple while loop that prints numbers from 1 to 5.
i = 1
while i <= 5:
print(i)
i += 1
What will be the output of len((10, 0, 3))
3
How do you loop through a list to print each item?
for loop
Can you change the values inside a tuple after it is created?
No, tuples are immutable.
What is the purpose of the return statement in a function?
It sends back a value from the function.
Write a simple for loop that prints numbers from 10 to 1.
for x in range (10, 0, -1):
print(x)
What is the difference between lists and tuples in Python?
Lists are mutable; tuples are immutable
What method would you use to remove an item from a specific index in a list?
pop(index)
How do you access the first item of a tuple called my_tuple
my_tuple[0]
Write the syntax to create a function that returns the area of a rectangle.
def area(a, b):
return a * b
What is the output of the following code:
for i in range(100, 30, -20):
print(i)
100, 80, 60, 40
How do you define a function that takes two parameters: x,y?
def function_name(x,y):
Write a one-line code to calculate the sum of all numbers in a list called my_list.
sum(my_list)
What function can you use to convert a list to a tuple?
tuple(list)
What is the output of the following code:
def triangle(base, h):
area=1/2*base*h
return area
print(triangle(10,7))
35.0
What is the output of the following code:
x=100
sum=0
while x>10:
sum+=1
x-=20
print(sum)
5
Write a code to create a list of random 10 numbers.
import random
list=[]
for i in range(10):
list.append(random.randint(1,100))
print(list)