[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Class9 Python Textbook Codes With Outputs

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)
10 views3 pages

Class9 Python Textbook Codes With Outputs

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
You are on page 1/ 3

Q1.

Print first 10 natural numbers


for i in range(1, 11):
print(i)
Prints numbers from 1 to 10 using a for loop.
Output: 1 2 3 4 5 6 7 8 9 10

Q2. Multiplication table of a number


n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
Prints multiplication table of a given number.
Example (n=5): 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50

Q3. Squares from 1 to 15


for i in range(1, 16):
print("Square of", i, "=", i * i)
Prints squares of numbers from 1 to 15.
Output: Square of 1 = 1 Square of 2 = 4 ... Square of 15 = 225

Q4. Even numbers between 1 and 50


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

Q5. Sum of first 20 natural numbers


total = 0
for i in range(1, 21):
total += i
print("Sum =", total)
Calculates sum of first 20 natural numbers.
Output: Sum = 210

Q6. Numbers 1–10 using while loop


i = 1
while i <= 10:
print(i)
i += 1
Prints numbers 1 to 10 using while loop.
Output: 1 2 3 4 5 6 7 8 9 10

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.
Example (n=1234): Reversed number = 4321
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 of a number one by one.
Example (n=456): Digits are: 6 5 4

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 whether number is positive, negative or zero.
Example: Input: 7 → Output: Positive

Q12. Even or Odd


n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Checks whether number is even or odd.
Example: Input: 9 → Output: 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.
Example: Input: 4, 8, 2 → Output: Largest = 8

Q14. Create list using append()


items = []
n = int(input("How many items? "))
for i in range(n):
item = input("Enter item: ")
items.append(item)
print("Final list =", items)
Creates a list with user inputs.
Example: Input: apple, ball, cat → Output: ['apple', 'ball', 'cat']

Q15. Create list of integers and sort


nums = []
n = int(input("How many numbers? "))
for i in range(n):
num = int(input("Enter number: "))
nums.append(num)
nums.sort()
print("Sorted list =", nums)
Creates a list of integers and sorts it.
Example: Input: 45, 12, 78 → Output: [12, 45, 78]

You might also like