0% found this document useful (0 votes)
5 views10 pages

Python Programming Assignments Guide

Uploaded by

lonelyhillstar0
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)
5 views10 pages

Python Programming Assignments Guide

Uploaded by

lonelyhillstar0
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

Python Assignment Program

1. Input Initial velocity (u), acceleration (f) and time duration (t) from the
user and find the final velocity (v) where v = u + f t.
Answer:-

#Initial all the required variables

u = float(input("Enter the Initial Velocity: ")) # U is refers as Initial Velocity

f = float(input("Enter the Accelaration: ")) # F is refers as the Acceleration

t = float(input("Enter time (in seconds): ")) # T is refers as Time

#finding the final Velocity

v = u+f*t;

print("Final Velocity: ",v)

output

Enter the Initial Velocity: 25


Enter the Accelaration: 10
Enter time (in seconds): 5
Final Velocity: 75.0
2. Input radius ( r ) and height (h) of a cylinder from the user and find the
total surface area and volume where surface area = 2πr(r+h) and
volume = πr2h.
Answer:-

importing math Library

import math

#Initialize all Values

r = float(input("Enter radius of the Cylinder: ")); #radius


h = float(input("Enter Height of the Cylinder: ")); #height

pi = [Link]

#Total Surface area of the Cylinder

total_Surface_Area = 2*pi*r*(r+h)

print("Total Surface area of Cylinder: ",total_Surface_Area)

#Volume of the Cylinder

volume = pi*r*r*h

print("Volume of the Cylinder: ",volume)

output

Enter radius of the Cylinder: 5


Enter Height of the Cylinder: 4
Total Surface area of Cylinder: 282.7433388230814
Volume of the Cylinder: 314.1592653589793
3. Input an integer from the user and check it is odd or even.
Answer:-
#Checking the given number is odd or even

#initialize all the nessary values


num = int(input("Enter a Number: "))

#setting condition for odd or even

if num%2==0:
print("Given Number ",num," is Even")
elif num == 0:
print("Number is Zero")
else:
print("Given Number ",num," is Odd")
output:-
Enter a Number: 5
Given Number 5 is Odd

[Link] 3 sides from the user and check whether triangle can be formed or not. If
yes find the perimeter and area of the triangle.
Answer:-
# Input three values from the user
side_a = float(input("Enter the length of side a: "))
side_b = float(input("Enter the length of side b: "))
side_c = float(input("Enter the length of side c: "))

# Check if the input values form a triangle


if side_a + side_b > side_c and side_b + side_c > side_a and side_c + side_a >
side_b:
# Calculate and print the perimeter
perimeter = side_a + side_b + side_c
print(f"The input values form a triangle with perimeter {perimeter}.")
else:
print("The input values do not form a triangle.")

output:-
Enter the length of side a: 10
Enter the length of side b: 10
Enter the length of side c: 10
The input values form a triangle with perimeter 30.0.

[Link] a year from the user and check whether it is leap year or not.
Answer:-

# Input a year from the user


year = int(input("Enter a year: "))

# Check if the year is a leap year


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
output:-
Enter a year: 2023
2023 is not a leap year.

[Link] a natural number (n) and display the sum of all the natural numbers till n.
Answer:-

# Input a natural number from the user


n = int(input("Enter a natural number (n): "))

# Ensure that the entered number is positive


if n <= 0:
print("Please enter a positive natural number.")
else:
# Calculate the sum of natural numbers up to n
sum_of_numbers = (n * (n + 1)) // 2

# Print the result


print(f"The sum of all natural numbers up to {n} is: {sum_of_numbers}")
output:-
Enter a natural number (n): 20
The sum of all natural numbers up to 20 is: 210

output:-

[Link] a positive integer and find its factorial. n! = 1x2x3x…x n


Answer:-

# Input a non-negative integer from the user


n = int(input("Enter a non-negative integer (n): "))

# Check if the entered number is non-negative


if n < 0:
print("Please enter a non-negative integer.")
else:
# Initialize the factorial variable
factorial_result = 1
# Calculate the factorial of the entered number
for i in range(1, n + 1):
factorial_result *= i

# Print the result


print(f"The factorial of {n} is: {factorial_result}")
output:-
Enter a non-negative integer (n): 3
The factorial of 3 is: 6

[Link] a positive integer and display all the series: 1, 4, 7, 10, ….., n
Answer:-

# Input a positive integer from the user


n = int(input("Enter a positive integer (n): "))

# Check if the entered number is positive


if n <= 0:
print("Please enter a positive integer.")
else:
# Initialize a variable to store the series
series = []

# Generate the series 1, 4, 10, ..., n


for i in range(1, n + 1):
series_value = i * (i + 1) // 2
[Link](series_value)

# Print the series


print("The series is:", ", ".join(map(str, series)))
output:-
Enter a positive integer (n): 6
The series is: 1, 3, 6, 10, 15, 21

[Link] a positive integer and find the sum of all its digits.
Answer:-

# Input a number from the user


number = int(input("Enter a number: "))
# Ensure that the entered number is non-negative
if number < 0:
print("Please enter a non-negative number.")
else:
# Initialize a variable to store the sum of digits
sum_of_digits = 0

# Calculate the sum of digits


while number > 0:
digit = number % 10
sum_of_digits += digit
number //= 10

# Print the result


print(f"The sum of the digits is: {sum_of_digits}")
output:-
Enter a number: 50
The sum of the digits is: 5

[Link] 2 integers and display the numbers within the given range which are
divisible by 3 or 5.
Answer:-

# Input two integers from the user


start_num = int(input("Enter the starting integer: "))
end_num = int(input("Enter the ending integer: "))

# Ensure that the starting integer is less than or equal to the ending integer
if start_num > end_num:
print("Invalid input. The starting integer should be less than or equal to the ending
integer.")
else:
# Display numbers within the range that are divisible by 3 or 5
print(f"Numbers divisible by 3 or 5 within the range {start_num} to {end_num}:")
for num in range(start_num, end_num + 1):
if num % 3 == 0 or num % 5 == 0:
print(num)
output:-
Enter the starting integer: 4
Enter the ending integer: 10
Numbers divisible by 3 or 5 within the range 4 to 10:
5
6
9
10

[Link] a string check whether in upper case. If yes, convert to lower case.
Answer:-

# Input a string from the user


input_string = input("Enter a string: ")

# Check if the string is in uppercase


if input_string.isupper():
# Convert the string to lowercase
converted_string = input_string.lower()
print(f"The string in lowercase is: {converted_string}")
else:
print("The entered string is not in uppercase.")
output:-
Enter a string: SUMAN
The string in lowercase is: suman

[Link] two strings s1, s2. Check whether s1+s2=s2+s1


Answer:-

# Input two strings from the user


s1 = input("Enter the first string (s1): ")
s2 = input("Enter the second string (s2): ")

# Check if s1 + s2 is equal to s2 + s1
if s1 + s2 == s2 + s1:
print("s1 + s2 is equal to s2 + s1.")
else:
print("s1 + s2 is not equal to s2 + s1.")
ouput:-
Enter the first string (s1): suman
Enter the second string (s2): shah
s1 + s2 is not equal to s2 + s1.

[Link] a list, check how many are odd numbers, how many are even numbers.
Answer:-

# Input a list of numbers from the user


numbers = input("Enter a list of numbers separated by spaces: ").split()

# Convert the input values to integers


numbers = [int(num) for num in numbers]

# Initialize counters for odd and even numbers


odd_count = 0
even_count = 0

# Check each number and update the counters


for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

# Print the results


print(f"The list contains {even_count} even number(s) and {odd_count} odd
number(s).")
output:-
Enter a list of numbers separated by spaces: 2 3 4 5 6
The list contains 3 even number(s) and 2 odd number(s).

[Link] a list find sum of all numerical values present in it.


Answer:-

# Input a list of values from the user


values = input("Enter a list of values separated by spaces: ").split()

# Initialize a variable to store the sum of numerical values


sum_numerical_values = 0

# Iterate through the values and calculate the sum of numerical values
for value in values:
# Check if the value is a numerical value (integer or float)
if [Link](".", "", 1).isdigit(): # Removing decimal point for float check
sum_numerical_values += float(value)

# Print the result


print(f"The sum of numerical values in the list is: {sum_numerical_values}")
output:-
Enter a list of values separated by spaces: 4 5 6 7 8 9
The sum of numerical values in the list is: 39.0

[Link] a list of integers. If integer is even divide it by 2. If integer is odd


multiply by 2. Display both input and output lists.
Answer:-
# Input a list of integers from the user
input_list = input("Enter a list of integers separated by spaces: ").split()

# Convert the input values to integers


input_list = [int(num) for num in input_list]

# Perform the specified operations and create the output list


output_list = []
for num in input_list:
if num % 2 == 0:
# If the number is even, divide it by 2
output_list.append(num // 2)
else:
# If the number is odd, multiply it by 2
output_list.append(num * 2)

# Print the input and output lists


print("Input List:", input_list)
print("Output List:", output_list)
output:-
Enter a list of integers separated by spaces: 2 3 4 5 6 7 8
Input List: [2, 3, 4, 5, 6, 7, 8]
Output List: [1, 6, 2, 10, 3, 14, 4]

Common questions

Powered by AI

To determine if a number is odd or even, examine the result of the number modulo 2 operation. If the number modulo 2 results in 0, the number is even; otherwise, it is odd. For example, if you input the number 5 and calculate 5%2, the result is 1, indicating that 5 is an odd number. The use of modulo operation is a simple and efficient method to classify numbers as odd or even .

To calculate the total surface area and volume of a cylinder, you need its radius (r) and height (h). The total surface area is calculated using the formula 2πr(r + h), which accounts for both the lateral surface and the two circular bases. The volume is calculated using the formula πr²h, which quantifies the space enclosed by the cylinder. For example, if r = 5 and h = 4, the total surface area would be approximately 282.74 and the volume would be approximately 314.16 .

The factorial of a positive integer n, denoted as n!, is the product of all positive integers up to n. To compute n!, multiply all integers from 1 to n sequentially. For instance, for n = 3, the factorial is 3! = 1 * 2 * 3 = 6. This is typically implemented using a loop in programming, iterating from 1 through n, and multiplying each value to accumulate the result .

To determine if a list contains more odd or even numbers, iterate through the list and classify each number using the modulo operation. Initialize counters for odd and even numbers. For each number, check if it's even (num%2 == 0) and increment the even counter; otherwise, increment the odd counter. Compare the two counters to identify which type is more prevalent. For example, in a list [2, 3, 4, 5, 6], there are 3 even numbers and 2 odd numbers .

A year is a leap year if it fulfills either of the two conditions: 1) It is divisible by 4 but not divisible by 100, or 2) it is divisible by 400. These conditions exist because a solar year (the time it takes the Earth to orbit the Sun) is approximately 365.25 days; hence, an extra day is added every four years to account for these fractions. The subtle adjustment of skipping leap years in multiples of 100 but including those divisible by 400 ensures that the calendar year remains in alignment with the astronomical year .

To convert an uppercase string to lowercase, first check if the string is entirely in uppercase using the `isupper()` method. If true, transform the string to lowercase using the `lower()` method, and then return the modified string. This ensures case conversion is applied only when needed. For example, for input 'SUMAN', the process checks its case, finds it uppercase, and then converts it to 'suman' .

To calculate the final velocity (v) in a physics problem, you can use the formula v = u + f*t, where u is the initial velocity, f is the acceleration, and t is the time duration. The process involves multiplying the acceleration by the time duration and then adding the product to the initial velocity. For example, if the initial velocity is 25 units, the acceleration is 10 units, and the time duration is 5 seconds, the final velocity would be calculated as 25 + 10*5 = 75.0 units .

To find numbers divisible by either 3 or 5 within a specified range, iterate through the range using a loop. For each number, use the modulo operator to check divisibility: `num % 3 == 0` checks for divisibility by 3, and `num % 5 == 0` for 5. Any number satisfying either condition is collected as part of the result. For instance, in the range 4 to 10, numbers 5, 6, 9, and 10 satisfy the divisibility conditions .

To construct a sequence of natural numbers up to a given integer n, you start from 1 and proceed to n. The sequence can be generated using a loop that iterates from 1 to n, appending each integer to a list. The series 1, 3, 6, 10, etc., up to n, can be created as each term can follow a formula based on the sum of the first 'i' natural numbers calculated as i*(i+1)/2, for i = 1 to n. This type of series reflects triangular numbers .

To determine whether three sides can form a triangle, the sum of any two sides must be greater than the third side. This is known as the triangle inequality theorem. If sides a, b, and c are given, then check if a + b > c, a + c > b, and b + c > a. If these conditions are satisfied, a triangle can be formed, and its perimeter can be calculated as the sum of the three sides, a + b + c .

You might also like