Pythonlabfilemk
Pythonlabfilemk
Btec-907D-18
1
PROGRAM-1
1. AIM: Write a program to program to demonstrate number data types in Python.
Source code:
a=5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
OUTPUT:
PROGRAM-2
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("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.
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
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 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.
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 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:
8
b. If false, then print ‘num3’ as the greatest number.
Source Code:
largest = num2
else:
Program 9
Aim: Write a Python program to convert temperatures to and from Celsius, Fahrenheit. Software:
JUPYTER NOTEBOOK (Python 3.8.0)
Source Code:
9
Output:
Program 10
Aim: Write a Python program to construct the following pattern, using a nested for loop
*
**
***
****
***
**
*
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
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:
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
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).
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:
OUTPUT:
Program 14
Aim: Write a python program to define a module to find Fibonacci Numbers and import the module to another
program.
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
Program 15
Aim:
Write a python program to define a module and import a specific function in that module to another program.
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