What is the purpose of the while loop in Python?
To execute a block of code as long as a certain condition is true
What will be the output of the following code?
for i in range(3):
print(i)
0 1 2
Which method is used to remove an element from a list by its value?
remove()
What will the following code print?
i = 1
while i < 4:
print(i)
i += 1
The output will be:
1
2
3
Explain how the range() function works in a for loop. Provide an example.
The range() function generates a sequence of numbers, which can be used in a for loop to iterate a specific number of times.
for i in range(1, 10, 2):
print(i)
What is the primary purpose of the extend() method?
Adds multiple elements to the end of a list
What is the correct way to avoid an infinite loop in the following code?
x = 10
while x > 0:
print(x)
Add x = x - 1 inside the loop
(Reason: Decreasing x ensures the loop terminates when x reaches 0.)
Write a Python program that uses a for loop to print the squares of the numbers from 1 to 10.
for i in range(1, 11):
print(i ** 2)
What does the append() method do in a list?
Adds a new item to the end of the list
What is the difference between using break and continue in a while loop?
What will the following code output?
total = 0
for i in range(5):
total += i
print(total)
10
What is the purpose of the elif statement?
To add another condition in an if-else structure
Who created the Python programming language
Python was created by Guido van Rossum in the late 1980s
When the 25th anniversary of SDU University was celebrated
2021
What was the route of the Titanic's first trip?
The Titanic's first trip was from Southampton in England to New York City in the USA.