PYTHON CODING
EXERCISES
CODING FOR BEGINNERS
JP TAM
Python Program Hello World
PROGRAM
# Sample Python Program to Print Hello World
print("Hello World")
OUTPUT
Hello World
>
Simple Python Program to add Two numbers
PROGRAM
# Simple Python program to Add Two Numbers
number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")
# Using arithmetic + Operator to add two numbers
sum = float(number1) + float(number2)
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))
OUTPUT
Please Enter the First Number: 5
Please Enter the second number: 6
The sum of 5 and 6 is 11.0
Python Arithmetic Operations
PROGRAM
# Python Program to Perform Arithmetic Operations
num1 = float(input(" Please Enter the First Value Number 1: "))
num2 = float(input(" Please Enter the Second Value Number 2: "))
# Add Two Numbers
add = num1 + num2
# Subtracting num2 from num1
sub = num1 - num2
# Multiply num1 with num2
multi = num1 * num2
# Divide num1 by num2
div = num1 / num2
# Modulus of num1 and num2
mod = num1 % num2
# Exponent of num1 and num2
expo = num1 ** num2
print("The Sum of {0} and {1} = {2}".format(num1, num2, add))
print("The Subtraction of {0} from {1} = {2}".format(num2, num1, sub))
print("The Multiplication of {0} and {1} = {2}".format(num1, num2, multi))
print("The Division of {0} and {1} = {2}".format(num1, num2, div))
print("The Modulus of {0} and {1} = {2}".format(num1, num2, mod))
print("The Exponent Value of {0} and {1} = {2}".format(num1, num2, expo))
OUTPUT
Please Enter the First Value Number 1: 10
Please Enter the Second Value Number 2: 20
The Sum of 10.0 and 20.0 = 30.0
The Subtraction of 20.0 from 10.0 = -10.0
The Multiplication of 10.0 and 20.0 = 200.0
The Division of 10.0 and 20.0 = 0.5
The Modulus of 10.0 and 20.0 = 10.0
The Exponent Value of 10.0 and 20.0 = 1e+20
Python Calendar Example
PROGRAM
# Python Calendar Example
# import calendar module
import calendar
# ask of month and year
year = int(input("Please Enter the year Number: "))
month = int(input("Please Enter the month Number: "))
# Displaying the Python calendar
print(calendar.month(year, month))
OUTPUT
Python Cube of a Number
PROGRAM
EXAMPLE 1
# Python Program to Calculate Cube of a Number
number = float(input(" Please Enter any numeric Value : "))
cube = number * number * number
print("The Cube of a Given Number {0} = {1}".format(number, cube))
OUTPUT
Please Enter any numeric Value : 5
The Cube of a Given Number 5.0 = 125.0
EXAMPLE 2
# Python Program to Calculate Cube of a Number
number = float(input(" Please Enter any numeric Value : "))
cube = number ** 3
print("The Cube of a Given Number {0} = {1}".format(number, cube))
EXAMPLE 3
Cube of a Number using Functions
# Python Program to Calculate Cube of a Number
def cube(num):
return num * num * num
number = float(input(" Please Enter any numeric Value : "))
cub = cube(number)
print("The Cube of a Given Number {0} = {1}".format(number, cub))
Sum and Average of N Natural Numbers using For Loop
PROGRAM
# Python Program to find Sum and Average of N Natural Numbers
number = int(input("Please Enter any Number: "))
total = 0
for value in range(1, number + 1):
total = total + value
average = total / number
print("The Sum of Natural Numbers from 1 to {0} = {1}".format(number, total))
print("Average of Natural Numbers from 1 to {0} = {1}".format(number, average))
OUTPUT
Please Enter any Number: 20
The Sum of Natural Numbers from 1 to 20 = 210
Average of Natural Numbers from 1 to 20 = 10.5
>
Program Check if a Number is Odd or Even
PROGRAM
# Python Program to check if a Number is Odd or Even
number = int(input(" Please Enter any Integer Value : "))
if(number % 2 == 0):
print("{0} is an Even Number".format(number))
else:
print("{0} is an Odd Number".format(number))
OUTPUT
Please Enter any Integer Value : 65151
is an Odd Number
>
Program to Print Even Numbers
PROGRAM
# Python Program to Print Even Numbers from 1 to N
maximum = int(input(" Please Enter the Maximum Value : "))
for number in range(1, maximum+1):
if(number % 2 == 0):
print("{0}".format(number))
OUTPUT
Please Enter the Maximum Value : 20
Program to check Number is Positive or Negative
PROGRAM
# Python Program to check Number is Positive or Negative
number = float(input(" Please Enter any Numeric Value : "))
if(number > 0):
print("{0} is a Positive Number".format(number))
elif(number < 0):
print("{0} is a Negative Number".format(number))
else:
print("You have entered Zero")
OUTPUT
Please Enter any Numeric Value : 50
50.0 is a Positive Number
>
Program to Calculate Profit or Loss using Elif Statement
PROGRAM
# Python Program to Calculate Profit or Loss
actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))
if(actual_cost > sale_amount):
amount = actual_cost - sale_amount
print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
amount = sale_amount - actual_cost
print("Total Profit = {0}".format(amount))
else:
print("No Profit No Loss!!!")
OUTPUT
Please Enter the Actual Product Price: 5000
Please Enter the Sales Amount: 6000
Total Profit = 1000.0
Program to find Square root
PROGRAM
# Python Program to find Square root of a Number
import math
number = float(input(" Please Enter any numeric Value : "))
squareRoot = math.sqrt(number)
print("The Square Root of a Given Number {0} = {1}".format(number, squareRoot))
OUTPUT
Please Enter any numeric Value : 50
The Square Root of a Given Number 50.0 = 7.0710678118654755
Python Program to find Student Grade
PROGRAM
# Python Program to find Student Grade
english = float(input(" Please enter English Marks: "))
math = float(input(" Please enter Math score: "))
computers = float(input(" Please enter Computer Marks: "))
physics = float(input(" Please enter Physics Marks: "))
chemistry = float(input(" Please enter Chemistry Marks: "))