0% found this document useful (0 votes)
8 views

Class 8_Python Assignment-1 1

Uploaded by

Upasana Pandita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Class 8_Python Assignment-1 1

Uploaded by

Upasana Pandita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Delhi Public School Ghaziabad

Vasundhara
Subject – ICT
Grade – VIII
Iterative statements -Assignment 1
1. Which keyword is used to create a loop in Python that runs a specific number
of times?
a) while
b) for
c) loop
d) repeat

2. What does the following code do?


for letter in "Python":
print(letter)
a) Prints the word "Python" as a whole
b) Prints each character in "Python" on a new line
c) Throws an error
d) Repeats the word "Python"

3. What will be the output of the following code?


fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Yes")
a) Yes
b) No
c) Error
d) banana

4. What is the output of this code?


count = 0
while count < 3:
print(count)
count += 1
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Infinite loop

5. Which of the following statements is correct for not in?


a) Returns True if the item exists in the sequence
b) Returns True if the item does not exist in the sequence
c) Used to add an item to the sequence
d) None of the above

6. What is the correct syntax for a for loop?


a) for i in range(5):
b) for (i=0; i<5; i++):
c) for (i : 5)
d) for range(5):

Fill in the Blanks (5 Marks)


1. A ________ loop is used when the number of iterations is unknown.
2. The ________ operator checks for membership in a sequence.
3. A for loop iterates over a sequence in the order of its ________.
4. The ________ keyword is used to stop a loop prematurely.
5. To iterate through a list named items, we use the syntax ________ item in
items.

Numerical Problems (10 Marks)


1. Write a Python program using a for loop to print the first 10 even numbers.
2. Use a while loop to calculate the sum of all numbers from 1 to 50.
3. Write a program to check if a given character exists in a string using the in
operator.
Example: Input string: "programming", Character: "g" → Output: "Character
exists."
4. Write a program to count how many vowels are present in a given string
using a for loop and the in operator.
Python Programs for Numerical Problems
1. Program to print the first 10 even numbers using a for loop
print("The first 10 even numbers are:")
for i in range(2, 21, 2): # Start from 2, go up to 20, increment by 2
print(i)
2. Program to calculate the sum of all numbers from 1 to 50 using a
while loop
total = 0
num = 1
while num <= 50:
total += num
num += 1
print("The sum of all numbers from 1 to 50 is:", total)
3. Program to check if a given character exists in a string using the in
operator
input_string = input("Enter a string: ")
char = input("Enter a character to search for: ")
if char in input_string:
print("Character exists.")
else:
print("Character does not exist.")
4. Program to count the number of vowels in a string using a for loop
and in operator
input_string = input("Enter a string: ").lower() # Convert to lowercase for
uniformity
vowels = "aeiou"
vowel_count = 0
for char in input_string:
if char in vowels:
vowel_count += 1
print("The number of vowels in the string is:", vowel_count)

You might also like