Grade 11 CS
Grade 11 CS
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
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))
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)
total = 0
for i in range(1, 11):
total += i
print("Sum of numbers from 1 to 10 (using for loop):", total)
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
num = 5
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
num = 5
i=1
while i <= 10:
print(f"{num} x {i} = {num * i}")
i += 1
i=1
while i <= 20:
if i % 2 == 0:
print(i, end=" ")
i += 1
print() # For newline
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.")
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)