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

Task 4 Inputs yash kumar 23.51119

The document contains a series of Python programming tasks, including calculating sums, checking for prime numbers, and manipulating lists and dictionaries. Each task is accompanied by example code snippets that demonstrate the required functionality. The tasks cover a range of topics suitable for a B.A. program in computer science.

Uploaded by

Rijobrata Dhar
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)
6 views

Task 4 Inputs yash kumar 23.51119

The document contains a series of Python programming tasks, including calculating sums, checking for prime numbers, and manipulating lists and dictionaries. Each task is accompanied by example code snippets that demonstrate the required functionality. The tasks cover a range of topics suitable for a B.A. program in computer science.

Uploaded by

Rijobrata Dhar
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/ 7

#Task 4

#Name: yash kumar


#Roll Number: 23/51119
#Course: B.A. program
#UV roll no.:- 23021501246
#1. Write a program to calculate the sum of series
up to n term.
n = 5 # Example: n = 5
result = sum(int("2" * i) for i in range(1, n + 1))
print(result) # Output: 24690
#2. Write Python Program to Find the Sum of Digits
in a Number.
num = int(input("Enter a number: "))
sum_digits = sum(int(digit) for digit in str(num))
print("Sum of digits:", sum_digits)
#3. Write a Program to Check Whether a Number Is
Prime or Not.
num = int(input("Enter a number: "))
if num > 1 and all(num % i != 0 for i in range(2,
int(num**0.5) + 1)):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
#4. Write a program that uses a while loop to add up
all the even numbers between 100 and 200.
sum_even = 0
i = 100
while i <= 200:
if i % 2 == 0:
sum_even += i
i += 1
print("Sum of even numbers:", sum_even)
#5. Write a program to find factorial of 6.
factorial = 1
for i in range(1, 7):
factorial *= i
print("Factorial of 6:", factorial)
#6. Given a list of integers, a, add all the elements of
a=[2,3,1,3,3].
a = [2, 3, 1, 3, 3]
print("Sum of list elements:", sum(a))
#7. Write a paragraph describing yourself of about
100-150 words and fill in your Name, Course name,
College Name, and place of birth with the help of the
format command. Also, print the first and last words
at the 10th position in your text.
name = "Your Name"
course = "Your Course"
college = "Your College"
birthplace = "Your Place of Birth"
text = f"My name is {name}. I am studying {course}
at {college}. I was born in {birthplace}."
print(text)
print("10th Position Word:", text.split()[9], "Last
Word:", text.split()[-1])
#8. S = [10,20,30,40,50,60,70,80] Make a two-
element list from the above list containing
(respectively) the sum of the even numbers and the
sum of the odd numbers.
S = [10, 20, 30, 40, 50, 60, 70, 80]
sum_even = sum(x for x in S if x % 2 == 0)
sum_odd = sum(x for x in S if x % 2 != 0)
result = [sum_even, sum_odd]
print(result)
#9. Define a dict whose keys are dates (represented
by strings) from the most recent week and whose
values are temperatures. Ask the user to enter a
date, and display the temperature on that date, as
well as the previous and subsequent dates.
temperatures = {
"2024-12-19": 25,
"2024-12-20": 22,
"2024-12-21": 24,
"2024-12-22": 23,
"2024-12-23": 20,
"2024-12-24": 19,
"2024-12-25": 18
}
date = input("Enter a date (YYYY-MM-DD): ")
if date in temperatures:
temp = temperatures[date]
print(f"Temperature on {date}: {temp}")
dates = sorted(temperatures.keys())
idx = dates.index(date)
prev_date = dates[idx - 1] if idx > 0 else "No previous
date"
next_date = dates[idx + 1] if idx < len(dates) - 1 else
"No subsequent date"
print(f"Previous date: {prev_date}, Next date:
{next_date}")
else:
print("Date not found.")
#10. Write a program to calculate distance between
two points taking input from user.
x1 = int(input("Enter X1: "))
y1 = int(input("Enter Y1: "))
x2 = int(input("Enter X2: "))
y2 = int(input("Enter Y2: "))777
distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
print("Distance:", distance)
#11. Using for loops find decimal equivalents of
1/2,1/3, 1/4,1/15, 1/10.
for denominator in [2, 3, 4, 15, 10]:
print(f"1/{denominator} = {1/denominator}")
#12. Write a program to write a table of 4.
for i in range(1, 11):
print(f"4 x {i} = {4 * i}")
#13. Write a Python script to print a dictionary where
the keys are numbers between 1 and 15 (both
included) and the values are the squares of the keys.
squares = {x: x**2 for x in range(1, 16)}
print(squares)
#14. Build a multiplication table up to 9×9.
for i in range(1, 10):
for j in range(1, 10):
print(f"{i} x {j} = {i * j}", end="\\t")
print()
#15. Write a Python program to combine two
dictionaries by adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}
combined = {k: d1.get(k, 0) + d2.get(k, 0) for k in
set(d1) | set(d2)}
print(combined)

You might also like