0% found this document useful (0 votes)
2 views7 pages

Chapter 28 Class 9python

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

Chapter 28 Class 9python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 CODING PRACTICE – 2 (Programs)

1. Add fruit at the end


fruits = ["Apple", "Banana", "Mango"]
[Link]("Orange")
print(fruits)

2. Sum of 5 integers
nums = [2, 4, 6, 8, 10]
print(sum(nums))

3. Palindrome check
s = input("Enter string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

4. Count capital letters


s = input("Enter string: ")
count = 0
for ch in s:
if [Link]():
count += 1
print(count)

5. Convert to uppercase
s = input("Enter string: ")
print([Link]())

6. Min, Max, Average


nums = [3, 6, 1, 8, 4, 9, 2]
print(min(nums))
print(max(nums))
print(sum(nums)/len(nums))

7. Replace odd numbers with square


nums = [1,2,3,4,5,6,7]
for i in range(len(nums)):
if nums[i] % 2 != 0:
nums[i] = nums[i]**2
print(nums)

8. Descending order
nums = [9,2,5,1,7,3,6,4,8]
new_list = sorted(nums, reverse=True)
print(new_list)
9. Convert negative to positive
nums = []
for i in range(5):
n = int(input())
[Link](abs(n))
print(nums)

10. Remove non-multiples of 3


nums = [3,5,6,9,10,12,7,15]
for n in nums[:]:
if n % 3 != 0:
[Link](n)
print(nums)

1. What is a string and how to define it?


A string is a sequence of characters used to store text.
In Python, a string is defined by enclosing characters inside single (' '), double (" "), or triple
quotes (''' ''').

2. How to create multiline strings?


A multiline string is created using triple quotes. It allows text to span multiple lines.
Example:
s = """Hello
Welcome to Python"""

3. What is string slicing? How to slice last 3 characters?


String slicing means extracting a part of a string using index values.
To get last 3 characters:
s[-3:]

4. How can we escape single and double quotes in a string?


By using a backslash (\) before the quote.
Example:
"He said \"Hello\""

5. Explain positive and negative index of a string.


Positive index starts from 0 (left to right).
Negative index starts from -1 (right to left).

6. How can we replicate a string?


By using the * operator.
Example:
"Name" * 5

7. What is string concatenation?


String concatenation means joining two or more strings using the + operator.
Example:
"A" + "B" + "C"

8. When are two strings equal?


Two strings are equal when they have the same characters in the same order.

9. How to convert a string into uppercase?


Using the upper() method.
Example:
[Link]()

10. How to find the length of a string?


Using the len() function.
Example:
len("This is a long string")

11. When should we use lists in Python?


Lists are used when we need to store multiple values in a single variable.

12. Explain two ways of creating a list.


Using square brackets:
a = [1,2,3]
Using list() function:
a = list((1,2,3))

13. What is an empty list? Can it be filled later?


An empty list has no elements:
a = []
Yes, elements can be added later.

14. How to traverse a list?


By using a loop to access each element.
Example:
for i in a:
print(i)

15. What is list concatenation?


Joining two or more lists using + operator.
Example:
a + b + c

16. How to slice first 3 elements of a list?


Using slicing:
a[:3]
17. How to check if an element is present in a list?
Using the in keyword.
Example:
if 5 in a:

18. Explain insert() function.


insert() adds an element at a specified position.
Example:
[Link](1, 10)

19. How does remove() function work?


remove() deletes the first occurrence of a given value from the list.

20. How can we copy a list?


Using the copy() method.
Example:
b = [Link]()

21. Which function finds maximum and minimum in a list?


max() → maximum value
min() → minimum value

22. Explain sum() function in lists.


sum() returns the total of all numeric elements in a list.
Example:
sum([1,2,3])

CODING PRACTICE – 1 (OUTPUTS)

1
str1 = "We are
writing a multi-line string"
print(str1)
Output
We are
writing a multi-line string

2
str1 = "We are
writing a multi-line string"
print(str1)
Output
SyntaxError: EOL while scanning string literal
3
str1 = "Python Language"
print(str1[1:4])
Output
yth

4
str1 = "Python Language"
print(str1[1:4] * 3)
Output
ythythyth

5
str1 = "Hello"
print(len(str1))
Output
5

6
str1 = "python"
print([Link]())
Output
PYTHON

7
str1 = "HELLO"
print([Link]())
Output
hello

8
str1 = "Hello"
print(str1 + " World")
Output
Hello World

9
str1 = "Python"
print(str1 * 2)
Output
PythonPython
10
str1 = "Programming"
print(str1[-3:])
Output
ing

11
list1 = [1, 2, 3, 1, 0]
print(any(list1))
Output
True

12
list1 = [0, 0, 0]
print(any(list1))
Output
False

13
list1 = [1, 2, 3]
print(sum(list1))
Output
6

14
list1 = [1,3,5,7,9,2,4,6,8,0]
for i in range(3,7):
list1[i] = list1[i] * 3
print(list1)
Output
[1, 3, 5, 21, 27, 6, 12, 6, 8, 0]

15
list1 = [1,3,6,8,0]
[Link](2,90)
print(list1)
[Link](3)
print(list1)
[Link](2)
print(list1)
[Link]()
print(list1)
Output
[1, 3, 90, 6, 8, 0]
[1, 90, 6, 8, 0]
[1, 90, 8, 0]
[]

You might also like