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

Pythonlabfilemk

Python practical of 7th sem

Uploaded by

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

Pythonlabfilemk

Python practical of 7th sem

Uploaded by

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

Python Programming

Btec-907D-18

Electronics And Communication Engineering


7th Semester

Submitted To:Pradeep K.Gaur Submitted By:Mithlesh Kumar


2102001
ECE-A2

1
PROGRAM-1
1. AIM: Write a program to program to demonstrate number data types in Python.

Software: Vs Code (Python 3.10.0)


ALGORITHM:
Step 1-Take one integer and other float datatypes
Step 2-Print both of them using type() function to know which class a variable or a value belongs to.
Step 3- Similarly, the isinstance() function is used to check if an object belongs to a particular class.
Step 4-So result will be printed

Source code:
a=5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j print(a, "is complex number?", isinstance(1+2j,complex),”muskan


kumari”)

OUTPUT:

PROGRAM-2

2. AIM: Write a program to perform different arithmetic operations on numbers in python

Software: Vs Code (Python 3.10.0)

2
ALGORITHM:
Step 1-Take two input integers .
Step2- Addition operation using + operator, a + b adds 2 numbers.
Step3- Subtraction operation using - operator, a - b right hand operand from left hand operand.
Step4- Multiplication operation using * operator, a * b multiplies 2 numbers.
Step5- Division operation using / operator, a / b divides left hand operand by right hand operand.
Step6- Floor Division operation using // operator, a // b divides left hand operand by right hand operand, here it
removes the values after decimal point.
Step7- Modulus % operator when applied returns the remainder when left hand operand is divided by right hand
operand a % b.
Step8- Exponential operation using ** operator, a ** b returns value of a b
Step9- Print the result of each operation.

Source code:

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

print("substraction 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 divison 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)

OUTPUT:

PROGRAM-3
3. AIM: Write a program to create, concatenate and print a string and accessing sub-string from a given
string in python.

3
Software: Vs Code (Python 3.10.0)

ALGORITHM:
Step 1-Take input of two strings
Step 2-Then print the two strings as output.
Step 3-Concateniate the two strings using + operator.
Step 4- Print the substring using slicing by specify the starting index [1] and the ending index[6] of the substring

Source code:
s1=input("Enter first String : ");
s2=input("Enter second String : ");
print("First string is : ",s1); print("Second
string is : ",s2); print("concatenations of two
strings :",s1+s2); print("Substring of given
string :",s1[1:4]);

OUTPUT:

PROGRAM-4

4
4. AIM: Write a python script to print the current date in the following format “Sun May 29 02:26:23 IST
2017 in python.

Software: Vs Code (Python 3.10.0)

ALGORITHM:
Step 1- Import the library time
Step 2- then make a variable and assign localtime() which will give local time zone time.localtime()
method of Time module is used to convert a time expressed in seconds Step 3-Strftime() method used to
create formatted strings like:
%a : Abbreviated weekday name.
%b : Abbreviated month name.
%d : Day of the month as a decimal number [01,31].
%H : Hour (24-hour clock) as a decimal number [00,23].
%M : Minute as a decimal number [00,59].
%S : Second as a decimal number [00,61].
%Z : Time zone name (no characters if no time zone exists).
%Y : Year with century as a decimal number.
Step 4-Exit

Source code: import


time;
ltime=time.localtime()
;
print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime),”muskan kumari”);

OUTPUT:

5
Program-5
AIM: Write a program to create, append and remove lists in python. Software:
Vs Code (Python 3.10.0)

Algorithm:

Step 1:Create two lists using the[] square brackets separated by commas

Step 2:Print both lists.

Step 3:Add both the lists by using + operator and create animals list

Step 4:Remove the snakes list from the list animals y using remove function.

Step 5:Exit the program

Source Code:
pets = ['cat', 'dog', 'rat', 'pig', 'tiger']
snakes=['python','anaconda','fish','cobra','mamba'
] print("Pets are :",pets) print("Snakes are
:",snakes) animals=pets+snakes print("Animals
are :",animals) snakes.remove("fish") print("new
list by muskan kumari") print("updated Snakes
are :",snakes)

Output:

Program-6
AIM: Write a program to demonstrate working with tuples in python.
Software: Vs Code (Python 3.10.0)

Algorithm:

6
Step 1:Create a list and put values.

Step 2:Then print the first index of list and 3 to 6 index of list.

Step 3: Use for loop and putting condition using if in list

Step 4: If element present print yes otherwise no statement then it will move to length of the list

Step 5: Exit.

Source Code:
T = ("apple", "banana", "cherry","mango","grape","orange")
print("\n Created tuple is :",T) print("\n Second fruit is
:",T[1]) print("\n From 3-6 fruits are :",T[3:6])
print("\n List of all items in Tuple :") for x in T:
print(x) if "apple" in T: print("\n Yes, 'apple' is in the
fruits tuple") print("\n Length of Tuple by muskan
kumari :",len(T))

Output:

Program-7
AIM: Write a program to demonstrate working with dictionary in
python. Software: Vs Code (Python 3.10.0) Algorithm:
Step1 : Enter the employee data in the list(i.e. Name, age , salary , company name).
Step 2 : Print the employee type.
Step3 : Print the statement of inserting employee data and print it.
Step4 : Now print delete some of the employee data.
Step5: Delete Name and Company Name from the list and print the modified information.

7
Step6 : Now try to delete the whole dictionary(i.e. Employee).
Step7 : Now print the statement and print employee.
Step8 : Output of the program.
Program:
Employee={“Name”: “Adarsh”, “Age”:20, “Salary”:500000, “Company”:
“GOOGLE”} print(type(Employee)) print(“Printing Employee data….”)
print(Employee) print(“printing employee data”) print(“Name : %s”
%Employee[“Name”]) print(“Age : %d” %Employee[“Age”]) print(“Company : %s”
%Employee[“Company”]) print(“Deleting some of the employee data”) del
Employee[“Name”] del Employee[“Company”]
print(“printing the modified information”)
print(Employee) print(“Deleting the
dictionary : Employee”); del Employee
print(“Let’s try to print it again”);
print(Employee)
Output:

Program-8
AIM: Write a python program to print largest of three numbers.
Software: Vs Code (Python 3.10.0)

Algorithm:

Step1: Ask the user to enter three integer values.

Step2: Check if num1 is greater than num2.

Step 3: If true, then check if num1 is greater than num3.

a. If true, then print ‘num1’ as the greatest number.


b. If false, then print ‘num3’ as the greatest number.

Step 4: If false, then check if num2 is greater than num3.

a. If true, then print ‘num2’ as the greatest number.

8
b. If false, then print ‘num3’ as the greatest number.

Source Code:

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number:

")) num3 = int(input("Enter third number:

")) if (num1 >= num2) and (num1 >=

num3): largest = num1 elif (num2 >=

num1) and (num2 >= num3):

largest = num2

else:

largest = num3 print("The largest number is said by muskan kumari

Vs code", largest) Output:

Program 9
Aim: Write a Python program to convert temperatures to and from Celsius, Fahrenheit. Software:
JUPYTER NOTEBOOK (Python 3.8.0)

Source Code:

celsius_1 = float(input("Temperature value in degree Celsius: " ))


Fahrenheit_1 = (celsius_1 * 1.8) + 32
print('The %.2f degree Celsius is equal to: %.2f Fahrenheit' %(celsius_1, Fahrenheit_1))
Fahrenheit_2 = float(input("Temperature value in degree Fahrenheit: " ))
Celsius_2= ((Fahrenheit_2-32)*5)/9 print('The %.2f degree Fahrenheit is equal to: %.2f
Celsius' %( Fahrenheit_2, Celsius_2))

9
Output:

Program 10
Aim: Write a Python program to construct the following pattern, using a nested for loop
*
**
***
****
***
**
*

Software: Vs Code (Python 3.10.0)

Algorithm
Step1:Take input as n=5 already declared
Step2:Take the range till n and The iteration of the inner for loop depends on the outer loop. The inner loop is
responsible to print the number of columns.
Step3: In the first iteration, the value of i is 0, and it increased by 1, so it becomes 0+1, now inner loop iterated first
time and print one star(*).

10
Step4. The end argument prevents to jump into another line. It will printer the star until the loop is valid.
Syep5:The last print statement is responsible for ending the line after each row.AD

Source Code: n=5;

for i in range(n):
for j in range(i):
print ('* ', end="")
print('')

for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')

Output :

11
Program 11
Aim: Write a Python script that prints prime numbers less than 20 Software:
Vs Code (Python 3.10.0)

Algorithm
Step1: Print "Prime numbers between 1 and 20 are".
Step2:Store value 20 in a variable ultmt.
Step3: Run a loop from 0 to 20
Step4: Check if num is greater than 1
Step5 : If true run a loop from 2 to num a)
Check if (num % i) == 0
b) If true breakout of the loop Step6:
Else print num.

Source Code:

print("Prime numbers between 1 and 20 are:") ulmt=20; for num in


range(ulmt): if num > 1: for i in range(2,num): if (num
% i) == 0: break else:
print(num)

Output :

Program 12
Aim: Write a python program to find factorial of a number using Recursion
Software: Vs Code (Python 3.10.0)

Algorithm
Step1: Create a function to return the factorial of a number using recursion.

12
Step2:Take input from the user to calculate factorial.
Step 3: Check if the num is negative
Step 4: If true then print "Sorry, factorial does not exist for negative numbers".
Step 4: If false, then check if the num is zero.
Step5: If true then print "The factorial of 0 is 1".
Step6: If false then call the function fact

Source Code: def


recur_factorial(n):
if n == 1:
return n else:
return n*recur_factorial(n-1) #
take input from the user num =
int(input("Enter a number: ")) #
check is the number is negative 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 :

Program 13
Aim: Write a python program to that accepts length of three sides of a triangle as inputs. The program should
indicate whether or not the triangle is a right- angled triangle (use Pythagorean theorem).

Software: Vs Code (Python 3.10.0)

ALGORITHM:

Step1: Read input for hypotenuse side, base side and height
side. Step2: Call pythagorean(a,b,c) pythagorean(a,b,c)
Step1: Initialize a,b,c as square of the sides.

13
Step2: if a == b+c print yes
Step3: Else print no

Source Code:

x=float(input("'enter length of hypotenuse


side:")) y=float(input("enter length of base
side:")) z=float(input("enter length of height
side:")) def pythagoreanthm(x,y,z):
x,y,z=x*x,y*y,z*z if(x==y+z): print("yes!! the given inputs
are triplets of a right angled triangle!!") else: pass
pythagoreanthm(x,y,z) # calling function

OUTPUT:

Program 14
Aim: Write a python program to define a module to find Fibonacci Numbers and import the module to another
program.

Software: Vs Code (Python 3.10.0)

Algorithm
Step 1: In a python(.py) file define a function for printing Fibonacci series
Step 2: Import that module using import function
Step 3: Take the input from user
Step 4: Call the Fibonacci function and return Fibonacci series

14
Source Code:

def fib(n): a, b =
0, 1 while b < n:
print(b, end =" ")
a, b = b, a+b

#import fibonacci module


import fibonacci
num=int(input("Enter any number to print Fibonacci series: "))
fibonacci.fib(num) Output :

Program 15
Aim:
Write a python program to define a module and import a specific function in that module to another program.

Software: Vs Code (Python 3.10.0)

Algorithm
Step 1: In a python(.py) file define four functions for printing result after Arithmetic Operations.
Step 2: Import the required function from that file using import function Step 3: Call the

15
function by importing module calculator in another file. Step 5: Call the function to get the
required result in another file
Source Code:

#creation of module
Calculator def
add(num1,num2):
print(num1+num2) def
subtraction(num1,num2):
print(num1-num2) def
mult(num1,num2):
print(num1*num2) def
div(num1,num2):
print(num1/num2)
#Calling function add in another file using Calculator module
from Calculator import add,subtract subtract(7,9)
add(9,9)

Output

16
Name of Student :Mithlesh Kumar Name of Faculty :Dr.Pradeep K.Gaur
Roll No:2102001
Subject(with Code) : __________ Name of Lab Instructor :_____________
Semester/Group) : ___________
Marks
Page LW FW VV Total
Sr No. Name of Experiment No. Date 12 3 3 Marks

17

You might also like