Name the 5 different types of variables in Python.
Float, Int, Boolean, String, NoneType
Write code to print the following using a loop:
1
2
3
4
5
6
x=0
while x<=6:
print(x)
x+=1
OR
for x in range(1,7):
print(x)
What are the elements needed for a function? You can write an example function with the correct structure if that helps.
Definition, name, inputs, docstrings, body, return(or print)
def addition(x,y):
" Input: two numbers
Output: sum of the two numbers
"
return x+y
LISTS
Use the list:
s = [a,b,c,d,e,f,g,1,2,3,4,5]
What is:
s[1]:
s[4]:
s[-2]:
s[0]:
Solution:
s[1]: b
s[4]: e
s[-2]: 4
s[0]: a
Why won´t this code run?
x=random.randint(1,10)
print(x*2)
Need to import random!
import random
x=random.randint(1,10)
print(x*2)
x=input("Enter a number: ")
What type is x?
String
Write code to print the following while using a loop:
7
6
5
4
3
2
1
for x in range(7,0,-1):
print(x)
OR
x=7
while x>=1:
print(x)
x=x-1
What is the difference between return and print?
Return: ends the function, shows the user information and saves this information
Print: shows the user information
a=[1,2,3,4,5,6]
Delete the first item of the list.
Then add 7 to the end of the list.
Print the new list.
Result should be:
a=[2,3,4,5,6,7]
a=[1,2,3,4,5,6]
a.remove(1)
a.append(7)
print(a)
Output: a=[2,3,4,5,6,7]
Find two ways to print the numbers 0,4,8,12,16.
x=0
while x<=16:
print(x)
x+=4
OR
for x in range(0,17,4):
print(x)
What will this print?
"Ray"=="ray"
False;
strings have to be exactly the same to be equal
"ray"=="ray"
"Ray"=="Ray"
Iterate through all the numbers 1 to 20 and print the odd numbers.
for number in range(1,21):
if number%2!=0:
print(number)
OR
for number in range(1,21,2):
print(number)
What does this print?
def funfunction(x,y):
z = x + y
return z + x + y
print(funfunction(23,8))
62
STRINGS
Take your team name as a string and print each letter in the name. Then print "is going to win!" at the end.
teamname = "MIT Students"
for letter in teamname:
print(letter)
print("is going to win!")
What is wrong with this code?
def add_three(x):
x+3
print(add_three(7))
Need a return or print!
def add_three(x):
return x+3
print(add_three(7))
OR
def add_three(x):
print(x+3)
add_three(7)
a=8/4
b=7//2
What are the types of a and b?
a is....
b is....
A is a float (2.0)
B is an integer (3)
Iterate through the numbers 1 to 25 and break once you reach a number that can be divided by both 2 and 5.
for number in range(1,26):
print(number)
if number%2==0 and number%5==0:
break
Write a function that counts the number of vowels in a word and returns that number.
Vowels are a,e,i,o,u.
For example:
"apple" should return 2
"jeopardy" should return 3
Your code should work with both examples.
def vowels(word):
vowel_list=[¨a¨,¨e¨,¨i¨,¨o¨,¨u¨]
count=0
for letter in word:
for item in vowel_list:
if letter==item:
count+=1
return count
print(vowels("apple"))
What is printed?
x = ["I", "like", 5, "Ray", "Python", True]
x.pop()
x.remove(x[2])
x.remove(x[-2])
z = "_".join(x)
print(z)
I_like_Python
We are trying to print a list of odd numbers 1 to 10 and a list of even numbers 1 to 10. What is wrong with this code?
odd=[]
even=[]
while x<=10:
if x%2==0:
even+=[x]
else:
odd.append(x)
print(odd)
print(even)
Solution: need x+=1
odd=[]
even=[]
while x<=10:
x+=1
if x%2==0:
even+=[x]
else:
odd.append(x)
print(odd)
print(even)
Ask the user to input two numbers.
Add the numbers together.
Print the sum as an integer. Then print the sum as a float.
x = int(input("Number 1: "))
y = int(input("Number 2: "))
print(x+y)
print(float(x+y))
Use a double for loop to print the letters that are in both of two different words:
Your code must be able to print:
p
y
if your words are:
word1="python"
word2="spyder"
and should print:
r
y
word1="courtney"
word2="ray"
Show us that your code works for both examples.
word1="python"
word2="spyder"
for letter_a in word1:
for letter_b in word2:
if letter_a==letter_b:
print(letter_a)
What is printed?
def function_a(z):
return z*21
def function_b(w):
return w%3
def function_c(x,y):
return x + y
print(function_a(function_b(function_c(5,6))))
42
What will this print?
x = [[1, 2, 3], 2, [3, 2], [1]]
print(x[0][2] + x[1] + x[2][1])
7
We are trying to count the number of times 2 is after 1.
i.e. if the string is “17129”, the code should print “count is 1”
What is wrong with this code though?
s = "12341234"
num_count=0
for index in range(len(s)-1):
if s[index]=="12":
num_count+=1
print(num_count)
Solution: need [index:index+2]
s = "12341234"
num_count=0
for index in range(len(s)-1):
if s[index:index+2]=="12":
num_count+=1
print(num_count)