Example 1: Add Two Numbers print('The square root of %0.
3f is
%0.3f'%(num ,num_sqrt))
num1 = 1.5
num2 = 6.3
Example 4: Arithmetic operators in Python
# Add two numbers
a=9
sum = num1 + num2
b=4
# Display the sum
# Addition of numbers
print('The sum of {0} and {1} is
{2}'.format(num1, num2, sum)) add = a + b
# Subtraction of numbers
Example 2: Add Two Numbers With User sub = a - b
Input
# Multiplication of number
# Store input numbers
mul = a * b
num1 = input('Enter first number: ')
# Division(float) of number
num2 = input('Enter second number: ')
div1 = a / b
# Add two numbers
# Division(floor) of number
sum = float(num1) + float(num2)
div2 = a // b
# Display the sum
# Modulo of both number
print('The sum of {0} and {1} is
mod = a % b
{2}'.format(num1, num2, sum))
# Power
p = a ** b
Example 3:Program to Find the Square
Root # print results
num = 8 print(add)
# To take the input from the user print(sub)
#num = float(input('Enter a number: ')) print(mul)
num_sqrt = num ** 0.5
print(div1) print(a and b)
print(div2) # Print a or b is True
print(mod) print(a or b)
print(p) # Print not a is False
Example 5: Comparison Operators in print(not a)
Python
a = 13
Example 7: Program to Calculate the Area
b = 33 of a Triangle
# a > b is False a=5
print(a > b) b=6
# a < b is True c=7
print(a < b)
# a == b is False # Uncomment below to take inputs from
the user
print(a == b)
# a = float(input('Enter first side: '))
# a != b is True
# b = float(input('Enter second side: '))
print(a != b)
# c = float(input('Enter third side: '))
# a >= b is False
print(a >= b)
# calculate the semi-perimeter
# a <= b is True
s = (a + b + c) / 2
print(a <= b)
Example 6: Logical Operators
# calculate the area
a = True
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
b = False
print('The area of the triangle is %0.2f'
# Print a and b is False
%area)
Example 8: Python Program to Swap Two Example 10: Program to Generate a
Variables Random Number
x=5 # Program to generate a random number
between 0 and 9
y = 10
# importing the random module
# To take inputs from the user
import random
#x = input('Enter value of x: ')
print(random.randint(0,9))
#y = input('Enter value of y: ')
# create a temporary variable and swap the
values Example 11: Program to Check if a
Number is Odd or Even
temp = x
num = int(input("Enter a number: "))
x=y
if (num % 2) == 0:
y = temp
print("{0} is Even".format(num))
print('The value of x after swapping:
{}'.format(x)) else:
print('The value of y after swapping: print("{0} is Odd".format(num))
{}'.format(y))
Example 12: Program to Check Leap Year
Example 9: Without Using Temporary
Variable
year = 2000
x=5
if (year % 400 == 0) and (year % 100 == 0):
y = 10
print("{0} is a leap year".format(year))
x, y = y, x
elif (year % 4 ==0) and (year % 100 != 0):
print("x =", x)
print("{0} is a leap year".format(year))
print("y =", y)
else:
print("{0} is not a leap year".format(year))
Example 13: Program to Find the Largest Example 14: Program to Check Prime
Among Three Numbers Number
num1 = 10
num2 = 14 num = 29
num3 = 12 flag = False
# uncomment following lines to take three
if num > 1:
numbers from user
# check for factors
#num1 = float(input("Enter first number: "))
for i in range(2, num):
#num2 = float(input("Enter second
number:")) if (num % i) == 0:
#num3 = float(input("Enter third number:")) # if factor is found, set flag to True
flag = True
if (num1 >= num2) and (num1 >= num3):
# break out of loop
largest = num1
break
elif (num2 >= num1) and (num2 >= num3):
largest = num2
# check if flag is True
else:
if flag:
largest = num3
print(num, "is not a prime number")
else:
print("The largest number is", largest)
print(num, "is a prime number")