Basic
Python
Programs
This resource can assist you in
preparing for your interview
Piush Kumar Sharma
,11/26/23, 4:53 AM Basic Python Program - Jupyter Notebook
Program 1
Write a Python program to print "Hello Python".
In [1]: 1 print("Hello Python")
Hello Python
Program 2
Write a Python program to do arithmetical operations addition and division.
In [2]: 1 # Addition
2 num1 = float(input("Enter the first number for addition: "))
3 num2 = float(input("Enter the second number for addition: "))
4 sum_result = num1 + num2
5 print(f"sum: {num1} + {num2} = {sum_result}")
Enter the first number for addition: 5
Enter the second number for addition: 6
sum: 5.0 + 6.0 = 11.0
In [3]: 1 # Division
2 num3 = float(input("Enter the dividend for division: "))
3 num4 = float(input("Enter the divisor for division: "))
4 if num4 == 0:
5 print("Error: Division by zero is not allowed.")
6 else:
7 div_result = num3 / num4
8 print(f"Division: {num3} / {num4} = {div_result}")
Enter the dividend for division: 25
Enter the divisor for division: 5
Division: 25..0 = 5.0
Program 3
Write a Python program to find the area of a triangle.
In [4]: 1 # Input the base and height from the user
2 base = float(input("Enter the length of the base of the triangle: "))
3 height = float(input("Enter the height of the triangle: "))
4 # Calculate the area of the triangle
5 area = 0.5 * base * height
6 # Display the result
7 print(f"The area of the triangle is: {area}")
Enter the length of the base of the triangle: 10
Enter the height of the triangle: 15
The area of the triangle is: 75.0
localhost:8888/notebooks/Piush Kumar Sharma/Basic Python Program.ipynb 1/95
,11/26/23, 4:53 AM Basic Python Program - Jupyter Notebook
Program 4
Write a Python program to swap two variables.
In [5]: 1 # Input two variables
2 a = input("Enter the value of the first variable (a): ")
3 b = input("Enter the value of the second variable (b): ")
4 # Display the original values
5 print(f"Original values: a = {a}, b = {b}")
6 # Swap the values using a temporary variable
7 temp = a
8 a = b
9 b = temp
10 # Display the swapped values
11 print(f"Swapped values: a = {a}, b = {b}")
Enter the value of the first variable (a): 5
Enter the value of the second variable (b): 9
Original values: a = 5, b = 9
Swapped values: a = 9, b = 5
Program 5
Write a Python program to generate a random number.
In [6]: 1 import random
2 print(f"Random number: {random.randint(1, 100)}")
Random number: 89
Program 6
Write a Python program to convert kilometers to miles.
In [7]: 1 kilometers = float(input("Enter distance in kilometers: "))
2
3 # Conversion factor: 1 kilometer = 0.621371 miles
4 conversion_factor = 0.621371
5
6 miles = kilometers * conversion_factor
7
8 print(f"{kilometers} kilometers is equal to {miles} miles")
Enter distance in kilometers: 100
100.0 kilometers is equal to 62.137100000000004 miles
Program 7
Write a Python program to convert Celsius to Fahrenheit.
localhost:8888/notebooks/Piush Kumar Sharma/Basic Python Program.ipynb 2/95
, 11/26/23, 4:53 AM Basic Python Program - Jupyter Notebook
In [8]: 1 celsius = float(input("Enter temperature in Celsius: "))
2
3 # Conversion formula: Fahrenheit = (Celsius * 9/5) + 32
4 fahrenheit = (celsius * 9/5) + 32
5
6 print(f"{celsius} degrees Celsius is equal to {fahrenheit} degrees Fahr
Enter temperature in Celsius: 37
37.0 degrees Celsius is equal to 98.6 degrees Fahrenheit
Program 8
Write a Python program to display calendar.
In [9]: 1 import calendar
2
3 year = int(input("Enter year: "))
4 month = int(input("Enter month: "))
5
6 cal = calendar.month(year, month)
7 print(cal)
Enter year: 2023
Enter month: 11
November 2023
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Program 9
Write a Python program to solve quadratic equation.
The standard form of a quadratic equation is:
𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0
where
a, b and c are real numbers and
𝑎≠0
The solutions of this quadratic equation is given by:
(−𝑏 ± (𝑏2 − 4𝑎𝑐)1/2)/(2𝑎)
localhost:8888/notebooks/Piush Kumar Sharma/Basic Python Program.ipynb 3/95