0% found this document useful (0 votes)
27 views11 pages

Atul (Python)

The document contains 41 Python programs with the objective, code and output of each program. The programs cover topics like removing punctuation from strings, checking if two strings are anagrams, using global variables in nested functions, importing modules, counting vowels in a string, mail merge, finding image resolution, handling multiple exceptions, splitting lists into chunks, hashing files, and catching exceptions. The programs are part of a Data Analysis with Python course for a BTech third year student.

Uploaded by

Rahul Lohar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
27 views11 pages

Atul (Python)

The document contains 41 Python programs with the objective, code and output of each program. The programs cover topics like removing punctuation from strings, checking if two strings are anagrams, using global variables in nested functions, importing modules, counting vowels in a string, mail merge, finding image resolution, handling multiple exceptions, splitting lists into chunks, hashing files, and catching exceptions. The programs are part of a Data Analysis with Python course for a BTech third year student.

Uploaded by

Rahul Lohar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

COLLEGE OF TECHNOLOGY AND ENGINEERING

NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON


CLASS:- B.TECH III YEAR DATE:-

Program 31

Object :- Write a program to Remove Punctuations from a string.

Code :-

#define punctuation
from string import punctuation
punctuation = "'!()-[]{};:\,<>./?@#$%^&*_~'"
my_str = "Hello!!!, he said ---and went."
#To take input from the user
#my_str = input("Enter a string :")
#remove punctuation from the string
no_punct = ""
for char in my_str:
if char not in punctuation:
no_punct = no_punct + char
#display the unpunctuated string
print(no_punct)

Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 32

Object :- Write a program to check if two strings are Anagram.


Code :-
str1 = "Race"
str2 = "Care"
# convert both the strings into lowercase
str1 = str1.lower()
str2 = str2.lower()
# check if length is same
if(len(str1) == len(str2)):
# sort the strings
sorted_str1 = sorted(str1)
sorted_str2 = sorted(str2)
# if sorted char arrays are same
if(sorted_str1 == sorted_str2):
print(str1 + " and " + str2 + " are anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
else:
print(str1 + " and " + str2 + " are not anagram.")
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 33

Object :- Write a program using a Global variable in Nested Function.


Code :-
from re import X
def foo():
x=20
def bar():
global x
x=25
print("Befpre calling bar :-",x)
print("Calling bar now....")
bar()
print("After calling bar :-",x)
foo()
print("x is main :-",x)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 34

Object :- Write a program to import a module.


Code :-
#importing standard module math
import math
print("Value of pi is",math.pi)
#import module by renaming it
import math as m
print("Value of pi (after renaming) is",m.pi)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 35

Object :- Write a program to count the no. of each vowels.


Code :-
from itertools import count
vowels = 'aeiou'
input_string = 'Hello, have you tried our tutorial section yet?'
input_string = input_string.casefold()
count = {}.fromkeys(vowels,0)
for char in input_string:
if char in count:
count[char] += 1
print(count)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 36

Object :- Write a program to mail merger.


Code :-
#open names.txt file for reading
with open("names.txt", 'r', encoding='utf-8') as names_file
#open body.txt file for reading
with open("body.txt", 'r', encoding='utf-8') as body_file
#reading entire content of the body file
body = body_file.read()
for name in names_file:
mail = "Hello " + name.strip() + body
with open(name.strip() +".txt", 'w', encoding='utf-8') as mail_file:
mail_file.write(mail)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 37

Object :- Write a program to find the size(resolution) of a image.


Code :-
def jpeg_res(filename):
#open image for reading in binary mode
with open(filename, 'rb') as img_file:
#height of image (in 2 bytes) is at 164th position
img_file.seek(163)
#read the 2 bytes
a = img_file.read(2)
#calculate height
height = (a[0] << 8) + a[1]
#next 2 bytes is width
a = img_file.read(2)
#calculate width
width = (a[0] << 8) + a[1]
print("The resolution of the image is ",width," x ",height)
jpeg_res("img1.jpg")
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 38

Object :- Write a program to check multiple exceptions as a parenthesized tuple.


Code :-
string = input("Enter the string :- ")
try:
num = int(input("Enter num :- "))
print(string + num)
except (TypeError, ValueError) as e:
print(e)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 39

Object Write a program to Split a List into Evenly Sized Chunks.


Code :-
from pkg_resources import yield_lines
def split(list_a, chunk_size):
for i in range(0, len(list_a), chunk_size):
yield list_a[i:i + chunk_size]
chunk_size = 2
my_list = [1,2,3,4,5,6,7,8,9]
print(list(split(my_list, chunk_size)))
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 40

Object :- Write a program to find Hash of file.


Code :-
import hashlib
def hash_file(filename):
h = hashlib.sha1()
with open(filename,'rb') as file:
chunk = 0
while chunk != b'':
chunk = file.read(1024)
h.update(chunk)
return h.hexdigest()
message = hash_file("Game.mp3")
print(message)
Output :-
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME:- ATUL KUMAWAT SUBJECT:- DATA ANALYSIS WITH PYTHON
CLASS:- B.TECH III YEAR DATE:-

Program 41

Object :- Write a program for catching exceptions in python.


Code :-
import sys
random_list = ['a',0,2]
for entry in random_list:
try:
print("The entry is ", entry)
r = 1/int(entry)
break
except:
print("Oops!", sys.exc_info()[0], "occured.")
print("Next entry.")
print()
print("The reciprocal of ", entry," is", r)
Output :-

You might also like