Enrolment No. – 0103IT191082 Class Roll No.
– 81
Lakshmi Narain College of Technology, Bhopal
IT-605
Programming in Python
Submitted by: Submitted to:
Pranjali Prakash Prof. Pushpendra Singh Tomar
VI semester B section
III year
[1]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Index
S.No. Experiments Date of Date of
Experiment Submission
1. Write a program to demonstrate
different number datatypes in
python
2. Write a program to perform
different arithmetic operations
on numbers in python
3. Write a program to create,
concatenate and print a string
and accessing sub-string from a
given string
4. Write a python script to print
the current date in following
format “Sun May 29 02:26:23
IST 2017”
5. Write a python program to
create, append, and remove lists
in python
6. Write a program to demonstrate
working with tuples in python
7. Write a program to demonstrate
working with dictionaries in
python
8. Write a python program to find
largest of three numbers
9. Write a python program to
convert temperature to and
from Celsius to Fahrenheit
10. Write a python program to print
prime numbers less than 20
[2]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-1
Aim: A program to demonstrate different number datatypes in python.
Theory:
Number data types store numeric values. Number objects are created when you
assign a value to them. For example −
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del statement.
The syntax of the del statement is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement.
For example −
del var
del var_a, var_b
Python supports four different numerical types −
int (signed integers)
long (long integers, they can also be represented in octal and
hexadecimal)
float (floating point real values)
complex (complex numbers)
Program:
a=10; #Integer Datatype
b=11.5; #Float Datatype
c=2.05j; #Complex Number
print("a is Type of",type(a)); #prints type of variable a
[3]
Enrolment No. – 0103IT191082 Class Roll No. – 81
print("b is Type of",type(b)); #prints type of variable b
print("c is Type of",type(c)); #prints type of variable c
Output:
[4]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-2
Aim: A program to perform different arithmetic operations on numbers in
python.
Program:
a=int(input("Enter a value")); #input() takes data from console at
runtime as string.
b=int(input("Enter b value")); #typecast the input string to int.
print("Addition of a and b ",a+b);
print("Subtraction of a and b ",a-b);
print("Multiplication of a and b ",a*b);
print("Division of a and b ",a/b);
print("Remainder of a and b ",a%b);
print("Exponent of a and b ",a**b); #exponent operator (a^b)
print("Floor division of a and b ",a//b); # floor division
Output:
[5]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-3
Aim: Write a program to create, concatenate and print a string and accessing
sub-string from a given string
Program:
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:
[6]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-4
Aim: Write a python script to print the current date in following format “Sun
May 29 02:26:23 IST 2017”
Program:
import time;
ltime=time.localtime();
print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime)); #returns the
formatted time
'''
%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.'''
Output:
[7]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-5
Aim: Write a python program to create, append, and remove lists in python
Program:
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("updated Snakes are :",snakes)
Output:
[8]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-6
Aim: Write a program to demonstrate working with tuples in python.
Program:
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 is :",len(T))
Output:
[9]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-7
Aim: Write a program to demonstrate working with dictionaries in python.
Program:
dict1 = {'StdNo':'532','StuName': 'Naveen', 'StuAge': 21, 'StuCity':
'Hyderabad'}
print("\n Dictionary is :",dict1)
#Accessing specific values
print("\n Student Name is :",dict1['StuName'])
print("\n Student City is :",dict1['StuCity'])
#Display all Keys
print("\n All Keys in Dictionary ")
for x in dict1:
print(x)
#Display all values
print("\n All Values in Dictionary ")
for x in dict1:
print(dict1[x])
#Adding items
dict1["Phno"]=85457854
#Updated dictionary
print("\n Updated Dictionary is :",dict1)
#Change values
dict1["StuName"]="Madhu"
#Updated dictionary
print("\n Updated Dictionary is :",dict1)
#Removing Items
dict1.pop("StuAge");
#Updated dictionary
print("\n Updated Dictionary is :",dict1)
#Length of Dictionary
print("Length of Dictionary is :",len(dict1))
#Copy a Dictionary
dict2=dict1.copy()
#New dictionary
print("\n New Dictionary is :",dict2)
#empties the dictionary
dict1.clear()
print("\n Updated Dictionary is :",dict1)
[10]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Output:
[11]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-8
Aim: Write a python program to find largest of three numbers.
Program:
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",largest)
Output:
[12]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-9
Aim: Write a python program to convert temperature to and from Celsius to
Fahrenheit.
Program:
print("Options are \n")
print("1.Convert temperatures from Celsius to Fahrenheit \n")
print("2.Convert temperatures from Fahrenheit to Celsius \n")
opt=int(input("Choose any Option(1 or 2) : "))
if opt == 1:
print("Convert temperatures from Celsius to Fahrenheit \n")
cel = float(input("Enter Temperature in Celsius: "))
fahr = (cel*9/5)+32
print("Temperature in Fahrenheit =",fahr)
elif opt == 2:
print("Convert temperatures from Fahrenheit to Celsius \n")
fahr = float(input("Enter Temperature in Fahrenheit: "))
cel=(fahr-32)*5/9;
print("Temperature in Celsius =",cel)
else:
print("Invalid Option")
Output:
[13]
Enrolment No. – 0103IT191082 Class Roll No. – 81
Experiment-10
Aim: Write a python program to print prime numbers less than 20.
Program:
print("Prime numbers between 1 and 20 are:")
ulmt=20;
for num in range(ulmt):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output:
[14]