WORKSHEET - GRADE 11
(FLOW OF CONTROL & STRING MANIPULATION)
I. Answer the following questions using both for and while loops wherever specified.
1.Write a Python program to calculate the sum of all integers from 1 to 10:
a) Using a for loop
b) Using a while loop
2.Write a Python program to print the multiplication table of 5:
a) Using a for loop
b) Using a while loop
3.Write a Python program to print all even numbers between 1 and 20:
a) Using a for loop
b) Using a while loop
4.Write a Python program to reverse the digits of a number entered by the user:
a) Using a for loop
b) Using a while loop
(Example: Input = 123, Output = 321)
5. Write a Python program to print your name 10 times using the following loops:
a) for loop
b) while loop
II. Answer the following questions using Python’s if and if-else statements.
1.Write a Python program to check whether a number entered by the user is even or odd using an
if-else statement.
2.Write a Python program to check if a number entered by the user is positive, negative, or zero
using if-else statements.
3.Write a Python program that takes two numbers from the user and prints the larger number
using an if-else statement.
4.Write a Python program to check whether a number entered by the user is divisible by 5 using
an if statement.
5.Write a Python program that takes marks as input and assigns a grade based on the following
criteria:
• Marks >= 90 → Grade A
• Marks >= 80 → Grade B
• Marks >= 70 → Grade C
• Marks >= 60 → Grade D
• Marks < 60 → Grade F`
Use if-Elif-else statements to implement the grading system.
III. Write the output of the following code:
1) str = "String Slicing in Python"
a) print (str [13: 18])
b) print (str[-2 : -4: -2])
c) print (str [12:18:2])
d) print (str[-17 : -1 : 1])
e) print (str[-6: -20: -2])
f) print (str[0: 9 : 3])
g) print (str[19:29])
h) print (str[-6 : -9 : -3])
i) print (str[-9: -0: -1])
j) print(str[2: 16 : 3])
2. str = "Welcome to my blog"
a. print(str [3 : 18]
b. print (str[ 2:14: 2])
c. print(str [7])
d. print(str[8: -1: 1])
e. print(str [-9 : -15])
f. print(str[0: 9:3])
g. print(str[9:29:2])
h. print(str[-6: -9: -3])
i. print(str[-9: -9: -1])
j. print(str[8:25:3])
IV. Perform concatenation and replication with the following strings and predict the
output:
1. str1 = "Hello"
str2 = "World"
a. print (str1 + " " + str2)
b. print ((str1 + "! ") * 3)
2. str1 = "Welcome"
str2 = "Back"
a. Combine the first 3 characters of str1 with the last 2 characters of str2.
b. Reverse str1 and concatenate it with str2.
c. Concatenate str1 with a space in between and replicate the result 2 times.
V. Write a program in Python to print the following pattern:
a) 1 b) 54321
12 5432
123 543
1234 54
12345 5
c) 55555 d) 12345
4444 1234
333 123
22 12
1 1
VI. Given the sentence:
1. sentence = "Python programming is fun and educational"
Write the output of the following code snippets. For each code snippet, write the correct output
below:
a) print(sentence.upper())
b) print(sentence.lower())
c) print(sentence.split())
d) print(sentence.find("fun"))
e) print(sentence.replace("Python", "Java"))
f) print(sentence.title())
g) print(sentence.count("a"))
h) print(sentence.startswith("Python"))
i) print(sentence.endswith("educational"))
j) print(sentence.swapcase())
k) print(sentence.isalpha())
l) print(len(sentence))
2. sentence = "Learning Python is exciting and rewarding"
Write the output of the following code snippets. For each code snippet, write the correct
output below:
a) print(sentence.upper())
b) print(sentence.lower())
c) print(sentence.split())
d) print(sentence.find("exciting"))
e) print(sentence.replace("Python", "Java"))
f) print(sentence.title())
g) print(sentence.count("e"))
h) print(sentence.startswith("Learning"))
i) print(sentence.endswith("rewarding"))
j) print(sentence.swapcase())
k) print(sentence.isalpha())
l) print(len(sentence))
3. sentence = " Python programming is fun and exciting "
Write the output of the following code snippets. For each code snippet, write the correct
output below:
a) print(sentence.lstrip())
b) print(sentence.rstrip())
c) print(sentence.strip())
d) print(sentence.islower())
e) print(sentence.isupper())
f) print(sentence.join(["Learning", "Python", "is", "fun"]))
g) print(sentence.startswith("Python"))
h) print(sentence.endswith("exciting"))
i) print(sentence.count("o"))
j) print(" ".join(["Python", "is", "fun"]))
1. Sum of Numbers (1 to 10)
Using for Loop:
total = 0
for i in range(1, 11):
total += i
print("Sum of numbers from 1 to 10 (using for loop):", total)
Using while Loop:
total = 0
i=1
while i <= 10:
total += i
i += 1
print("Sum of numbers from 1 to 10 (using while loop):", total)
2. Multiplication Table of 5
Using for Loop:
num = 5
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Using while Loop:
num = 5
i=1
while i <= 10:
print(f"{num} x {i} = {num * i}")
i += 1
3. Even Numbers Between 1 and 20
Using for Loop:
for i in range(1, 21):
if i % 2 == 0:
print(i, end=" ")
print() # For newline
Using while Loop:
i=1
while i <= 20:
if i % 2 == 0:
print(i, end=" ")
i += 1
print() # For newline
4. Reverse Digits of a Number
Using for Loop:
num = int(input("Enter a number: "))
reverse = 0
for digit in str(num)[::-1]:
reverse = reverse * 10 + int(digit)
print("Reversed number (using for loop):", reverse)
Using while Loop:
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num //= 10
print("Reversed number (using while loop):", reverse)
1. Check whether a number is even or odd:
# Program to check even or odd
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
2. Check if a number is positive, negative, or zero:
# Program to check if a number is positive, negative, or zero
number = float(input("Enter a number: "))
if number > 0:
print(f"{number} is positive.")
elif number < 0:
print(f"{number} is negative.")
else:
print(f"{number} is zero.")
3. Find the larger of two numbers:
# Program to find the larger number
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num1 > num2:
print(f"The larger number is {num1}.")
else:
print(f"The larger number is {num2}.")
4. Check divisibility by 5:
# Program to check divisibility by 5
number = int(input("Enter a number: "))
if number % 5 == 0:
print(f"{number} is divisible by 5.")
else:
print(f"{number} is not divisible by 5.")
5. Assign grades based on marks:
# Program to assign grades based on marks
marks = float(input("Enter marks: "))
if marks >= 90:
grade = "A"
elif marks >= 80:
grade = "B"
elif marks >= 70:
grade = "C"
elif marks >= 60:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}")
III. Write the output of the following code:
a) come to my blog
b) loet y
c) Welcome
d) to my blo
e) No Output
f) Wce
g) om lg
h) y
i) No Output
j) tmbg
2. a) g in
b) o
c) nn
d) Slicing in Pytho
e) Pn ncl
f) Si
g) ython
h) P
i) i gnicils gnirt
j) rgli
IV.
1. str1 = "Hello", str2 = "World"
a) print(str1 + " " + str2)
Output: "Hello World"
(Concatenates "Hello" with a space and "World")
b) print((str1 + "! ") * 3)
Output: "Hello! Hello! Hello! "
(Concatenates "Hello! " 3 times)
2. str1 = "Welcome", str2 = "Back"
a) Combine the first 3 characters of str1 with the last 2 characters of str2:
print(str1[:3] + str2[-2:])
Output: "Welck"
(First 3 characters of "Welcome" are "Wel", and last 2 characters of "Back" are "ck")
b) Reverse str1 and concatenate it with str2:
print(str1[::-1] + str2)
Output: "emocleWBack"
(Reverses "Welcome" to "emocleW" and concatenates it with "Back")
c) Concatenate str1 with a space in between and replicate the result 2 times:
print((str1 + " ") * 2)
Output: "Welcome Welcome "
(Concatenates "Welcome" with a space, then repeats the result 2 times)
V. 1.
Ans.
for i in range(1, 6):
for j in range(1, i+1):
print(j, end = " ")
print()
2. Ans.
for i in range(5,0,-1):
for j in range(5, 5-i,-1):
print(j, end = " ")
print()
3. Ans.
for i in range(5, 0, -1):
for j in range(i):
print(i, end = " ")
print()
4.
for i in range(6,0,-1):
for j in range(1, i):
print(j, end = " ")
print()
a) print(sentence.upper())
Output: "PYTHON PROGRAMMING IS FUN AND EDUCATIONAL"
b) print(sentence.lower())
Output: "python programming is fun and educational"
c) print(sentence.split())
Output: ['Python', 'programming', 'is', 'fun', 'and', 'educational']
d) print(sentence.find("fun"))
Output: 26 (The index of the first occurrence of "fun")
e) print(sentence.replace("Python", "Java"))
Output: "Java programming is fun and educational"
f) print(sentence.title())
Output: "Python Programming Is Fun And Educational"
g) print(sentence.count("a"))
Output: 4 (The number of times "a" appears in the sentence)
h) print(sentence.startswith("Python"))
Output: True
i) print(sentence.endswith("educational"))
Output: True
j) print(sentence.swapcase())
Output: "pYTHON PROGRAMMING IS FUN AND EDUCATIONAL"
k) print(sentence.isalpha())
Output: False (The sentence contains spaces, which are not alphabetic)
l) print(len(sentence))
Output: 43 (The total number of characters in the sentence)
2.
a) print(sentence.upper())
Output: "LEARNING PYTHON IS EXCITING AND REWARDING"
b) print(sentence.lower())
Output: "learning python is exciting and rewarding"
c) print(sentence.split())
Output: ['Learning', 'Python', 'is', 'exciting', 'and', 'rewarding']
d) print(sentence.find("exciting"))
Output: 23 (The index of the first occurrence of "exciting")
e) print(sentence.replace("Python", "Java"))
Output: "Learning Java is exciting and rewarding"
f) print(sentence.title())
Output: "Learning Python Is Exciting And Rewarding"
g) print(sentence.count("e"))
Output: 4 (The number of times "e" appears in the sentence)
h) print(sentence.startswith("Learning"))
Output: True
i) print(sentence.endswith("rewarding"))
Output: True
j) print(sentence.swapcase())
Output: "lEARNING pYTHON IS EXCITING AND REWARDING"
k) print(sentence.isalpha())
Output: False (The sentence contains spaces, which are not alphabetic)
l) print(len(sentence))
Output: 43 (The total number of characters in the sentence)
3.
a) print(sentence.lstrip())
Output: "Python programming is fun and exciting "
(Removes leading spaces)
b) print(sentence.rstrip())
Output: " Python programming is fun and exciting"
(Removes trailing spaces)
c) print(sentence.strip())
Output: "Python programming is fun and exciting"
(Removes both leading and trailing spaces)
d) print(sentence.islower())
Output: False
(The sentence contains uppercase letters, so it's not entirely lowercase)
e) print(sentence.isupper())
Output: False
(The sentence contains lowercase letters, so it's not entirely uppercase)
f) print(sentence.join(["Learning", "Python", "is", "fun"]))
Output: " LearningPython programming is fun and exciting LearningPython programming is
fun and exciting LearningPython programming is fun and exciting"
(Joins the list of words using the sentence as a separator)
g) print(sentence.startswith("Python"))
Output: False
(The sentence starts with spaces, not "Python")
h) print(sentence.endswith("exciting"))
Output: False
(The sentence ends with spaces, not "exciting")
i) print(sentence.count("o"))
Output: 3
(Counts the occurrences of the letter "o" in the sentence)
j) print(" ".join(["Python", "is", "fun"]))
Output: "Python is fun"
(Joins the list of words with a single space in between)