Python 21
Python 21
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.
'''
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)