Week1.py: Write A Program To Demonstrate Different Number Data Types in Python
Week1.py: Write A Program To Demonstrate Different Number Data Types in Python
Experiment 1
Write a program to demonstrate different number data types in Python.
Source Code:
week1.py
Output:
Experiment 2:
Write a program to perform different Arithmetic Operations on numbers in Python.
Source Code:
week2.py
'''Aim:Write a program to perform different Arithmetic Operations on numbers in Python.'''
Output:
2
Experiment 3:
Write a program to create, concatenate and print a string and accessing sub-string from a
given string.
Source Code:
week3.py
''' Aim:Write a program to create, concatenate and print a string and accessing sub-string
from a given string.'''
Output:
Experiment 4
Write a python script to print the current date in the following format “Sun May 29 02:26:23
IST 2017”
Source Code:
week4.py
'''Aim: . Write a python script to print the current date in the following format Sun May 29
02:26:23 IST 2017 '''
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.'''
3
Output:
Experiment 5
Write a program to create, append, and remove lists in python.
Source Code:
week5.py
'''Aim: Write a program to create, append, and remove lists in python. '''
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)
Experiment 6:
Write a program to demonstrate working with tuples in python.
Source Code:
week6.py
Output:
4
Experiment 7:
Write a program to demonstrate working with dictionaries in python.
Source Code:
week7.py
dict2=dict1.copy()
#New dictoinary
print("\n New Dictionary is :",dict2)
#empties the dictionary
dict1.clear()
print("\n Uadated Dictionary is :",dict1)
Output:
6
Experiment 8:
Write a python program to find largest of three numbers.
Source Code:
week8.py
Output:
Experiment 9:
Write a Python program to convert temperatures to and from Celsius, Fahrenheit. [Formula:
c/5 = f-32/9]
Source Code:
week9.py
''' Write a Python program to convert temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9] '''
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")
7
Output:
Experiment 10 :
Write a Python program to construct the stars (*) pattern, using a nested for loop
Source Code:
week10.py
'''Write a Python program to construct the following pattern, using a nested for loop
*
**
***
****
8
*****
****
***
**
*
'''
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:
Experiment 11:
Write a Python script that prints prime numbers less than 20.
Source Code:
week11.py
'''Write a Python script that prints prime numbers less than 20'''
print(num)
Output:
Experiment 12 :
Write a python program to find factorial of a number using Recursion.
Source Code:
week12.py
Output:
10
Experiment 13
Write a program that accepts the lengths of three sides of a triangle as inputs. The program
output should indicate whether or not the triangle is a right triangle (Recall from the
Pythagorean Theorem that in a right triangle, the square of one side equals the sum of the
squares of the other two sides).
Source Code:
week13.py
'''Write a program that accepts the lengths of three sides of a triangle as inputs. The program
output should indicate whether or not the triangle is a right triangle (Recall from the
Pythagorean Theorem that in a right triangle, the square of one side equals the sum of the
squares of the other two sides).'''
base=float(input("Enter length of Base : "))
perp=float(input("Enter length of Perpendicular : "))
hypo=float(input("Enter length of Hypotenuse : "))
if hypo**2==((base**2)+(perp**2)):
print("It's a right triangle")
else:
print("It's not a right triangle")
Output:
11
Experiment 14:
Write a python program to define a module to find Fibonacci Numbers and import the
module to another program.
Source Code:
fibonacci.py
# Fibonacci numbers module
week14.py
'''Write a python program to define a module to find Fibonacci Numbers and import the
module to another program'''
#import fibonacci module
import fibonacci
num=int(input("Enter any number to print Fibonacci series "))
fibonacci.fib(num)
Output:
Experiment 15:
Write a python program to define a module and import a specific function in that module to
another program.
Source Code:
arth.py
''' Arithmetic Operations Module with Multiple functions'''
def Add(a,b):
c=a+b
return c
def Sub(a,b):
c=a-b
return c
def Mul(a,b):
c=a*b
return c
12
week15.py
'''Write a python program to define a module and import a specific function in that
module to another program.'''
from arth import Add
num1=float(input("Enter first Number : "))
num2=float(input("Enter second Number : "))
print("Addition is : ",Add(num1,num2))
print("Subtraction is : ",Sub(num1,num2)) #gives error:Not importing Sub function from arth
Module
Output:
Experiment 16:
Write a script named copyfile.py. This script should prompt the user for the names of two text
files. The contents of the first file should be input and written to the second file.
Source Code:
file1.txt
week16.py
'''Write a script named copyfile.py. This script should prompt the user for the names of two
text files. The contents of the first file should be input and written to the second
file'''
file1=input("Enter First Filename : ")
file2=input("Enter Second Filename : ")
# open file in read mode
fn1 = open(file1, 'r')
Output:
Experiment 17:
14
Write a program that inputs a text file. The program should print all of the unique words in
the file in alphabetical order.
Source Code:
file1.txt
week17.py
'''Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order'''
fname = input("Enter file name: ")
fh = open(fname)
lst = list() # list for the desired output
words=[];
for line in fh: # to read every line of file romeo.txt
words += line.split()
words.sort()
Output:
Experiment 18:
15
class irconvert:
num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'),(50, 'L'),
(40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
def num2roman(self,num):
roman = ''
while num > 0:
for i, r in self.num_map:
while num >= i:
roman += r
num -= i
return roman
Output:
Experiment 19
Write a Python class to implement pow(x, n)
Source Code:
week19.py
if x==-1:
16
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.powr(x,-n)
val = self.powr(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
Output:
Experiment 20
Write a Python class to reverse a string word by word.
Source Code:
week20.py
Output:
17