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

Python Lab Program 2022

Uploaded by

nagakiran9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Lab Program 2022

Uploaded by

nagakiran9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

V Sem B.

Com – Computers Paper – 7 A – Data Science Using Python Lab Programs – 2022

1. Python Program to find the Square Root.


Program :-
import math
num = float(input(" Enter a number: "))
sqRoot = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot))
Output
Enter a number: 16
The square root of a given number 16.0 = 4.0

2. Python Program to swap two variables.


Program :-
x=5
y=7
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# code to swap 'x' and 'y'
x, y = y, x

print ("After swapping: ")


print("Value of x : ", x, " and y : ", y)
Output :-
Before swapping:
Value of x : 5 and y : 7
After swapping:
Value of x : 7 and y : 5
3. Python Program to Generate a Random Number.
1. Program :-
import random
print(random.randint(0,9))
Output:-
9

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 1
2. Program :-
import random

# prints a random value from the list


list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))

# prints a random item from the string


string = "striver"
print(random.choice(string))
Output :-
3
S
4. Python Program to check if a Number is Odd or Even.
Program :-
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output :-
Enter a number: 5
5 is Odd
5. Python Program to Find the Largest Among Three Numbers.
Program :-
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 2
print("The largest number is",largest)

Output :-
Enter first number: 10
Enter second number: 15
Enter third number: 5
The largest number is 15.0
6. Python Program to check prime number?
Program :-
a=int(input("Enter number: "))
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print("Number is prime")
else:
print("Number isn't prime")
Output :-
Enter number: 5
Number is prime
7. Python Program to Display the multiplication Table.
Program :-
number = int(input ("Enter the number : "))
# We are using "for loop" to iterate the multiplication 10 times
print ("The Multiplication Table of: ", number)
for count in range(1, 11):
print (number, 'x', count, '=', number * count)
Output :-
Enter the number : 10
The Multiplication Table of: 10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 3
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
8. Python Program to print the Fibonacci sequence ?
Program :-
Number = int(input("\nPlease Enter the Range : "))
# Initializing First and Second Values
i=0
First_Value = 0
Second_Value = 1

# Find & Displaying


while(i < Number):
if(i <= 1):
Next = i
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
i=i+1
Output :-
Please Enter the Range : 10
0
1
1
2
3
5
8
13
21
34

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 4
9. Python program to Find the Sum of Natural Numbers.
Program :-
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)
Output :-
Enter a number: 5
The sum is 15
10. Python Program to Find Factorial of Number Using Recursion.
Program :-
def factorial(n):
if(n <= 1):
return 1
else:
return(n*factorial(n-1))
n = int(input("Enter number:"))
print("Factorial:")
print(factorial(n))
Output :-
Enter number:5
Factorial:
120
11. Python Program to work with string methods.
Program :-
str = 'Hellow World!'
print(str [0]) # output is "H"
print(str [7]) # output is "W"
print(str [0:6]) #output is Hellow
print(str [7:12]) #output is World

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 5
str1 = 'Hellow '
str2 = ' World!'
print(str1 + str2)
print(str[::-1])
print(len(str))
str = "Python is Object Oriented"
substr = "Object"
print(str.count(substr)) # return 1, becasue the word Object exist 1 time in str
str = "Python is Object Oriented"
substr = "is"
print(str.index(substr))
print(str.upper())
print(str.lower())
print(str.startswith("Python"))
print(str.startswith("Object"))
print(str.endswith("Oriented"))
print(str.endswith("Object"))
print(str.split())
str = "Python is Object Oriented"
tmp = "-"
print (tmp.join(str))
str = "Python is Object Oriented"
st = "Object"
print (str.find(st))
print (str.find(st,20)) #finding from 20th position
str = " Python is Object Oriented "
print (str.strip())
str = " Python is Object Oriented "
print (str.rstrip())
str = " Python is Object Oriented "
print (str.lstrip())
Output:-
H
W
Hellow

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 6
World
Hellow World!
!dlroW wolleH
13
1
7
PYTHON IS OBJECT ORIENTED
python is object oriented
True
False
True
False
['Python', 'is', 'Object', 'Oriented']
P-y-t-h-o-n- -i-s- -O-b-j-e-c-t- -O-r-i-e-n-t-e-d
10
-1
Python is Object Oriented
Python is Object Oriented
Python is Object Oriented
12. Python Program to create a dictionary and print its content.
Program :-
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
#deleting key value
del thisdict["year"]
print(thisdict)
thisdict["year"]=1964
print(thisdict)
Output :-
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
{'brand': 'Ford', 'model': 'Mustang'}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 7
13. Python Program to create class and objects.
Program :-
class Employee:
id = 10
name = "John"
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
# Creating a emp instance of Employee class
emp = Employee()
emp.display()
Output :-
ID: 10
Name: John

Prepared by S.H. Khaseem, MCA, MBA, Lecturer in Computer Science, 9010797739 Page 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy