0% found this document useful (0 votes)
9 views2 pages

Python Programs for Beginners

The document contains multiple Python programs demonstrating basic programming concepts. It includes examples for printing a message, swapping variables, performing arithmetic operations, solving quadratic equations, generating random numbers, converting units, calculating the area of a triangle, and checking if a number is odd or even. Each program is accompanied by user input prompts and output displays.

Uploaded by

puseyash27
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)
9 views2 pages

Python Programs for Beginners

The document contains multiple Python programs demonstrating basic programming concepts. It includes examples for printing a message, swapping variables, performing arithmetic operations, solving quadratic equations, generating random numbers, converting units, calculating the area of a triangle, and checking if a number is odd or even. Each program is accompanied by user input prompts and output displays.

Uploaded by

puseyash27
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

Write a Python program to print "Hello Python". Write a Python program to swap two variables.

les. Write a Python program to solve quadratic


print("Hello Python") # Input two variables equation.
hello python a = input("Enter the value of the first variable (a): ") The standard form of a quadratic equation is:
Write a Python program to do arithmetical b = input("Enter the value of the second variable (b): 2
operations addition and division. ") 𝑥
𝑎 +𝑏𝑥+𝑐=0
# Addition # Display the original values
where
num1 = float(input("Enter the first number for print(f"Original values: a = {a}, b = {b}")
a, b and c are real numbers and
addition: ")) # Swap the values using a temporary variable 𝑎≠0
num2 = float(input("Enter the second number temp = a The solutions of this quadratic equation is given by:
for addition: ")) a=b 2
sum_result = num1 + num2 b = temp 𝑏
print(f"sum: {num1} + {num2} = {sum_result}") # Display the swapped values (−𝑏 ± ( −4𝑎𝑐 )/(2𝑎)
Enter the first number for addition: 5 print(f"Swapped values: a = {a}, b = {b}") )1/
Enter the second number for addition: 6 Enter the value of the first variable (a): 5 import math
sum: 5.0 + 6.0 = 11.0 Enter the value of the second variable (b): 9 # Input coefficients
# Division Original values: a = 5, b = 9 a = float(input("Enter coefficient a: "))
num3 = float(input("Enter the dividend for Swapped values: a = 9, b = 5d values: a = 9, b = 5 b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
division: ")) Write a Python program to generate a random
# Calculate the discriminant
num4 = float(input("Enter the divisor for number.
discriminant = b**2 - 4*a*c
division: ")) import random # Check if the discriminant is positive, negative, or
if num4 == 0: print(f"Random number: {[Link](1, 100)}") zero
print("Error: Division by zero is not allowed.") Random number: 89 if discriminant > 0:
else: Write a Python program to convert kilometers to # Two real and distinct roots
div_result = num3 / num4 miles. root1 = (-b + [Link](discriminant)) / (2*a)
print(f"Division: {num3} / {num4} = kilometers = float(input("Enter distance in root2 = (-b - [Link](discriminant)) / (2*a)
{div_result}") kilometers: ")) print(f"Root 1: {root1}")
Enter the dividend for division: 25 # Conversion factor: 1 kilometer = 0.621371 miles print(f"Root 2: {root2}")
Enter the divisor for division: 5 conversion_factor = 0.621371 elif discriminant == 0:
Division: 25.0 / 5.0 = 5.0 miles = kilometers * conversion_factor # One real root (repeated)
Write a Python program to find the area of a print(f"{kilometers} kilometers is equal to {miles} root = -b / (2*a)
triangle. print(f"Root: {root}")
miles")
# Input the base and height from the user else:
Enter distance in kilometers: 100
base = float(input("Enter the length of the base # Complex roots
100.0 kilometers is equal to 62.137100000000004 real_part = -b / (2*a)
of the triangle: ")) miles
height = float(input("Enter the height of the imaginary_part = [Link](abs(discriminant)) /
Write a Python program to convert Celsius to (2*a)
triangle: "))
Fahrenheit print(f"Root 1: {real_part} + {imaginary_part}i")
# Calculate the area of the triangle
celsius = float(input("Enter temperature in Celsius: print(f"Root 2: {real_part} - {imaginary_part}i")
area = 0.5 * base * height
# Display the result ")) Enter coefficient a: 1
print(f"The area of the triangle is: {area}") # Conversion formula: Fahrenheit = (Celsius * 9/5) + Enter coefficient b: 4
Enter the length of the base of the triangle: 10 32 Enter coefficient c: 8
Enter the height of the triangle: 15 fahrenheit = (celsius * 9/5) + 32 Root 1: -2.0 + 2.0i
The area of the triangle is: 75.0 print(f"{celsius} degrees Celsius is equal to Root 2: -2.0 - 2.0i
Write a Python program to swap two variables. {fahrenheit} degrees Fahr Write a Python program to swap two variables
# Input two variables without temp variable.
Enter temperature in Celsius: 37
a = input("Enter the value of the first variable a=5
37.0 degrees Celsius is equal to 98.6 degrees
(a): ") b = 10
Fahrenheit # Swapping without a temporary variable
b = input("Enter the value of the second Write a Python program to display calendar.
variable (b): ") a, b = b, a
import calendar print("After swapping:")
# Display the original values
year = int(input("Enter year: ")) print("a =", a)
print(f"Original values: a = {a}, b = {b}")
month = int(input("Enter month: ")) print("b =", b)
# Swap the values using a temporary variable
temp = a cal = [Link](year, month) After swapping:
a=b print(cal) a = 10
b = temp Enter year: 2023 b=5
# Display the swapped values Enter month: 11 Write a Python Program to Check if a Number is
print(f"Swapped values: a = {a}, b = {b}") November 2023 Odd or Even.
Enter the value of the first variable (a): 5 num = int(input("Enter a number: "))
Mo Tu We Th Fr Sa Su
Enter the value of the second variable (b): 9 if num%2 == 0:
1 2 3 4 5
Original values: a = 5, b = 9 print("This is a even number")
6 7 8 9 10 11 12 else:
Swappe 13 14 15 16 17 18 19 print("This is a odd number")
20 21 22 23 24 25 26 Enter a number: 3
27 28 29 30 This is a odd number

You might also like