You are on page 1of 6

Python Programming

1 Lab

Use Python Shell for the given below

>>> type(1)
<class 'int'>
>>> type(15)
<class 'int'>
>>> type(0)
<class 'int'>
>>> type(-46)
<class 'int'>
>>> type(4.5)
<class 'float'>
>>> type(5.8)
<class 'float'>
>>> type(2342423424.3)
<class 'float'>
>>> type(4.0)
<class 'float'>
>>> type(0.0)
<class 'float'>
>>> type(-23.5)
<class 'float'>
>>> complex(4, 5)
(4+5j)
>>> complex(6, 8)
(6+8j)
>>> complex(3.4, 3.4)
(3.4+3.4j)
>>> complex(0, 0)
0j
>>> complex(5)
(5+0j)
>>> complex(0, 4)
4j
>>> my_string = "Hello"
>>> my_string[0]
'H'
>>> my_string[1]
'e'
>>> my_string[2]
'l'

Compiled By: Rajeev Ranjan Kumar Tripathi


Python Programming
2 Lab

>>> my_string[3]
'l'
>>> my_string[4]
'o'
>>> my_string = "Hello"
>>> my_string[-1]
'o'
>>> my_string[-2]
'l'
>>> my_string[-3]
'l'
>>> my_string[-4]
'e'
>>> my_string[-5]
'H'
>>>name="Rajeev Ranjan Kumar Trtipathi"
name[0:24:2]
'Rje ajnKmrTt'
>>>name[:8]
'Rajeev R'

Compiled By: Rajeev Ranjan Kumar Tripathi


Python Programming
3 Lab

Object 1: Implementation of Calculator in Python


def calc(n):
s=0
p=1
a=int(input("first number"))
b=int(input("second number"))
if n==1:
s=a+b
print("sum",s)
elif n==2:
p=a*b
print("prod",p)
elif n==3:
s=a-b
print("diff",s)
else:
p=a/b
print("divide",p)
i=int(input("enter for calculation 1. add 2. product 3.subtract
4.divide"))
calc(i)

Object 2: Implementation of Simple Interest Calculator

n = int(input("Enter the principle amount:"))


rate = int(input("Enter the rate:"))
years = int(input("Enter the number of years:"))
for i in range(years):
n = n + ((n * rate) / 100)
print(n)

Object 3: Implementation of Binary Search in Python


def binary(a, fir, las, term):
mid=int((fir+las)/2)
if term>a[mid]:
binary(a, mid, las, term)
elif term<a[mid]:
binary(a,fir, mid, term)
elif term==a[mid]:
print("Number found at", mid+1)

Compiled By: Rajeev Ranjan Kumar Tripathi


Python Programming
4 Lab

else:
print("Number is not there in the array")
b=[1,2,3,4,5]
fir=0
las=len(b)
term=4
binary(b,fir,las,term)

Object 4: Reversing a String


string=input("Enter a string:")
revstring=string[::-1]
print(revstring)

Object 5: Area calculation of a Triangle


a = int(input("area with sides(1) or base-height(2) :"))
if a == 2:
b = int(input("enter the base:"))
h = int(input("enter the height:"))
area = float(0.5*b*h)
print("area is", area)
else:
s1 = float(input("enter side 1:"))
s2 = float(input("enter side 2:"))
s3 = float(input("enter side 3:"))
s = float((s1 + s2 + s3) / 2)
area = (s * (s - s1)*(s - s2)*(s - s3)) ** 0.5
print("area is", area)

Object 6: Python program to find average of N numbers


n = int(input("Enter the total number you want to enter:"))
sum = 0
for i in range(n):
x = int(input("Enter the number:"))
sum = sum + x
avg = sum / n

Compiled By: Rajeev Ranjan Kumar Tripathi


Python Programming
5 Lab

print("Average=", avg)

Object 7:Fibonacci series program in Python

n= int(input("enter the number of elements you want in series:"))


c=[ ]
c.append(0)
c.append(1)
a=0
b=1
d=0
for i in range( 1, n-1):
d=a+b
c.append(d)
a=b
b=d
for i in c:
print(i)

Object 8: Factorial calculation in Python


n= int(input("Enter the number you want to find the factorial of: "))
prod=1
for i in range(1,n+1):
prod=prod*i
print(prod)

Object 9: To check whether a number is prime or not.


n = int(input("Enter the number that has to be checked: "))
a=0
for i in range(2, int(n / 2)):
if n % i == 0:
a=a+1
if a > 0:
print("The number is not prime")
else:
print("The number is prime")

Object10: Performing Bubble sort in Python


arr=[1,5,9,3,2,6]
n=len(arr)

Compiled By: Rajeev Ranjan Kumar Tripathi


Python Programming
6 Lab

for i in range(n):
for j in range(0,n-i-1):
if arr[j]>arr[j+1]:
temp=arr[j+1]
arr[j+1]=arr[j]
arr[j]=temp
print("Sorted Array : ", arr )

Object 11: To check whether a number is palindrome or not.


n = int(input("Input a number"))
temp = n
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n = n // 10
if temp == rev:
print("palindrome")
else:
print("Not palindrome")

Compiled By: Rajeev Ranjan Kumar Tripathi

You might also like