What will be printed?
def main():
value = [99]
show_value(value)
print(value[0])
def show_value(num):
num[0] = 0
main()
0
The following code runs without error and prints "B".
x = 7
if x > 5:
if x < 10:
if x % 2 == 0:
print("A")
else:
print("B")
else:
print("C")
else:
print("D")
B
will this run forever
try:
n = 5 / 0
except Exception:
n = 10
finally:
print(n)
no it ends
What will the following code print?
s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = s1 & s2
s1.add(6)
s3.add(7)
print(len(s1), len(s3))
4 2
free points
100
import sqlite3
conn = sqlite3.connect('class.db')
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT)")
cur.execute("INSERT INTO students (name) VALUES (?)", ("Lily",))
cur.execute("SELECT * FROM students WHERE name = ?", ("Lily",))
print(cur.fetchone())
will this run every time good
ye
What will be the output of the following code?
for i in range(1, 6):
if i % 2 == 0:
continue
for j in range(i):
if j == i - 1:
print(f"Inner: {j}")
Inner: 0
Inner: 2
Inner: 4
What will be the output of the following code?
y = 20
def update():
y = y + 5
print(y)
update()
print(y)
error teehee
WHAT WILL THIS OUTPUT
numbers=[1,2,3,4,5,6]
result = [x * 2 for x in numbers if x % 2 != 0]
print(result)
[2, 6, 10]
what is the symbols for
lists
sets
tuples
dictionaires
[]
{}
[]
{}
def add_numbers(a, b=5):
return a + b
result = add_numbers(3)
print(result)
will this work?
yes
What will this nested loop structure produce?
def pattern(n):
result = []
for i in range(n):
if i % 2 == 0:
inner = []
for j in range(i+1):
if j == 0 or j == i:
inner.append(1)
else:
inner.append(0)
result.append(inner)
return result
print(pattern(4))
[[1], [1, 0, 1]]
text = "banana"
count = 0
for i in range(1, len(text), 2):
if text[i] == "a":
count += 1
print(count)
what is the output
3
What will be the output of this dictionary comprehension?
dictA = {'x': 4, 'y': 1, 'z': 5}
dictB = {val: key for key, val in dictA.items() if val >= 4}
print(dictB)
{4: 'x', 5: 'z'}
does this raise an error?
class Book:
def __init__(self, title):
self.title = title
def update_title(self, title):
title = "New Title"
b1 = Book("Introduction to programming")
b1.update_title("Data science")
print(b1.title)
NO
What is SQL create smth about it rn
random
What will be the output of the following code?
x = 5
y = 0
while x > 0 and y < 10:
if x % 2 == 0:
y += x
else:
y += 1
x -= 1
print(x, y-1)
0 8
What will be the output of the following code?
list1 = [1, 2, 3]
list2 = list1
list1.extend([4, 5])
list2.append(6)
print(list1[-2])
5
What will the following code print?
students_dict = {'Frank': [70, 80], 'Lily': [88, 92]}
for scores in students_dict.values():
scores[1] -= 10
print(students_dict['Frank'][0])
70
text = "banana"
count = 0
for i in range(1, len(text), 2):
if text[i] == "a":
count += 1
print(count)
40 40
create a SQL query that grabs employees making less then 5000, and then orders by salary descending order.
off paper
Consider this function using string methods. What will be printed on the screen?
def validate(text):
for c in text:
if not (c.isalpha() or c.isspace()):
return False
return True
def format_text(template, text):
if not validate(text):
return None
words = text.split()
if len(words) == 0:
return template.strip()
count = 0
for i in range(len(template)):
if template[i:i+2] == "{}":
count += 1
result = template
for i in range(min(count, len(words))):
result = result.replace("{}", words[i], 1)
return result.title()
temp = "Hello {}, welcome to {}"
print(format_text(temp, "john doe python"))
print(format_text(temp, "123 test"))
print(format_text(temp, ""))
def show_value(num):
num[0] = 0
main()
Hello John, Welcome To Doe
None
Hello {}, welcome to {}
Write a Python program to manage the following inventory list for a small business:
products = ["laptop", "mouse", "keyboard", "monitor", "printer"]
Your program should:
check
Write a class CourseManager that:
Has methods to:
Stores courses in a dictionary where:
key is the course name
value is the number of credits
Example usage (You do not need to include this in your code. This section is not part of the points):
manager = CourseManager()
manager.add_course("Introduction to Programming", 3)
manager.add_course("AI for Business", 4)
manager.add_course("Data Science", 2)
print(manager.get_total_credits()) # Should print 9
work through
Using pickle, what is wrong with this code?
import pickle
class MyClass:
def __init__(self):
self.data = [1,2,3]
obj = MyClass()
with open('data.pkl', 'w') as f:
pickle.dump(obj, f)
WB