0% found this document useful (0 votes)
36 views

College of Technology and Engineering, Mpuat, Udaipur (Raj.)

The document contains 29 Python programs written by Sourabh Manawat, a third year B.Tech student. The programs cover topics like determining if a number is positive, negative or zero; calculating factorials, LCM, power of 2 and Fibonacci sequence using recursion; sorting words in a string; and basic math operations in a calculator. The code snippets and outputs are provided for each program.

Uploaded by

Sourabh Menaria
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)
36 views

College of Technology and Engineering, Mpuat, Udaipur (Raj.)

The document contains 29 Python programs written by Sourabh Manawat, a third year B.Tech student. The programs cover topics like determining if a number is positive, negative or zero; calculating factorials, LCM, power of 2 and Fibonacci sequence using recursion; sorting words in a string; and basic math operations in a calculator. The code snippets and outputs are provided for each program.

Uploaded by

Sourabh Menaria
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/ 18

COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.

)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-19

OBJECTIVE - Write a program to find weather the number is positive of negative using
ladder if - else

CODE –
num =float(input("Enter the number : "))
if num>0:
print(num," is a positive number")
elif num==0:
print(num," is a zero intiger")
else:
print(num," is a negative number")
OUTPUT :-

Enter the number : 5


5.0 is a positive number
Enter the number : 0
0.0 is a zero intiger
Enter the number : -9
-9.0 is a negative number
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-20

OBJECTIVE - Write a program to find weather the number is positive of negative using
nested if - else

CODE –
num =float(input("Enter the number : "))
if num>0:
if num==0:
print(num," is a zero intiger")
else:
print(num," is a positive number")
else:
print(num," is a negative number")

OUTPUT :-

Enter the number : 5


5.0 is a positive number
Enter the number : 0
0.0 is a negative number
Enter the number : -2
-2.0 is a negative number
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-21

OBJECTIVE - Write a program to find factorial of a number

CODE –
# print factors of a number
def factors(x):
for i in range(1,x+1):
if(x%i==0):
print(i)
num=int(input("enter the number : "))
factors(num)
OUTPUT :-

enter the number : 20


1
2
4
5
10
20
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-22

OBJECTIVE - Write a program to make a simple calculator

CODE –
# print calculator
def addition(x,y):
return x+y
def sub(x,y):
return x-y
def multi(x,y):
return x*y
def divide(x,y):
return x/y

print("Enter the operation to perform : ")


print(" 1 for addition ")
print(" 2 for minus")
print(" 3 for multi")
print(" 4 for divide")
while True:
choice=int(input("enter your choice 1/2/3/4 : "))

if choice in [1,2,3,4]:
num1=float(input("enter first number : "))
num2=float(input("enter 2nd number : "))

if choice==1:
print(" addition is : ",addition(num1,num2))
elif choice == 2:
print(" subtraction is : ", sub(num1, num2))
elif choice == 3:
print(" multipli is : ", multi(num1, num2))
else:
print(" divide is : ", divide(num1, num2))
else:
print("wrong input")
next_calculation=input("want to do next calculation (yes/no)? ")
if(next_calculation=="no"):
break

OUTPUT :-

Enter the operation to perform :


1 for addition
2 for minus
3 for multi
4 for divide
enter your choice 1/2/3/4 : 1
enter first number : 5
enter 2nd number : 15
addition is : 20.0
want to do next calculation (yes/no)? Yes
enter your choice 1/2/3/4 : 2
enter first number : 20
enter 2nd number : 5
subtraction is : 15.0
want to do next calculation (yes/no)? yes
enter your choice 1/2/3/4 : 3
enter first number : 2
enter 2nd number : 5
multipli is : 10.0
want to do next calculation (yes/no)? yes
enter your choice 1/2/3/4 : 4
enter first number : 20
enter 2nd number : 2
divide is : 10.0
want to do next calculation (yes/no)?
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-23

OBJECTIVE - Write a program to find the LCM of two input numbers.

CODE –
def calculate_lcm(x,y):
if(x>y):
greater=x
else:
greater=y

while True:
if((greater%x==0)and(greater%y==0)):
lcm=greater
break
greater+=1
return lcm
num1=int(input("enter first number : "))
num2=int(input("enter 2nd number : "))
print("lcm of numbers is : ",calculate_lcm(num1,num2))
OUTPUT :-

enter first number : 10


enter 2nd number : 20
lcm of numbers is : 20
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-24

OBJECTIVE - Write a program to display power of 2 using Anonymous function.

CODE –
# power of 2 using anonymous funciton
terms=int(input("enter the range of numbers : "))

result=list(map(lambda x:2**x,range(terms)))
for i in range(terms):
print("2 raised to power ",i," is ",result[i])

OUTPUT :-

enter the range of numbers : 10


2 raised to power 0 is 1
2 raised to power 1 is 2
2 raised to power 2 is 4
2 raised to power 3 is 8
2 raised to power 4 is 16
2 raised to power 5 is 32
2 raised to power 6 is 64
2 raised to power 7 is 128
2 raised to power 8 is 256
2 raised to power 9 is 512
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-25

OBJECTIVE – Write a program to display fibonacci sequence using Recursion

CODE –
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT :-
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-26

OBJECTIVE - Write a program to transpose a matrix using a nested loop

CODE –
X = [[12,7],
[4 ,5],
[3 ,8]]

result = [[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]

for r in result:
print(r)

OUTPUT :-
[12, 4, 3]
[7, 5, 8]
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-27

OBJECTIVE - Write a program to sort alphabetically word form a string provided by the
user.

CODE –
my_str = input("Enter a String : ")
words = [word.lower() for word in my_str.split()]
words.sort()
print("The sorted words are:")
for word in words:
print(word)

OUTPUT :-
Enter a String : My name is sourabh manawat
The sorted words are:
is
manawat
my
name
sourabh
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-28

OBJECTIVE - Write a program to find the sum of natural numbers using Recursion.

CODE –
def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)

num = int(input("Enter the Number : "))

if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))

OUTPUT :-
Enter the Number : 20
The sum is 210
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-29

OBJECTIVE - Write a program to find factorial of a number using recursion.

CODE –
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num = int(input(“Enter the number : “))


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

OUTPUT :-
Enter the number : 10
The factorial of 10 is 3628800
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-30

OBJECTIVE - Write a program to print Binary Number using Recursion.

CODE –
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')

dec = int(input("Enter the number : "))

convertToBinary(dec)
print()

OUTPUT :-

Enter the number : 10


1010
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-31

OBJECTIVE - Write a program to remove punctuations from a string.

CODE –
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = input("Enter a string : ");

no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char

# display the unpunctuated string


print(no_punct)

OUTPUT :-

Enter a string : Hello!!!, he said ---and went.


Hello he said and went
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-32

OBJECTIVE - Write a program to check if two string are Anagram.

CODE –
# str1 = "Race"
# str2 = "Care"
str1=input("Enter 1st string : ")
str2=input("Enter 2nd string : ")
# convert both the strings into lowercase
str1 = str1.lower()
str2 = str2.lower()

# check if length is same


if(len(str1) == len(str2)):
# sort the strings
sorted_str1 = sorted(str1)
sorted_str2 = sorted(str2)
# if sorted char arrays are same
if(sorted_str1 == sorted_str2):
print(str1 + " and " + str2 + " are anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")

OUTPUT :-
Enter 1st string : race
Enter 2nd string : care
race and care are anagram.
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-33

OBJECTIVE - Write a program to use a global variable in nested function.

CODE –
def outer_function():
num = 20

def inner_function():
global num
num = 25

print("Before calling inner_function(): ", num)


inner_function()
print("After calling inner_function(): ", num)

outer_function()
print("Outside both function: ", num)

OUTPUT :-

Before calling inner_function(): 20


After calling inner_function(): 20
Outside both function: 25
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI

Subject – Data Analysis with Python (CS 366)

PROGRAM-34

OBJECTIVE - Write a program to import a module

CODE –
# import standard math module
import math

# use math.pi to get value of pi


print("The value of pi is", math.pi)

# import module by renaming it


import math as m

print("import module by renaming it ",m.pi)

OUTPUT :-

The value of pi is 3.141592653589793


import module by renaming it 3.141592653589793

You might also like