Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Python Final Record

Uploaded by

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

Python Final Record

Uploaded by

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

EX.

NO : 1 DIFFERENT NUMBER DATATYPE


DATE :25.4.2023

AIM:
To write a program to demonstrate different number datatypes in python.

ALGORITHM:
Step 1: Start the program.
Step 2: Initialize and read the variables: i ,c ,f ,s ,b
Step 3: Print the values and types of each variable:
Print "the value of c is:", i, '\nits type is:', type(i)
Print "the value of c is:", f, '\nits type is:', type(f)
Print "the value of c is:", c, '\nits type is:', type(c)
Print "the value of c is:", s, '\nits type is:', type(s)
Print "the value of c is:", b, '\nits type is:', type(b)
Step 4: Stop the program.

CODING:

i=7 ;c=24+8j ;f=701 ; b= True


s='HELLO EVERYONE!!\nThis is john\'s python programming..'
print("the value of c is:",i,'\nits type is:',type(i))
print("the value of c is:",f,'\nits type is:',type(f))
print("the value of c is:",c,'\nits type is:',type(c))
print("the value of c is:",s,'\nits type is:',type(s))
print("the value of c is:",b,'\nits type is:',type(b))
1
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:
Thus the program to demonstrate different number datatypes in python
was executed successfully.

2
EX.NO : 2 ARITHMETIC OPERATION
DATE:2.5.2023
AIM:
To write a program to perform different arithmetic operations on numbers
in python.

ALGORITHM:
Step 1: Start the program.
Step 2: Initialize variables a and b with values 10 and 3, respectively.
Step 3: Print "addition of a:{a} & b: {b} is: {a + b}"
Step 4: Print "subtraction of a: {a} & b: {b} is: {a - b}"
Step 5: Print "multiplication of a: {a} & b: {b} is: {a * b}"
Step 6: Print "division of a: {a} & b: {b} is: {a / b}"
Step 7: Print "floor division of a: {a} & b: {b} is: {a // b}"
Step 8: Print "modulus of a: {a} & b: {b} is: {a % b}"
Step 9: Stop the program.

CODING:

a=10; b=3

print("addition of a:",a,"&b:",b,"is:",a+b)

print("subtraction of a:",a,"&b:",b,"is:",a-b)

print("multiplication of a:",a,"&b:",b,"is:",a*b)

print("division of a:",a,"&b:",b,"is:",a/b)
print("floor division of a:",a,"&b:",b,"is:",a//b)

print("moduli of a:",a,"&b:",b,"is:",a%b)
print("exponent of a:",a,"&b:",b,"is:",a**b)

3
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the program to perform different arithmetic operations on numbers


in python was executed successfully.

4
EX.NO : 3 CONCATENATE A STRING
DATE:9.5.2023

AIM:
To Write a program to create, concatenate and print a string and
accessing sub- string from a given string.

ALGORITHM:
Step 1: Start the program.
Step 2: Initialize a pi with the value 3.14 and s and v with the value.
Step 3: Print "the value of s is: {s}","the value of v is: {v}".
Step 4: Concatenate the strings s and v.
Step 5: Print "after concatenating s and v the string is: {string_add}".
Step 6: Create a string "The value of pi is " followed by the string pi.
Step 7: Print the text string.
Step 8: Stop the program.

CODING:
pi=3.14
s= "Venkata" ;v= "Subhramanyam"
print("the value of s is:",s)
print("the value of v is:",v)
string_add = s+v
print("after concatenating s and v the string is:",s+v)
text = 'The value of pi is ' + str(pi)
print(text)

5
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the program to create, concatenate and print a string and accessing
sub- string from a given string was executed successfully.

6
EX.NO : 4 PRINT THE TIME FORMAT
DATE: 16.5.2023

AIM:
To write a python script to print the current date in following format
“Sun May 29 02:26:23 IST 2017”

ALGORITHM:

Step 1: Start the program.


Step 2: Import the 'time' and 'datetime' module.
Step 3: Assign the current date and time using x=
datetime.datetime.now()
Step 4: Pass the "%c" format specifier to strftime to represent the date
and time in a locale-specific format.
Step 6: Print the formatted date and time using the print function.
Step 7: Stop the program.

CODING:

import time
import datetime
x =datetime.datetime.now()
print(x.strftime("%c"))

7
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python script to print the current date in following format “Sun
May 29 02:26:23 IST 2017” was executed successfully.

8
EX.NO : 5 APPEND AND REMOVE LIST
DATE: 24.5.2023

AIM:
To write a python program to create, append and remove lists in python.

ALGORITHM:

Step 1: Start the program


Step 2: Create an empty list using the square brackets: my_list = [].
Step 3: Use the append() method to add items to the end of the list
Step 4: Use the remove() method to remove a specific item from the list
Step5: Display the list
Step6: stop the program

CODING:

colleges = ["SIIET", "GNIT", "AVN"]


print(colleges)
colleges.append("MVSR")
del colleges[1]
print(colleges)

9
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to create, append and remove lists in python
was executed successfully.

10
EX.NO : 6 WORKING WITH TUPLES
DATE: 26.5.2023

AIM:
To write a program to demonstrate working with tuples in python.

ALGORITHM:

Step1: start the program


Step 2: Define the Tuple
Step 3: Check for Element
Step 4: If present print yes
Step 4.1: else print no
Step 5: Run the program.
Step 6: Stop the program.

CODING:

colleges = ("SIIET","BHARAT","GNIT", "AVN")


print("the lists in colleges tuple is",colleges)
print("we can\'t add or remove new elements in a tuple")
print("length of the tuple colleges is:",len(colleges))
if "SIIET" in colleges:
print("Yes, 'SIIET' is in the colleges tuple")

11
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the program to demonstrate working with tuples in python was


executed successfully.

12
EX.NO : 7 WORKING WITH DICTIONARY
DATE : 31.5.2023

AIM:
To write a program to demonstrate working with dictionaries in
python

ALGORITHM:

Step 1: start the program


Step 2: Creating a Dictionary
Step 3: Changing Index (Updating Values)
Step 4: Finding Length.
Step 5: Copy the dictionary: new_dict = my_dict.copy()
Remove a key-value pair: del my_dict['key_to_remove']
Step 6: stop the program

CODING:

for SIIET college = {


"name": "siiet",
"code": "INDI",
"id": "x3"
}

13
print(college)
college["location"] = "IBP"
print(college)
college["location"] = "sheriguda"
print(college)
use pop()
college.pop("code")
print(college)
print("length of college is:",len(college))
mycollege= college.copy()
print(mycollege)

14
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the program to demonstrate working with dictionaries in python


was executed successfully.

15
EX.NO : 8 LARGEST OF THREE NUMBERS
DATE : 7.6.2023
AIM:
To write a python program to find largest of three numbers.

ALGORITHM:
Step 1: start the program
Step 2: Input three numbers: num1, num2, and num3.
Step 3: Compare the three numbers using statements (if, elif, else).
Step 4: Print the largest number. print("The largest number is:", largest).
Step 5: stop the program.

CODING:
larger def bigOf3(a,b,c):
if(a>b):
if(a>c):
print("a is greater than b and c")
else:
print("c is greater than a and b")
elif(b>c):
print("b is greater than a and c")
else:
print("c is greater than a and b")
txt= input("enter a,b,c values:")
a,b,c= txt.split()
bigOf3(int(a),int(b),int(c))

16
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to find largest of three numbers was executed
successfully.

17
EX.NO : 9 CONVERT CELSIUS TO FAHRENHEIT
DATE : 14.6.2023

AIM:
To write a python program to convert Celsius to Fahrenheit
ALGORITHM:
Step 1: start the program .
Step 2: Read the temperature in Celsius (C)
Step 3: Calculate Fahrenheit using : F = (C * 9/5) + 32 and display it.
Step 4: stop the program .
CODING:
while(1):
choice=input("ENTER YOUR CHOICE:")
ch=int(choice)
if(ch==1):
c=int(input("ENTER TEMPERATURE IN CELSIUS:"))
f=((9*c)/5)+32
print("converted temperature is:",f)
elif(ch==2):
f=int(input("ENTER TEMPERATURE IN FAHRENHEIT:"))
c=((f-32)/9)*5
print("converted temperature is:",c)
elif(ch==3):
exit()
else:
print("wrong choice")

18
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to convert temperature to and from Celsius to


Fahrenheit was executed successfully.

19
EX.NO : 10 PATTERN USING NESTED FOR LOOP
DATE : 21.6.2023

AIM:
To write a python program to construct the following pattern using
nested for loop.

ALGORITHM:

Step1: Start the program.


Step 2: Read the number of rows for the pattern (n)
Step 3: Loop from i = 1 to n
Step 4: Print i number of stars followed by a new line
Step 5: Loop from i = n - 1 to 1
Step 6: Print i number of stars followed by a new line
Step 7: Stop the program.

CODING:

n=int(input("ENTER A VALUE:"))
for x in range(0,n+1,1):
print(x*'*')
if(x==n):
for x in range(n,0,-1):
print(x*'*')

20
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to construct the following pattern using nested
for loop was executed successfully.

21
EX.NO : 11 PRINT THE PRIME NUMBERS LESS

DATE : 28.6.2023 THAN 20

AIM:
To write a python program to print prim numbers less than 20.

ALGORITHM:

Step1: Start the program.


Step 2: Loop from num = 2 to 19 (inclusive)
Step 3: Set is_prime = True
Step 4: Loop from divisor = 2 to the square root of num
Step 5: If num is divisible by divisor, set is_prime = False and break
Step 6: If is_prime is True, print num
Step7: Start the program.

CODING:
n=int(input("enter range of prime numbers:"))
for num in range(2,n+1):
count=0
for i in range(2,num//2+1):
if(num%i==0):
count=count+1
if(count==0):
print(num)

22
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to print prim numbers less than 20 was executed
successfully.

23
EX.NO : 12 FACTORIAL OF A NUMBER USING
DATE : 5.7.2023 RECURSION

AIM:
To write a python program to find factorial of a number using recursion

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the input number (n)
STEP 3: Define a function factorial(n)
STEP 3.1: If n is 0 or 1, return 1
STEP 3.2: Otherwise, return n times factorial of (n - 1)
STEP 4: Call the factorial function with n as input
STEP 5: Display the result
STEP 6: Stop the program.

CODING:
def recursion(n):
if(n<1):
print("FACTORIAL NOT POSSIBLE!!")
elif(n>1):
return n*recursion(n-1)
else:
return 1
n=int(input("enter a number:"))
print("factorial of",n,"is:",recursion(n))

24
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to find factorial of a number using recursion


was executed successfully.

25
EX.NO : 13 RIGHT ANGLED TRIANGLE
DATE : 12.7.2023
AIM:
To write a python program indicate not the triangle is a right- angled
triangle
ALGORITHM:
Step 1: Start the program.
Step 2: Read the lengths of the three sides of the triangle (a, b, c)
Step 3: Sort the sides in ascending order: sides = [a, b, c]
Step 4: Calculate sum_of_short_sides = sides[0]^2 + sides[1]^2
Step 5: Calculate : longest_side_squared= sides[2]^2
Step 6: If sum_of_short_sides equals longest_side_squared, then
Step 6.1: Print "The triangle is a right-angled triangle."
Step 7: Else, Print "The triangle is not a right-angled triangle."
Step 8: Stop the program.

CODING:
a=float(input("enter length of hypotenuse side:"))
b=float(input("enter length of base side:"))
c=float(input("enter length of height side:"))
def pythagorean(a,b,c):
a=a*a; b=b*b; c=c*c
if(a==b+c):
print("yes!! the given inputs are triplets of a right angled triangle!!")
print("height:",c**0.5,"\nbase:",b**0.5,"\nhypotenuse:",a**0.5)
pythagorean(a,b,c)

26
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python the program should indicate whether or not the triangle
is a right- angled triangle was executed successfully.

27
EX.NO : 14 FIBONACCI SERIES
DATE : 19.7.2023

AIM:
To write a python program to define a module to find Fibonacci
Numbers and import the module to another program.

ALGORITHM:

Step 1: Start the program.


Step 2: Import the FibonacciModule module
Step 3: Read the input for the Fibonacci number index (n)
Step 4: Call fibonacci function from module to get the nth Fibonacci number
Step 5: Display the result
Step 6: Stop the program.

CODING:

fibonacci.py
def fibonacci(n):
n1=0; n2=1;
print(n1)
print(n2)
for x in range(0,n):
n3=n1+n2

28
if(n3>=n):
break;
print(n3,end = ' ')
n1=n2
n2=n3
using_fibonacci.py
import fibonacci
n=int(input("enter range:"))
if(n<0):
print("enter correct range!!")
else:
print(" FIBONACCI SERIES \n")
fibonacci.fibonacci (n)

29
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:
Thus the python program to define a module to find Fibonacci
Numbers and import the module to another program was executed successfully.

30
EX.NO : 15 TO DEFINE A MODULE AND
DATE :26.7.2023 IMPORT A SPECIFIC FUNCTION

AIM:
To write a python program to define a module and import
a specific function in that module to another program.

ALGORITHM:

Step 1: Start the program.


Step 2: Import specific function my_function from MyModule module
Step 3: Read the input for the parameter
Step 4: Call the imported my_function with the input parameter
Step 5: Display the result
Step 6: Stop the program.

CODING:

def fibonacci(n):
n1=0; n2=1;
print(n1)
print(n2)
for x in range(0,n):
n3=n1+n2
if(n3>=n):
break;
print(n3,end = ' ')
31
n1=n2
n2=n3
n=int(input("enter range:"))
if(n<0):
print("enter correct range!!")
else:
print(" FIBONACCI SERIES \n")
fibonacci (n)

32
OUTPUT:

RE5¥ART: C:\Dsero\dell\Oeskrop\pyzAoo programs\usiog fibooacci.py


emzez rasoge :3O
-------------------------------FIBDNACCI BERIES--------------------------------

5
21

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the python program to define a module and import specific


function in that module to another program was executed successfully.

33
EX.NO : 16 COPY FILE
DATE :28.7.2023

AIM:
To write a script named copyfile.py. . The contents of the first
file should be input and written to the second file.

ALGORITHM:

Step 1: Start the program.


Step 2: Open the input file as file.
Step 3: Open the output file in write mode.
Step 4: For line input: output erite(line).
Step 5: Print "JOB DONE"
Step 6: Stop the program.

CODING:

with open("input.txt") as input:


with open("output.txt","w") as output:
for line in input: output.write(line)
print("JOB DONE!!")

34
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus a script named copyfile.py. The contents of the first file


should be input and written to the second file was executed successfully.

35
EX.NO: 17 TO PRINT ALL OF THE UNIQUE
DATE: 2.8.2023 WORDS

AIM:
To write a program that inputs a text file. The program should print all of
the unique words in the file in alphabetical order

ALGORITHM:

Step 1: Start the program.


Step 2: Open the specified file for reading.
Step 3: Iterate through each line in the opened file:
Step 3.1: Remove spaces from the line using the rstrip() method.
Step 3.2: Split the line into words using the split() method.
Step 4: If the element is already present, skip to the next iteration.
Step 5: Else append it to our_list and sort elements in alphabetical order.
Step 6: Print the sorted our_list containing unique words.
Step 7: Stop the program.

CODING:

fname=input("enter file name with correct extension:")


file_opened=open(fname)
our_list=list()
for line in file_opened:

36
word=line.rstrip().split()
for element in word:
if element in our_list:
continue
else:
our_list.append(element)
our_list.sort()
print(our_list)

37
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the program that inputs a text file. The program should print
all of the unique words in the file in alphabetical order was executed
successfully.

38
EX.NO : 18 CONVERT AN INTEGER TO A
DATE : 4.8.2023 ROMAN NUMERAL

AIM:
To write a Python class to convert an integer to a roman numeral.

ALGORITHM:

Step 1: Start the program.


Step 2: Define a class named roman_solution.
Step 3: Inside the class, define a method named int_to_Roman(num):
Step 3.1: Create two lists val and syb that correspond to the integer values
and their corresponding Roman numeral symbols.
Step 3.2: Initialize an empty string roman_num to store the Roman numeral
representation.
Step 3.3: Initialize an index i to 0.
Step 3.4: Check if the input number num is less than 1 or greater than 3999.
If it is, print an error message.
Step4: if the number is within the valid range:
Step 4: 1Enter a while loop that continues until num becomes 0.
Step 4.2: Check if subtracting the value at index i from num results in a
non-negative value:
Step 4.3: f yes, add the corresponding Roman numeral symbol at index i to
roman_num, and subtract the value at index i from num.
Step5: Outside the class definition:
Step 5.1: Prompt the user to input an integer value n.
Step 5.2: Create an instance of the roman_solution class.
Step 5.3: Call the int_to_Roman() method of the instance, passing the input
value n, and print the result.
Step 6: Stop the program.

39
CODING:

class roman_solution:
def int_to_Roman(num):
val = [1000, 900, 500, 400, 100, 90, 50, 40, 9, 5, 4, 1]
syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL","X", "IX", "V", "IV",
"I” ]
roman_num = “”

i=0
if(n<1 or n>3999):
print("ENTER A GOOD VALUE!")
else:
while num > 0:
if(num-val[i]>=0):
roman_num+=syb[i]
num-=val[i]
else:
i+=1
return roman_num
n=int(input("ENTER A NUMBER:"))
print("roman numeral of given number is:",roman_solution.int_to_Roman(n))

40
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the Python class to convert an integer to a roman numeral was


executed successfully.

41
EX.NO : 19 TO IMPLEMENT POW(x,n)
DATE : 9.8.2023

AIM:
To write a Python class to implement pow(x, n)

ALGORITHM:
Step1: Start the program.
Step2: Define a class named py_power and define a method named
power(x, n):
Step2.1: Print the values of x and n.
Step 2.2: Calculate the result of raising x the power n using the operator
**.
Step 2.3: Print the calculated result.
Step 3: Prompt the user to input the x (base) value as a floating-point
number.
Step 3.1: Prompt the user to input the n (power) value floating-point
number.
Step 4: Create an instance of the py_power class.
Step 4.1: Call the power() method of the instance, passing the input
values for x and n
Step 5:Stop the program.

CODING:
class py_power:
def power(x,n):
print("power of given literals:\nx:",x,"\nn\n:",n,"is:",x**n)
x=float(input("ENTER X(BASE) VALUE:"))
n=float(input("ENTER N(POWER) VALUE:"))
py_power.power(x,n)

42
OUTPUT:

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the Python class to implement pow(x, n) was executed successfully.

43
EX.NO : 20 REVERSE A STRING WORD
DATE : 11.8.2023 BY WORD

AIM:
To write a Python class to reverse a string word by word.

ALGORITHM:
Step 1: Start the program.
Step 2: Initialize the f name variable with a sentence.
Step 3: Split the sentence into words using the split () method.
Step 4: Iterate each element in the word list: Append the element to the
our list
Step 5: Print the original sentence list: "tried sentence is:" followed by
our_list.
Step 6: Reverse the elements in our_list using the reverse() method.
Step 7: Print list after reversal: "list after the reverse()" by the our_list.
Step 8: Stop the program.

CODING:
fname="HELLO EVERYONE THIS IS PYTHON PROGRAMMING AND
WE'RE PLAYING WITH LISTS"
our_list=list()
word=fname.split()
for element in word:
our_list.append(element)
print("tried sentence is:",our_list)
our_list.reverse()
print("list after the reverse()",our_list)

44
OUTPUT:
tried sentence is: ['HELLO']
tried sentence is: ['HELLO', 'EVERYONE']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING', 'AND']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING', 'AND', "WE'RE"]
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING', 'AND', "WE'RE", 'PLAYING']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING', 'AND', "WE'RE", 'PLAYING', 'WITH']
tried sentence is: ['HELLO', 'EVERYONE', 'THIS', 'IS', 'PYTHON',
'PROGRAMMING', 'AND', "WE'RE", 'PLAYING', 'WITH', 'LISTS']
list after the reverse() ['LISTS', 'WITH', 'PLAYING', "WE'RE", 'AND',
'PROGRAMMING', 'PYTHON', 'IS', 'THIS', 'EVERYONE', 'HELLO']

CLASS PERFORMANCE
RECORD
VIVA
TOTAL

RESULT:

Thus the Python class to reverse a string word by word was executed
successfully.
45

You might also like