0% found this document useful (0 votes)
28 views2 pages

CBSE Class 9 Python Practice Solutions

Uploaded by

deepagrawal492
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)
28 views2 pages

CBSE Class 9 Python Practice Solutions

Uploaded by

deepagrawal492
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

Class 9 Python Practice Programs - Solutions

Q1. Print first 10 natural numbers


for i in range(1, 11):
print(i)
This prints numbers from 1 to 10 using a for loop.

Q2. Multiplication table of a number


n = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
Takes a number and prints its multiplication table.

Q3. Squares from 1 to 15


for i in range(1, 16):
print(f"Square of {i} = {i * i}")
Prints square of numbers 1 to 15.

Q4. Even numbers between 1 and 50


for i in range(2, 51, 2):
print(i)
Prints even numbers from 2 to 50.

Q5. Sum of first 20 natural numbers


total = 0
for i in range(1, 21):
total += i
print("Sum =", total)
Calculates sum = 210.

Q6. Numbers 1–10 using while loop


i = 1
while i <= 10:
print(i)
i += 1
Prints 1 to 10 using while loop.

Q7. Reverse of a number


n = int(input("Enter a number: "))
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10
print("Reversed number =", rev)
Reverses digits of a number.

Q8. Print digits of a number


n = int(input("Enter a number: "))
print("Digits are:")
while n > 0:
digit = n % 10
print(digit)
n //= 10
Prints digits one by one.

Q11. Positive, Negative or Zero


n = int(input("Enter a number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Checks if number is positive, negative or zero.

Q12. Even or Odd


n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Checks if number is even or odd.

Q13. Largest of three numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Largest =", a)
elif b >= a and b >= c:
print("Largest =", b)
else:
print("Largest =", c)
Finds largest among three numbers.

Q14. Create list using append()


items = []
n = int(input("How many items? "))
for i in range(n):
item = input("Enter item: ")
[Link](item)
print("Final list =", items)
Creates a list with user input.

Q15. Create list of integers and sort


nums = []
n = int(input("How many numbers? "))
for i in range(n):
num = int(input("Enter number: "))
[Link](num)
[Link]()
print("Sorted list =", nums)
Creates list of integers and sorts it.

Common questions

Powered by AI

Creating a multiplication table for a given number involves using a loop to multiply the number by integers from 1 to 10. Establish a loop that iterates over this range and, within each iteration, compute the product of the given number and the current loop variable, then print the result in a formatted statement to represent the multiplication process ('n x i = result').

To find the largest of three numbers, use comparison algorithms by comparing each number with the others. Start by assuming the first number is the largest, then compare it to the second number; if the second is larger, update your assumption. Compare the assumed largest to the third number; update again if necessary. This involves nested 'if-else' statements to perform pairwise comparisons, ensuring that the largest number is correctly identified .

To print the squares of numbers from 1 to 15 in Python, use a for loop that iterates over a range from 1 to 16 (excluding 16). In each iteration, calculate the square of the loop variable by multiplying it with itself and print the result. This technique effectively utilizes a simple loop and arithmetic operations to achieve the desired output .

A method to reverse the digits of a number using a loop involves repeatedly extracting the last digit using modulus division and constructing the reversed number step-by-step. Initialize a reversed number variable to zero, then use a loop to iterate while the number is not zero. In each iteration, extract the last digit of the number, append it to the reversed number by multiplying the current reversed number by ten and adding the extracted digit, then remove the digit from the original number by integer division by ten. Continue this loop until the original number becomes zero .

In Python, you can create a list to store values entered by a user by initializing an empty list and using a loop to append each input to the list. Prompt the user for the number of values, then iterate that many times asking for input each time, appending each value to the list. Once the list is fully populated, use the sort method of the list object to sort the list in increasing order. This utilizes both the append() function for adding elements and the sort() method for organizing them .

Printing the digits of a number individually can be done by using a loop that iteratively extracts the last digit of the number using modulus operation and then removes that digit by performing integer division by ten. Continue the loop until the number becomes zero. Print each digit during the extraction process. This method effectively processes each digit from right to left .

To detect if a number is even or odd, check the remainder when the number is divided by 2 using the modulus operator. If the remainder is zero, the number is even; otherwise, it is odd. This logic is implemented using a simple 'if-else' conditional structure to print the appropriate classification based on the result of the modulus operation .

To print even numbers between 1 and 50, use a for loop that starts from 2 and iterates in steps of 2 up to 50. This loop effectively selects and prints each even number directly without needing additional conditional checks, thereby making it an efficient way of addressing the problem .

In programming, you can determine if a number is positive, negative, or zero by using conditional statements. First, you check if the number is greater than zero to identify it as positive. If it is not positive, you check if it is less than zero to determine if it's negative. If neither, you categorize it as zero. This process uses 'if,' 'elif,' and 'else' statements .

To sum the first 20 natural numbers, utilize a for loop iterating from 1 to 20, accumulating the sum into a variable initialized at zero. In each iteration, add the current loop variable to this sum variable. After completing the loop, the sum variable will contain the total. The result of this operation, particularly for the first 20 natural numbers, is 210 .

You might also like