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

Python Tasks

The document contains 9 tasks that involve writing Python programs to perform various tasks like reading user input, performing calculations, handling exceptions, reading and processing files. The tasks include: 1) Printing a greeting message with user's name. 2) Calculating sum, difference, product and division of two numbers. 3) Counting characters in a string. 4) Calculating area of different shapes. 5) Printing a name multiple times using for and while loops. 6) Handling divide by zero exception. 7) Printing current time repeatedly after an interval. 8) Reading a file line by line and counting words in each line.

Uploaded by

Radhey ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Tasks

The document contains 9 tasks that involve writing Python programs to perform various tasks like reading user input, performing calculations, handling exceptions, reading and processing files. The tasks include: 1) Printing a greeting message with user's name. 2) Calculating sum, difference, product and division of two numbers. 3) Counting characters in a string. 4) Calculating area of different shapes. 5) Printing a name multiple times using for and while loops. 6) Handling divide by zero exception. 7) Printing current time repeatedly after an interval. 8) Reading a file line by line and counting words in each line.

Uploaded by

Radhey ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Task 2 .

write a program to read your name and print "hello<your_name>"


message.

user_name = input('Plz enter your name :')


print(f"Hello {user_name}")

Task 3. write a program to read two numbers and their sum, difference product and
division

num1 = int(input('plz enter your first number :'))


num2 = int(input ("plz enter your second number :"))

print(f"sum of your numbers : {num1 + num2}",'\n')


print(f"difference of your numbers : {num2 - num1}",'\n')
print(f"Product of your numbers : {num1 * num2}",'\n')
print(f"Division of your numbers : {num1 / num2}",'\n')

Task 4. write a program to read a string and character count of a given string

user_input_string = "upflairs pvt. ltd. jaipur rajasthan"


temp_dictionary = dict()

# reading each and every character from the given string


for char in user_input_string:
temp_dictionary[char] = user_input_string.count(char)
print("YOU GIVEN STRING IS :- ", user_input_string,'\n')
print("YOUR RESULT IS :",'\n')
print(temp_dictionary)

Task 5. Write a program to calculate the area of a given shape (rectangle , triangle ,
circle) reading shape and appropriate values from standard input.

import math

def area_of_rectangle(length,width):
"""Formula is : AREA = Length * Width
"""
area = length * width
print(f"Area of rectangle will be : {area}")

def area_of_tringle(base, height):


area = 0.5 * base * height
print(f"Area of tringle will be : {area}")

def calculate_circle_area(radius):
area = math.pi * radius ** 2
print(f"Area of circle will be : {area}")

message = """
Hello welcome to you in area calculator program.
choose specific option:
1. press R/r for rectangle
2. press T/t for tringle
3. press C/c for circle
"""

print(message)
user_operation = input('plz enter your option : ')

if user_operation.lower() == "r":
l = int(input('Plz enter the length : '))
w = int(input('Plz enter the width : '))
area_of_rectangle(l,w)

elif user_operation.lower() == "t":


base = int(input('Plz enter the base : '))
height = int(input('Plz enter the height : '))
area_of_tringle(base,height)

elif user_operation.lower() == "c":


radius = int(input("Plz enter the radius :"))
calculate_circle_area(radius)
else:
print("choose correct option!")

Task 6. write a program to print a name “n” times, where name and “n” are read from
standard input, using for loop and while loop.
name = input("Plz enter your name : ")
n = int(input("Plze enter the 'n' : "))

# using for loop


print("output using for loop")
for i in range(n):
print(name)

print()

print('output using while loop')


i = 0
while i < n:
print(name)
i +=1
Task 7. write a program to handle divided by zero exception

num1 = int(input('Plz enter the first number : '))


num2 = int(input('Plz enter the second number : '))

try:
result = num1/num2
print(result)
except ZeroDivisionError as z:
print()
print("Plz dont insert zero !")
num1 = int(input('Plz enter the first number : '))
num2 = int(input('Plz enter the second number : '))
result = num1/num2
print(result)
Task 8. write a program to print current time for 10 times with
an interval of 10 seconds:

from datetime import datetime


import time

for i in range(10):
print(datetime.now())
time.sleep(10)

Task 9. write a program to read a file line by line and print the
word count of each line

with open('text_file.txt','r') as file:


lines_data = file.readlines()

word_count_temp = {}
for line in lines_data:
words_list = line.split(" ")
for word in words_list:
word_count_temp[word] = line.count(word)

print("<<<<<<< Line >>>>>>>")


print(word_count_temp)

You might also like