63 Program PDF
63 Program PDF
Page 1
❖ BASIC PYTHON PROGRAMS
1. Factorial
fact=1
num=int(input("enter the no="))
for i in range(num,0,-1):
fact=fact*ii
print("factorial is-=",fact)
Output:-
2. Fibonacci
a=0
b=1
count=10;
print( a,b)
for i in range (2,count,+1):
c=a+b
print(c)
a=b
b=c
Output:-
0
1
1
2
3
5
8
13
21
34
Page 2
for i in range (0,a):
if(a%2==0):
flag=1
break;
if(flag==1):
print("no is not prime")
else:
print("no is prime")
Output:-
enter the number= 12
no is not prime
Output:-
Output:-
8. Reverse String
a=input("enter string=")
a[::-1]
Output:-
9. Pallindrome
a=input("enter the string=")
b=a[::-1]
if(a==b):
print("string is pallindrome")
else:
print("string is not pallindrome")
Output:-
print(count)
Page 5
Output:-
enter string=shivaji
3
Output:-
enter the no =12
12
24
36
48
60
72
84
96
108
120
Page 6
Output:-
0.7658840982392084
Output:-
enter the age=22
your eligible for voting
Output:-
enter the no=5
15
Output:-
enter the no=123
321
Page 7
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if(j <= rows - i):
print(' ', end = ' ')
else:
print("*", end = ' ')
print()
Output:-
Please Enter the Total Number of Rows : 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Output:-
Enter the number of which you have to find power: 2
Enter the power: 3
8
Output:-
enter the no of element=5
12
Page 8
26
56
22
25
[12, 26, 56, 22, 25]
23.Write a program to create a list of values inputted by user & sort in increasing
order
list=[]
n=int(input("enter the no of element="))
for i in range(0,n):
ele=int(input())
list.append(ele)
list.sort()
print(list)
Output:-
enter the no of element=3
452
2
3
[2, 3, 452]
Output:-
Sorted array is:
11 12 22 25 34 64 90
Page 9
break
phone = input(f"Enter phone number for {name}: ").strip()
phone_dict[name] = phone
print("\nPhone Dictionary:")
for name, phone in phone_dict.items():
print(f"{name}: {phone}")
Output:-
Enter name (or 'exit' to stop): rushi
Enter phone number for rushi: 7035857070
Enter name (or 'exit' to stop): sandy
Enter phone number for sandy: 7972413282
Enter name (or 'exit' to stop): chaitanya
Enter phone number for chaitanya: 9423363448
Enter name (or 'exit' to stop): exit
Phone Dictionary:
rushi: 7035857070
sandy: 7972413282
chaitanya: 9423363448
Output:-
Enter values separated by spaces: rushi chaitanya deva
Tuple created: ('rushi', 'chaitanya', 'deva')
27.Reverse a interger
n=int(input("enter the number="))
reverse=0
while n!=0:
reminder=n%10
reverse=reverse*10+reminder
n //= 10
Page 10
print(reverse)
Output:-
enter the number=123
321
28.Write a program to create a new string made of an input strings first ,middle
and last character
f=(input("enter the first name="))
m=(input("enter the middle name="))
l=(input("enter the last name="))
print(f+m+l)
Output:-
enter the first name=rushi
enter the middle name=sunil
enter the last name=kolekar
rushisunilkolekar
Output:-
3
[9230]
Output:-
Enter the base value 2
Enter the power of value 3
8
Output:- [2, 10, 11, 18, 20, 21, 45, 45, 50, 63, 99]
32.Draw pyramid
def draw_pyramid(rows):
for i in range(1, rows + 1)
print(' ' * (rows - i), end='')
print('* ' * i)
rows = int(input("Enter the number of rows for the pyramid: "))
draw_pyramid(rows)
Output:-
Page 12
Enter the number of rows for the pyramid: 5
*
**
***
****
*****
Output:-
[2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
34.GCD of 2 numbers
a=int(input("Enter the 1st number"))
b=int(input("Enter the 2nd number"))
if(a>b):
small=b
else:
small=a
for i in range(1,small+1):
if((a%i==0) & (b%i==0)):
gcd=i
print("GCD of given numbers is ",gcd)
Output:-
Enter the 1st number 60
Enter the 2nd number 48
Page 13
GCD of given numbers is 12
Output:-
2020 is leap year
Output:-
Enter the number 6
6 is perfect number
37.concating 2 strings
a=input("Enter 1st string")
b=input("Enter 2nd string")
print(a+b)
Output:-
Enter 1st string omkar
Page 14
Enter 2nd string jadhav
omkarjadhav
Output:-
Enter the number 16
16 is perfect square number
Output:-
Enter the number 26
factors= [2, 13, 26]
Page 15
return True
str=input("Enter string")
if(ispangram(str)):
print("string is pangram")
else:
print("string is pangram")
Output:-
Enter string omkar
string is pangram
Output:-
Enter the radius of Cylinder: 5
Enter the height of Cylinder: 10
Volume of Cylinder is 785.0
Output:-
['a', 'b', 'e', 'g', 'q', 'x', 'z']
Page 16
c=a**b
print("The exponatial value is=",c)
Output:-
Enter the base number 2
Enter the power of base number 3
The exponatial value is= 8
Output:-
Enter the number 5
The sum of natural numbers from 1 to 5 is: 15
Page 17
median = median(num1, num2, num3)
print(f"The median of {num1}, {num2}, {num3} is: {median}")
Output:-
Enter 1st number 10
Enter 2nd number 15
Enter 3rd number 20
The median of 10, 15, 20 is: 15
Output:-
Binary representation of 65: 1000001
47.decimal to octal
decimal_number = 65
octal_number = oct(decimal_number)[2:]
print(f"Octal representation of {decimal_number}: {octal_number}")
Output:-
Octal representation of 65: 101
48.decimal to hexadecimal
decimal_number = 65
hexadecimal_number = hex(decimal_number)[2:]
print(f"Hexadecimal representation of {decimal_number}:
{hexadecimal_number.upper()}")
Output:-
Hexadecimal representation of 65: 41
Page 18
print(f"The sum of digits in {num} is: {result}")
Output:-
Enter the number 4578
The sum of digits in 4578 is: 24
Output:-
The sum of elements in the list [1, 2, 3, 4, 5] is: 15
Output:-
The area of the triangle with sides 5, 7, 8 is: 17.320508075688775
Page 19
{'name': 'Charlie', 'age': 20},
{'name': 'David', 'age': 35},
{'name': 'Eve', 'age': 28}
]
sorted_people = sorted(people, key=lambda x: x['age'],reverse="True")
print("Sorted by age (ascending):")
for person in sorted_people:
print(person)
Output:-
Sorted by age (ascending):
{'name': 'David', 'age': 35}
{'name': 'Bob', 'age': 30}
{'name': 'Eve', 'age': 28}
{'name': 'Alice', 'age': 25}
{'name': 'Charlie', 'age': 20}
Output:-
Enter a string: Hello
ell
Page 20
Output:
HelloWorld
55.Create a new string made of the first, middle, and last characters of
each input string:
def create_new_string(*strings):
new_string = ''
for s in strings:
if len(s) < 3:
new_string += s
else:
new_string += s[0] + s[len(s)//2] + s[-1]
return new_string
print(create_new_string('abc', 'hello', 'python'))
Output:-
abchlophn
56.Arrange string characters such that lowercase letters should come fir
st:
def arrange_string(s):
lowercase_chars = [c for c in s if c.islower()]
uppercase_chars = [c for c in s if c.isupper()]
return ''.join(lowercase_chars + uppercase_chars)
s = "Hello World"
print(arrange_string(s))
Output:-
elloorldHW
57.Count all letters, digits, and special symbols from a given string:
def count_characters(s):
letters = 0
digits = 0
symbols = 0
for char in s:
if char.isalpha():
Page 21
letters += 1
elif char.isdigit():
digits += 1
else:
symbols += 1
return letters, digits, symbols
s = input("Enter a string: ")
letters, digits, symbols = count_characters(s)
print(f"Letters: {letters}")
print(f"Digits: {digits}")
print(f"Symbols: {symbols}")
Output:
Enter a string: Hello World
Letters: 10
Digits: 0
Symbols: 1
Output:-
Page 22
arbqcp
Output:
True
False
Page 23
Output:-
9
Output:-
The input string is: Pythonforbeginners is a great source .
Count of character 'a' is 2
Count of character 'f' is 1
Count of character 'r' is 4
Count of character 'o' is 3
Count of character 't' is 2
Count of character 'g' is 2
Count of character 'c' is 1
Count of character '.' is 1
Count of character 'b' is 1
Count of character 'y' is 1
Count of character 'i' is 2
Count of character 'e' is 4
Count of character 's' is 3
Count of character 'h' is 1
Count of character 'u' is 1
Count of character 'P' is 1
Count of character 'n' is 3
Count of character ' ' is 5
print(str)
Output:-
dlroW olleH
Page 24