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

Python Practice Programs

python
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)
1 views7 pages

Python Practice Programs

python
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

Grade X

List of Python program


Note: Write all the following python programs in A4 sheet neatly and submit
by 6th Jan, 2025.

1. Python program to print the all the Fibonacci numbers up to the given range
n = int(input("Enter the term number: "))
a=0
b=1
for i in range(2, n + 1):
​ ​ c=a+b
​ ​ a=b
​ ​ b=c

How it works:
The Fibonacci series starts with 0 and 1.
Each next number = sum of previous two numbers.
The loop continues until the values reach the given range limit.

2. Python program to print grades based on marks scored by a student given marks scored by
student
marks = int(input("Enter the marks scored by the student: "))

if marks >= 90:


print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
elif marks >= 50:
print("Grade: D")
else:
print("Grade: Fail")
How it works:

●​ Takes marks as input.


●​ Checks the marks against conditions using if, elif, and else.
●​ Prints the correct grade.

3. write a program to check whether the given year is a leap year or not.
year = int(input("Enter a year: "))

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):


print(year, "is a leap year.")
else:
print(year, "is not a leap year.")

Leap Year Rules:

A year is a leap year if:

●​ It is divisible by 400, OR
●​ It is divisible by 4 but not divisible by 100​

4. Write a program to calculate the factorial of a given number.


num = int(input("Enter a number: "))
factorial = 1

if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)

How it works:

●​ Factorial means:​
n! = n × (n−1) × (n−2) × ... × 1
●​ Example:​
5! = 5 × 4 × 3 × 2 × 1 = 120

5. Python program to check whether a number is Prime or not


num = int(input("Enter a number: "))

# Prime numbers are greater than 1


if num > 1:
for i in range(2, num):
if num % i == 0:
print(num, "is not a prime number.")
break
else:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")

How it works:

●​ A prime number has only two factors: 1 and itself.


●​ We check whether the number is divisible by any number between 2 and (num-1).
●​ If divisible → Not Prime
●​ If not divisible → Prime

6. Python program to print all Prime numbers in an Interval


start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

print("Prime numbers between", start, "and", end, "are:")

for num in range(start, end + 1):


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")

Summary of the Program

The program takes a starting number and an ending number from the user and checks each number in
that range to find out which numbers are prime. For every number, it tests whether it can be divided evenly
by any number other than 1 and itself. If it cannot be divided by any such number, it is identified as a prime
number and printed. In simple words, the program prints all the prime numbers present between the given
range.
7. Python program to print list in reverse order using loops
l=[]
rl=[]
n=int(input("how many elements do you want to give:? "))
for i in range(n):
print("enter list element",i+1)
v=int(input() )
[Link](v)
print("printing the list before reversing :",l)

for i in range(len(l)-1, -1, -1):


[Link](l[i])
print("printing the list after reversing :",rl)

Summary of the Program

This program allows the user to enter a list of numbers and then prints the list in reverse order. First, the
program asks how many elements the user wants to input and then stores each value in the list l. After
displaying the original list, it uses a loop to access elements from the last index to the first index and
stores them in another list called rl. Finally, it prints the reversed list. In simple terms, the program
creates a list from user input and then prints its reverse using a loop.

8. Python program to count the frequency of given element in list


l = []
n = int(input("How many elements do you want to enter? "))

for i in range(n):
value = int(input("Enter element: "))
[Link](value)

print("List:", l)

element = int(input("Enter the element whose frequency you want to count: "))

count = 0
for i in l:
if i == element:
count += 1

print("Frequency of", element, "is:", count)

How it works:
1.​ The program first takes input from the user to create a list.
2.​ Then it asks which element's frequency needs to be counted.
3.​ Using a loop, it checks every element of the list and increases the count when it matches the given
number.
4.​ Finally, it prints how many times the element appears in the list.

9. Python program to sort the given list


l=[]
n=int(input("how many elements do you want in the list? "))
for i in range(n):
print("enter list element",i+1)
v=int(input() )
[Link](v)
print("Before sorting the list given list :")
print(l)
[Link]()
print("---------------------")
print("after sorting the list given list :")
print(l)

Summary of the Program

This program allows the user to enter a list of numbers and then displays the list before and after sorting it.
First, the user is asked how many elements they want to add, and each value is collected and stored in
the list l. The program then prints the original list as entered by the user. After that, the sort() function is
used to arrange the list elements in ascending order. Finally, the sorted list is printed, showing the list in
order from smallest to largest. In simple terms, the program collects numbers from the user and sorts them
in ascending order.

10. Write a program to display only odd numbers in the given list using a for loop.
l = []
n = int(input("How many elements do you want in the list? "))

for i in range(n):
value = int(input("Enter element: "))
[Link](value)

print("Original List:", l)

print("Odd numbers in the list:")


for num in l:
if num % 2 != 0:
print(num, end=" ")

How it works:

●​ The program takes input from the user and stores the values in a list.
●​ Using a for loop, each number in the list is checked.
●​ If a number is not divisible by 2, it is odd, and the program prints it.

11. Python program to find second largest number in a list


l = []
n = int(input("How many elements do you want in the list? "))

for i in range(n):
value = int(input("Enter element: "))
[Link](value)

print("List entered:", l)

[Link]()

print("Second largest number is:", l[-2])

How it works:

1.​ The program takes input from the user to create a list.
2.​ It prints the original list.
3.​ The sort() function arranges the list elements in ascending order.
4.​ l[-2] accesses the second last element, which is the second largest number.

Note: Practice Orange Data mining tool. Links for the data are mentioned
below.

Statistical Data-
[Link]

Computer Vision-
[Link]

Natural Language Processing-

[Link]

You might also like