0% found this document useful (0 votes)
15 views3 pages

Q1 - Swap The Values of Two Variables A and B - Q2-wr...

The document provides solutions and explanations for 10 Python programming questions, covering topics such as variable swapping, time conversion, leap year determination, temperature conversion, age calculations, GST invoicing, profit calculations, marks input, multiples of a number, and triangle area calculation. Each question includes sample code, output, and a brief explanation of the logic used. The document serves as a practical guide for beginners to understand basic Python programming concepts.

Uploaded by

arcader095
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)
15 views3 pages

Q1 - Swap The Values of Two Variables A and B - Q2-wr...

The document provides solutions and explanations for 10 Python programming questions, covering topics such as variable swapping, time conversion, leap year determination, temperature conversion, age calculations, GST invoicing, profit calculations, marks input, multiples of a number, and triangle area calculation. Each question includes sample code, output, and a brief explanation of the logic used. The document serves as a practical guide for beginners to understand basic Python programming concepts.

Uploaded by

arcader095
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/ 3

I'd be glad to help you with these 10 Python programming questions.

Here are the solutions and


explanations:
Q1: Swap the values of two variables a and b
a = 10
b = 20

# Swapping values without a temporary variable


a, b = b, a

print("a =", a)
print("b =", b)

Output:
a = 20
b = 10

Explanation: The a, b = b, a syntax simultaneously assigns the value of b to a and the value of
a to b, effectively swapping their values.
Q2: Write a Python code to convert the time given in minutes into hours and minutes.
total_minutes = 150

hours = total_minutes // 60
minutes = total_minutes % 60

print("Hours:", hours)
print("Minutes:", minutes)

Output:
Hours: 2
Minutes: 30

Explanation: The // operator performs integer division, giving the number of whole hours. The
% operator calculates the remainder, giving the remaining minutes.
Q3: Write a program that asks the user for the year and prints whether it is a leap year or
not.
year = int(input("Enter a year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):


print(year, "is a leap year.")
else:
print(year, "is not a leap year.")

Explanation: A leap year is divisible by 4, but not by 100 unless it's also divisible by 400. The
code checks these conditions using the % operator for divisibility.
Q4: Write a program to obtain temperature in Celsius and convert it into Fahrenheit using
the formula C*9/5+32
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32

print("Temperature in Fahrenheit:", fahrenheit)

Explanation: The formula C*9/5+32 is used to convert Celsius to Fahrenheit.


Q5: Write a program that asks the user to enter the name and age, and prints a message
telling the user the year in which they will turn 100 years old.
name = input("Enter your name: ")
age = int(input("Enter your age: "))

current_year = 2024 # Replace with the current year


year_of_birth = current_year - age
year_to_turn_100 = year_of_birth + 100

print(name, "you will turn 100 in the year", year_to_turn_100)

Explanation: The code calculates the year of birth by subtracting the age from the current year.
Then, it adds 100 to the year of birth to find the year when the user will turn 100.
Q6: Write a program to get selling price and GST rate, and print an invoice along with
both GST and SGST.
selling_price = float(input("Enter selling price: "))
gst_rate = float(input("Enter GST rate: "))

cgst_rate = gst_rate / 2
sgst_rate = gst_rate / 2

cgst_amount = (selling_price * cgst_rate) / 100


sgst_amount = (selling_price * sgst_rate) / 100
total_gst = cgst_amount + sgst_amount
total_amount = selling_price + total_gst

print("Invoice")
print("Selling Price:", selling_price)
print("CGST Rate:", cgst_rate, "%")
print("CGST Amount:", cgst_amount)
print("SGST Rate:", sgst_rate, "%")
print("SGST Amount:", sgst_amount)
print("Total GST:", total_gst)
print("Total Amount:", total_amount)

Explanation: The code calculates the CGST and SGST amounts based on the selling price and
GST rate. It then calculates the total GST and total amount.
Q7: Write a Python code to calculate and display the selling price of an item, cost price,
and profit to be accepted by the user.
cost_price = float(input("Enter cost price: "))
profit_percentage = float(input("Enter profit percentage: "))

profit = (cost_price * profit_percentage) / 100


selling_price = cost_price + profit

print("Selling Price:", selling_price)

Explanation: The code calculates the profit based on the cost price and profit percentage, and
then calculates the selling price by adding the profit to the cost price.
Q8: Write a Python program to accept marks in 5 subjects and outputs total and average
marks.
subject1 = float(input("Enter marks in Subject 1: "))
subject2 = float(input("Enter marks in Subject 2: "))
subject3 = float(input("Enter marks in Subject 3: "))
subject4 = float(input("Enter marks in Subject 4: "))
subject5 = float(input("Enter marks in Subject 5: "))

total_marks = subject1 + subject2 + subject3 + subject4 + subject5


average_marks = total_marks / 5

print("Total Marks:", total_marks)


print("Average Marks:", average_marks)

Explanation: The code takes input for marks in 5 subjects, calculates the total marks and
average marks, and prints the results.
Q9: Write a program to input a number and print its first five multiples.
number = int(input("Enter a number: "))

for i in range(1, 6):


multiple = number * i
print(multiple)

Explanation: The code uses a for loop to iterate from 1 to 5, multiplying the input number by
each iteration and printing the result.
Q10: Write a program to find the area of a triangle.
base = float(input("Enter base of the triangle: "))
height = float(input("Enter height of the triangle: "))

area = (base * height) / 2

print("Area of the triangle:", area)

Explanation: The formula (base * height) / 2 is used to calculate the area of a triangle.
● https://edurev.in/t/268072/Assignment-Python-Variables-Data-Types
● https://github.com/Aashrya123/assignment

You might also like