The type of loop that allows me to control the number of times I enter it directly.
For Loop
ListA = [1,2,3,4]
The value of ListA[-2]:
3
Three parts of an IF statement
What are IF, Else, Elif
The number of different types of quotation marks a string can have to define it as one
what is 2
The three letter word that defines a function
What is def
Question 2: The type of loop that allows a conditional statement to control it.
While loop
Guess the output
numbers = [1, 2, 3, 4, 5]
squared = [num ** 2 for num in numbers]
print(squared)
What is [1, 4, 9, 16, 25]
#Guess the Output
x = 15
if x < 10:
print("A")
elif x < 20:
print("B")
elif x < 30:
print("C")
else:
print("D")
What is B
import string
#Guess the output
def reverse_words(input_string):
words = input_string.split()
reversed_string = " ".join(reversed(words))
return reversed_string
input_str = "Python is fun"
output_str = reverse_words(input_str)
print(output_str)
fun is Python
#The code below gives an output of:
def up(x,y):
x+=x
return x + y
print(up(1,2))
What is 4
Output of
for i in range(5,10): print (i)
What is 5,6,7,8,9
Given this list: grades = [[85, 92, 88], [78, 86, 90], [90, 95, 88]]
Find the average of each list.
for student_grades in grades:
total_grade = sum(student_grades)
average_grade = total_grade / len(student_grades)
print(average_grade)
The output of this statement :
X = True, Y = False
if (not (X or Y) or Y) : print(1)
else: print (2)
What is 2
The letters i,j that create the split in this word from "Coding is fun!" to ”is”
word[i:j] = "Coding is fun!"
What is 7,9
def A():
return "apple"
def B():
s = A().replace("a","gra")
return s
print(B())
What is grapple.
for i in range(1, 4):
for j in range(1, 4):
print(i * j, end=" ")
print()
1 2 3
2 4 6
3 6 9
Create a function to calculate the sum of all the elements in a 2D list. For instance, given [[1, 2], [3, 4], [5, 6]], the function should return 21.
def sum_2d_list(matrix):
total = 0
for row in matrix:
for element in row:
total += element
return total
#Guess the Output
a = 7
if a > 5:
if a > 10:
print("A")
else:
print("B")
else:
print("C")
What is B
The function add to this word to take it from "###hi###" to "hi"
What is word.strip('#')
The the function to check if a word is a palindrome and returns true or false
def pali(word):
if(word==word[::-1]): return True
return False
—-----------------------
Or you can do just
return (word==word[::-1])
Write a Python program to count the number of even and odd numbers from a list
#Guess the output
def count_even_odd(numbers):
even_count = 0
odd_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
return even_count, odd_count
The code that creates a matrix in 6 lines of code:
matrix = []
for i in range(rows):
row = []
for j in range(cols):
row.append(0)
matrix.append(row)
Write a Python program that uses conditional statements to provide a weather description. If the temperature is greater than 30, print "It's hot outside." If it's between 10 and 30 (inclusive), print "It's warm outside." Otherwise, print "It's cold outside."
temperature = int(input("Enter the temperature: "))
if temperature > 30:
print("It's hot outside.")
elif 10 <= temperature <= 30:
print("It's warm outside.")
else:
print("It's cold outside.")
The code to take this string " Hi! my! name! is! python! " to "Hi my name is python"
Use: Split and Join
string = " Hi! my! name! is! python! "
string_list = string. split()
for i in range(len(string_list)): string_list[i] = string_list[i].strip('!')
string = " ".join(string_list)
print(string)
write a function, find_duplicates, that takes a list and returns a new list containing only the duplicate elements.
def find_duplicates(input_list):
duplicate_list = []
for i in range(len(input_list)):
for j in range(i + 1, len(input_list)):
if input_list[i] == input_list[j] and input_list[i] not in duplicate_list:
duplicate_list.append(input_list[i])
return duplicate_list