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

Python 21

program

Uploaded by

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

Python 21

program

Uploaded by

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

1>>>

list_item = [21, 26, 5, 27, 8, 12, 9, 87]


print("Given list is = ",list_item)

sum = 0
for i in list_item:
sum = sum + i
print("Sum of the numbers in the given list is = ",sum)

2>>>
my_dict = {'data1':100,'data2':-54,'data3':247}
print("Given dictionary is =",my_dict)

sum = 0
for i in my_dict:
sum = sum + my_dict[i]
print('Sum of values of items is=',sum)

3>>>
filename = 'count.txt'
with open(filename, 'a') as f:
f.write('this is a test file.')
'''
Create the file count.txt and put the below text in it.
'this is a test file.'
Put the filein the same folder in which you are running this python code.

with open(filename, 'a') as f:


f.write('this is a test file.')

'''

try:
with open(filename, encoding='utf-8') as f:
contents = f.read()
except :
print()
else:
# Count the approximate number of words in the file.
words = contents.split(" ")
num_words = len(words)
print(f"The file count.txt has about {5} words.".format(num_words))

4>>>
filename = 'test_file.txt'
try:
with open(filename) as f:
contents = f.read()
except FileNotFoundError as e:
print("Sorry, the file test_file.txt does not exist.")

5>>>
first_number = 10
second_number = 0
try:
div = first_number/second_number
print(div)
except ZeroDivisionError as e:
print(e)

You might also like