PRACTICAL of Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

GOVERNMENT POLYTECHNIC AURAI BHADOHI

PRACTICAL : 1
AIM : Getting started with Python and IDLE in interactive and batch modes.
Steps :
1. Open the IDLE program (Windows : Start → All Programs→Python3.9.2→ IDLE.)
2. Run → Python sell
3. You see a windows labelled “ Python sell ”.
4. After Some technical info, There is a prompt, “>>> ” Now ,at prompt type
>>>print “Hello world ”
OUTPUT :

IDLE in batch mode :

1
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 2
AIM : What do the following string methods do?
• lower
• count
• replace
Coding :
str="my self surya prasad prajapati from gpa aurai"
print(str.replace("my","MY")) # This is replace() function
print(str.count(" s")) # This is count() function
str1="SURYA PRASAD PRAJAPATI"
print(str1.lower()) #This is lower() function

OUTPUT:

E:\5th sem\python\code>python pr.py


MY self surya prasad prajapati from gpa aurai
2
surya prasad prajapati

E:\5th sem\python\code>

2
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 3
AIM: Write instructions to perform each of the steps below
(a) Create a string containing at least five words and store it in a variable.
(b) Print out the string.
(c) Convert the string to a list of words using the string split method.
(d) Sort the list into reverse alphabetical order using some of the list methods
(you might need to use dir(list) or help(list) to find appropriate methods).
(e) Print out the sorted, reversed list of words.
Coding :
str="my self surya prasad prajapati from gpa aurai"
print(str)
li=(list(str.split(" "))) # String converting in the list by using list() & split() f
unction
print(li)
so=sorted(li) # Using sorted() function for sortiing the list
print("This is shorted list : ")
print(so)
print("This is reversed list : ")
print(list(reversed(so))) #Using reversed() function for reversing the list

OUTPUT :
E:\5th sem\python\code>python pr3.py
my self surya prasad prajapati from gpa aurai
['my', 'self', 'surya', 'prasad', 'prajapati', 'from', 'gpa', 'aurai']
this is shorted list :
['aurai', 'from', 'gpa', 'my', 'prajapati', 'prasad', 'self', 'surya']
this is reversed list :
['surya', 'self', 'prasad', 'prajapati', 'my', 'gpa', 'from', 'aurai']

3
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 4
AIM : Write a program that determines whether the number is prime.
What is your favorite number? 24
24 is not prime
What is your favorite number?
31 31 is prime
Coding :
num=int(input("What is your favorite number ?: "))
if num>1:
for i in range(2,num): #for loop
if num%i==0: # if Steatement
print(num,"is not prime")
print(i,"time",num//i,"is",num)
break #break keyword
else: # else Statement
print(num,"is prime")
else:
print(num,"is not prime")

OUTPUT :
E:\5th sem\python\code>python pr4.py
What is your favorite number ?: 11
11 is prime

E:\5th sem\python\code>python pr4.py


What is your favorite number ?: 10
10 is not prime
2 time 5 is 10
E:\5th sem\python\code>

4
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 5
AIM : Find all numbers which are multiple of 17, but not the multiple of 5,
between 2000 and 2500?
Coding :
a=[]
for x in range(2000,2500):
if x%17==00 and x%5!=0:
a.append(str(x))
print(' '.join(a))

OUTPUT :

E:\5th sem\python\code>PYTHON PR5.PY


2006 2023 2057 2074 2091 2108 2142 2159 2176 2193 2227 2244 2261
2278 2312 2329 2346 2363 2397 2414 2431 2448 2482 2499

E:\5th sem\python\code>

5
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 6
AIM: Swap two integer numbers using a temporary variable. Repeat the exercise
using the code format: a, b = b, a. Verify your results in both the cases.:
Coding:
a=input("Enter first number : ")
b=input("Enter second number : ")
print("Before swapping first number is : ",a)
print("Before swapping second number is : ",b)

# Swapping using temp variable


temp=a
a=b
b=temp
print("After swapping first number is ",a)
print("After swapping second number is",b)

OUTPUT:
E:\5th sem\python\code>python pr6.py
Enter first number : 45
Enter second number : 12
Before swapping first number is : 45
Before swapping second number is : 12
After swapping first number is 12
After swapping second number is 45

E:\5th sem\python\code>

6
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 7
AIM : Find the largest of n numbers, using a user defined function largest().
Coding :
def largest(list):
max=list[0]
for a in list:
if a>max:
max=a
return max
a=largest([10,3,3,4,5,6,90])
print(a)
OUTPUT :
E:\5th sem\python\code>python pr7.py
90
E:\5th sem\python\code>

7
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 8
AIM : Write a function myReverse() which receives a string as an input and
returns the reverse of the string.
Coding :
def revs(s):
str=""
for i in s:
str=i+str
return str
s=input("Enter the string : ")
print("Orginal string is : ",end="")
print (s)

print("reverse string : ",end="")


print(revs(s))

OUTPUT :
E:\5th sem\python\code>python pr8.py
Enter the string : surya prasad prajapati
Orginal string is : surya prasad prajapati
reverse string : itapajarp dasarp ayrus

E:\5th sem\python\code>

8
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 9
AIM : Check if a given string is palindrome or not.
Coding :
def a(s):
return s[::-1]
def b(s):
rev=a(s)
if s==rev:
return True
else:
return False
str=input("Enter the string for chaking palindrome or not : ")
print (str)
ans=b(str)
if ans==1:
print("This is palindrome ")
else:
print("This is not palindrome ")

OUTPUT :
E:\5th sem\python\code>python pr9.py
Enter the string for chaking palindrome or not : naman
naman
This is palindrome

E:\5th sem\python\code>python pr9.py


Enter the string for chaking palindrome or not : surya
surya
This is not palindrome

E:\5th sem\python\code>

9
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 10
AIM : WAP to convert Celsius to Fahrenheit
Coding :
c=float(input("Enter the temperature in celsius : "))
f=(c*9)/5+32
print(c,"celsius is : ",f," fahrenhite")

OUTPUT:
E:\5th sem\python\code>python pr10.py
Enter the temperature in celsius : 12.22
12.22 celsius is : 53.996 fahrenhite

E:\5th sem\python\code>python pr10.py


Enter the temperature in celsius : 37
37.0 celsius is : 98.6 fahrenhite

E:\5th sem\python\code>

10
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 11
AIM : Find the ASCII value of charades
Coding :
a=input("inter the string : ")
for i in a:
x=ord(i)
print("The ASCII value of Character ",i," is ",x)

OUTPUT :

E:\5th sem\python\code>python pr11.py


inter the string : surya prasad prajapati
The ASCII value of Character s is 115
The ASCII value of Character u is 117
The ASCII value of Character r is 114
The ASCII value of Character y is 121
The ASCII value of Character a is 97
The ASCII value of Character is 32
The ASCII value of Character p is 112
The ASCII value of Character r is 114
The ASCII value of Character a is 97
The ASCII value of Character s is 115
The ASCII value of Character a is 97
The ASCII value of Character d is 100
The ASCII value of Character is 32
The ASCII value of Character p is 112
The ASCII value of Character r is 114
The ASCII value of Character a is 97
The ASCII value of Character j is 106
The ASCII value of Character a is 97
The ASCII value of Character p is 112
The ASCII value of Character a is 97
The ASCII value of Character t is 116
The ASCII value of Character i is 105

E:\5th sem\python\code>

11
GOVERNMENT POLYTECHNIC AURAI BHADOHI
PRACTICAL :- 12
AIM : WAP for simple calculator
Coding :
def add(a,b):
return a+b
def sub(a,b):
return a-b
def multi(a,b):
return a*b
def divi(a,b):
return a/b
def modu(a,b):
return a%b

print("Please select option-\n"


"1. add\n"
"2. subtraction\n"
"3. multiply \n"
"4. divison \n"
"5. modulas\n")

select=int(input("Enter operations from 1,2,3,4,5\n"))


if select<6:
num1=int(input("Enter the first number : "))
num2=int(input("Enter the second number : "))
if select==1:
print(num1," + ",num2," = ",add(num1,num2))
elif select==2:
print(num1," - ",num2," = ",
sub(num1,num2))
elif select==3:
print(num1," * ",num2," = ",
multi(num1,num2))
elif select==4:
print(num1," / ",num2," = ",
divi(num1,num2))
elif select==5:
print(num1," % ",num2," = ",

12
GOVERNMENT POLYTECHNIC AURAI BHADOHI
modu(num1,num2))
else:
print("Please input valid number becase this is a invalid number ")

OUTPUT :
E:\5th sem\python\code>python pr12.py
Please select option-
1. add
2. subtraction
3. multiply
4. divison
5. modulas
Enter operations from 1,2,3,4,5
6
Please input valid number becase this is a invalid number
E:\5th sem\python\code>python pr12.py
Please select option-
1. add
2. subtraction
3. multiply
4. divison
5. modulas
Enter operations from 1,2,3,4,5
1
Enter the first number : 2
Enter the second number : 3
2 + 3 = 5
13
GOVERNMENT POLYTECHNIC AURAI BHADOHI
E:\5th sem\python\code>python pr12.py
Please select option-
1. add
2. subtraction
3. multiply
4. divison
5. modulas
Enter operations from 1,2,3,4,5
5
Enter the first number : 3
Enter the second number : 2
3 % 2 = 1
E:\5th sem\python\code>
E:\5th sem\python\code>python pr12.py
Please select option-
1. add
2. subtraction
3. multiply
4. divison
5. modulas
Enter operations from 1,2,3,4,5
4
Enter the first number : 45
Enter the second number : 5
45 / 5 = 9.0

14

You might also like