What 2 types of logic types do while loops combine?
Iterative logic (repeating) and conditional/Boolean (always checks a condition)
In the following example, what is the value of x?
for x in lis:
print(x)
x - iterable item, will be every item in the list on a different loop repetition
What syntax denotes an index?
[]
What does SQL stand for?
Structured Query Language
What symbols denote integer and float division?
/ integer division
// float division
What is wrong with the following code?
x = 10
y = 1
while x > y:
print(y)
x = x + 1
X only gets larger and will never be less than y, resulting in an infinite loop.
What is the difference between 1) for i in range(len(lis)) and 2) for i in lis?
1) Accesses numbers 0-length of the list
2) Accesses each value in the list
[start:end]
Start is inclusive and not required, end value is non-inclusive and must always be specified
What 3 things must be done before you can execute any commands in SQL?
1) import mysqlite3
2) establish connection
3) establish cursor
What would the output be of the following?
lst = [1, 2, 3]
print(1 in lst)
True
def even(letters):
letters = ""
while ____:
def letter_length():
word = ""
while word != 'stop':
word = input("Enter a word: ")
print(len(word))
letter_length()
Write a for loop that asks the user to enter an integer number 10 times. Every time they enter a number, print 'hello world' as many times as they number they entered (ex: 3 prints 'hello world hello world hello world')
for i range(10):
num = int(input("Enter number: "))
print("hello world " * num)
Change "clown fish" in this list to "gorilla":
lst = ["Chimp", "Ape", "Clown fish", "Monkey"]
lst[2] = "Gorilla"
How do you format the connection and cursor in SQL?
conn = sqlite3.connect("FILE NAME")
curs = conn.cursor()
Write a program that checks if "dog" is in the following list:
pets = ['cat', 'hamster', 'dog', 'bird', 'guinea pig']
pets = ['cat', 'hamster', 'dog', 'bird', 'guinea pig']
if 'dog' in pets:
print("Yes, a dog is a pet")
Write a while loop that changes each value in lis to half of the previous value (rounded). (Hint: how does this differ to a for loop?)
i = 0
while i in range(len(lis)):
if i % 2 == 1:
lis[i] = lis[i-1] // 2
i +=1
Write a for loop that goes through a list of numbers lis and prints whether the number is even or odd.
for i in lis:
if i % 2 == 0:
print(i, "is even")
else:
print(i, "is odd")
Write the indexes for the following:
a) last item in list
2) everything up to index 5 (inclusive)
3) everything after index 5 (inclusive)
a) [-1]
b) [:6]
c) [5:]
When retrieving data using SQL, how do we format the .execute()?
Hint: what are all of the keywords we have and how are they formatted?
curs.execute("FROM [Table] SELECT [Column] WHERE [conditions] SORT BY [ASC or DESC]")
What is the difference between return and print()? Answer must include a) the format/syntax of each b) where values are sent.
Hint: think of what you do with multiple variables
return __
-sends things (variables, strings, etc.,) to the function when it's called
print(__)
-sends the same types of things to the terminal, where you/the user can see the message
Write a while loop that asks the user to enter an integer number x until they enter a negative number. First determine whether lis has at least x values. If it doesn't, tell the user to pick a lower number. If they enter a negative number, print a goodbye message. Otherwise, print the item with index x and the prior and following values.
x = 0
while x > 0:
x = int(input("Enter a number: "))
if len(lis) < x:
print("pick another number")
elif x < 0:
print("Bye!")
else:
print(lis[(x-1):lis[x+2])
Write a for loop for the following function that takes values from the list kilometers and converts each speed to miles. Using the same indexes, store the converted values in miles and round them.
def k_to_m(kilometers):
miles = [0] * len(kilometers)
__________:
___________
print(miles)
k_to_m([30, 50, 100, 150])
def k_to_m(kilometers):
miles = [0] * len(kilometers)
for i in range(len(kilometers)):
miles[i] = round(kilometers[i] / 1.609, 2)
print(miles)
k_to_m([30, 50, 100, 150])
Use a for loop and indexing to return the index number for each value in lis that are even.
for x in range(len(lis)):
if lis[x] % 2 == 0:
return x
Let's say we want to access a file called "cars.db" with a table called used_cars and print the name, year, and price of all cars that are color=blue. Print in descending order.
import sqlite3
conn = sqlite3.connect("cars.db")
curs = conn.cursor()
cars = curs.execute("FROM used_cars SELECT name, year, price WHERE color='blue' ORDER BY DESC")
What's wrong with the following code?
def sum_evensum(nums):
total = 0
even_total = 0
for n in nums:
total += 1
if n % 2 == 0:
even_total += 1
return even_total
return total
sum_evensum(nums)
If the number is even, the function will end due to the return and it won't go through the rest of the list