SG CLASS
COMPUTER
Python Basics – Questions and Answers
1. What is a list?
A list in Python is a collection of items that can be stored together in a single variable. The
items in a list can be numbers, words, or even a mix of both. Lists are written using square
brackets [ ], and the items are separated by commas.
2. What is a loop?
A loop is a way to repeat a set of instructions or code multiple times. In Python, loops help
us do the same task again and again without writing the code many times. There are two
main types of loops in Python: for loop and while loop.
3. Give an example of a list.
Here is an example of a list in Python:
fruits = ["apple", "banana", "mango", "orange"]
4. Write code to print all items in the list using a loop.
Here is a simple code to print all the items in the fruits list using a for loop:
fruits = ["apple", "banana", "mango", "orange"]
for fruit in fruits:
print(fruit)
5. What is indexing?
Indexing means finding or accessing an item in a list by its position (index number). In
Python, the first item has index 0, the second has index 1, and so on.
Example:
fruits = ["apple", "banana", "mango", "orange"]
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: mango
6. What is slicing?
Slicing means taking a part (a slice) of the list. We use a colon : inside the square brackets to
show where to start and where to stop.
Example:
fruits = ["apple", "banana", "mango", "orange", "grape"]
print(fruits[1:4]) # Output: ['banana', 'mango', 'orange']
7. What is append?
The append method is used to add a new item at the end of a list.
Example:
fruits = ["apple", "banana", "mango"]
[Link]("orange")
print(fruits) # Output: ['apple', 'banana', 'mango', 'orange']
8. What is a for loop?
A for loop is used to repeat a block of code a certain number of times. It is usually used
when you know how many times you want to repeat something, or when you want to go
through each item in a list.
Example:
# Print numbers from 1 to 5
for i in range(1, 6):
print(i)
9. What is a while loop?
A while loop is used to repeat a block of code as long as a condition is true. It is useful when
you do not know in advance how many times you need to repeat the code.
Example:
# Print numbers from 1 to 5
i=1
while i <= 5:
print(i)
i=i+1
BOOK QUESTION ANSWER :
1. The for loop is a condition-controlled loop.
Answer: F
2. Loops are used to execute the instructions several times.
Answer: T
3. The continue statement is used to terminate the loop.
Answer: F
4. The while loop is a condition-controlled loop.
Answer: T
________________________________________
B. Fill in the blanks.
1. ________ loop is used when we do not know how many times the loop will execute.
Answer: while
2. A ________ loop executes the loop body till a condition is true.
Answer: while
3. The ________ function in Python is used to generate a sequence of numbers.
Answer: range
4. The ________ statement is used to exit a loop in between based on a condition.
Answer: break
________________________________________
C. Multiple Choice Questions
[Link] one of the following is a valid Python while statement?
Answer: (b) while(n>=2):
[Link] of the following is not a loop?
Answer: (c) break
[Link] of the following is used to terminate the loop?
Answer: (b) break
[Link] of the following is a counter-controlled loop?
Answer: (a) for
D. Short Answer Questions
1. What is a loop?
A loop is a programming structure that repeats a set of instructions until a certain condition is met
or for a specific number of times.
2. What is counter controlled loop?
A counter-controlled loop repeats a set of instructions a fixed number of times, using a counter
variable to keep track of the number of repetitions. The for loop is an example.
3. Why continue statement is used in a program?
The continue statement is used to skip the rest of the code inside the loop for the current iteration
and move to the next iteration of the loop.
4. What is the significance of a break statement?
The break statement is used to exit a loop immediately when a certain condition is met, even if
the loop has not finished all its iterations.
E. Long Answer Questions
1. What are loops? Explain its types.
Loops are programming structures that allow us to repeat a set of instructions multiple times.
• Counter-controlled loop: Repeats a fixed number of times (e.g., for loop).
• Condition-controlled loop: Repeats as long as a condition is true (e.g., while loop).
2. Explain the range() function using an example.
The range() function generates a sequence of numbers.
for i in range(1, 6):
print(i)
3. What is the difference between a ‘while’ and a ‘for’ loop?
• A for loop is used when the number of repetitions is known.
• A while loop is used when the number of repetitions is not known and depends on a condition.
2. What is the use of a break statement in a Python program?
The break statement is used to exit a loop immediately when a certain condition is met, stopping
further iterations.
4. Find the output of the following code:
Colours = ['yellow', 'Red', 'Blue', 'White']
[Link](1)
[Link]()
print(Colours)
Output:
['yellow', 'Blue']
5. Python code:
n=5
i=1
while i < n:
print(i * 1)
if i == 4:
break
i += 1
Output:
1
2
3
4
6. python code:
items = ['Pencil', 'Pen', 'Scale']
for i in items:
if i == 'Scale':
continue
print(i)
output:
Pencil
Pen