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

Python Programs for Beginners

The document contains a collection of Python programs that demonstrate various programming concepts, including finding the largest of three numbers, checking for prime numbers, printing Fibonacci sequences, and more. Each program includes user input and outputs results based on the given logic. The programs cover a range of topics such as recursion, string manipulation, and list operations.

Uploaded by

areeba1077777
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)
11 views3 pages

Python Programs for Beginners

The document contains a collection of Python programs that demonstrate various programming concepts, including finding the largest of three numbers, checking for prime numbers, printing Fibonacci sequences, and more. Each program includes user input and outputs results based on the given logic. The programs cover a range of topics such as recursion, string manipulation, and list operations.

Uploaded by

areeba1077777
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

Python Practical File Programs

1. Program to Find the 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)

2. Program to Check Whether a Number is Prime


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

if num <= 1:
flag = False

for i in range(2, int(num**0.5) + 1):


if num % i == 0:
flag = False
break

if flag:
print("Prime Number")
else:
print("Not Prime")

3. Program to Print Fibonacci Sequence


n = int(input("Enter number of terms: "))
a, b = 0, 1

for _ in range(n):
print(a, end=" ")
a, b = b, a + b

4. Program to Check Palindrome


s = input("Enter a string/number: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

5. Program to Find Factorial Using Recursion


def fact(n):
if n == 0:
return 1
return n * fact(n - 1)

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


print("Factorial =", fact(n))

6. Program to Count Number of Vowels in a String


s = input("Enter a string: ").lower()
count = 0

for char in s:
if char in "aeiou":
count += 1

print("Number of vowels:", count)

7. Program to Find Sum of Digits of a Number


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

while num > 0:


total += num % 10
num //= 10

print("Sum of digits =", total)

8. Program to Reverse a Number


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

while num > 0:


rev = rev * 10 + num % 10
num //= 10

print("Reversed number =", rev)

9. Program to Check Armstrong Number


num = int(input("Enter a number: "))
s = str(num)
result = sum(int(d)**len(s) for d in s)

if result == num:
print("Armstrong Number")
else:
print("Not Armstrong")

10. Program to Display Prime Numbers in a Range


start = int(input("Start: "))
end = int(input("End: "))

print("Prime numbers:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num, end=" ")

11. Program to Find GCD of Two Numbers


import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD =", [Link](a, b))

12. Program to Find LCM of Two Numbers


import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

lcm = abs(a * b) // [Link](a, b)


print("LCM =", lcm)

13. Program to Convert Decimal to Binary


num = int(input("Enter decimal number: "))
print("Binary =", bin(num)[2:])

14. Program to Find Length of a String


s = input("Enter a string: ")
print("Length =", len(s))

15. Program to Replace a Substring


s = input("Enter main string: ")
old = input("Substring to replace: ")
new = input("New substring: ")

print("Result:", [Link](old, new))

16. Program to Merge Two Lists


list1 = list(map(int, input("Enter list1 elements: ").split()))
list2 = list(map(int, input("Enter list2 elements: ").split()))

merged = list1 + list2


print("Merged list:", merged)

17. Program to Sort a List in Ascending Order


lst = list(map(int, input("Enter list elements: ").split()))
[Link]()
print("Sorted list:", lst)

18. Program to Find Second Largest Element in a List


lst = list(map(int, input("Enter list elements: ").split()))
lst = list(set(lst))
[Link]()

if len(lst) < 2:
print("Not enough elements")
else:
print("Second largest =", lst[-2])

You might also like