A1- Basic Python Programs
1. Python Program to Print Hello world!
Code:
# This program prints Hello, world!
print('Hello, world!')
2. Python Program to Add Two Numbers
Code:
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum :’, sum)
3. Python Program to Find the Square Root
Code:
print("Enter a Number: ")
num = int(input())
squareroot = num ** 0.5
print("\nSquare Root =", squareroot)
4. Python Program to Calculate the Area of a Triangle
Code:
# Python Program to find the area of triangle
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is', area)
5. Python Program to Solve Quadratic Equation
Code:
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a=1
b=5
c=6
Prof. Pranay H. Panchal Page 1 of 4
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-[Link](d))/(2*a)
sol2 = (-b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
6. Python Program to Swap Two Variables using temporary variable
Code:
# Python program to swap two variables
# To take inputs from the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x=y
y = temp
print('The value of x after swapping:',x)
print('The value of y after swapping: ',y)
7. Python Program to Swap Two Variables without using temporary variable
Code (Method 1):
x=5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
Code (Method 2):
x = 55
y = 33
x=x+y
y=x-y
x=x-y
print("x =", x)
print("y =", y)
Code (Method 3):
x = 15
y = 12
x=x*y
y=x/y
x=x/y
Prof. Pranay H. Panchal Page 2 of 4
print("x =", x)
print("y =", y)
Code (Method 4):
x = 13
y = 17
x=x^y
y=x^y
x=x^y
print("x =", x)
print("y =", y)
8. Python Program to Generate a Random Number
Code:
# Program to generate a random number between 0 and 9
# importing the random module
import random
print([Link](0,9))
9. Python Program to Convert Kilometers to Miles
Formula:
1 kilometre equals 0.62137 miles.
Miles = kilometre * 0.62137
And,
Kilometre = Miles / 0.62137
Code:
kilometre = float (input ("Please enter the speed of car in Kilometre as a unit: "))
conversion_ratio_1 = 0.621371
miles = kilometre* conversionratio
print ("The speed value of car in Miles: ", miles)
10. Python Program to Convert Celsius To Fahrenheit
Formula: T(°Fahrenheit)=(T(°Celsius)*1.8)+32
Code:
celsius = float(input("Enter temperature in celsius: "))
fahrenheit = (celsius * 1.8) + 32
print(str(celsius )+ " degree Celsius is equal to " + str(fahrenheit )+ " degree
Fahrenheit.")
Prof. Pranay H. Panchal Page 3 of 4
11. Python Program to Convert Degree Fahrenheit into Degree Celsius:
Code:
fahrenheit = 86
celsius = (fahrenheit - 32)/1.8
print(str(fahrenheit )+ " degree Fahrenheit is equal to " + str(celsius ) + " degree
Celsius." )
*****************************
Prof. Pranay H. Panchal Page 4 of 4