You are on page 1of 12

Python examples

#this is python classes

Arithmetic operators
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication, etc.

Operato
Meaning Example
r

+ Add two operands or unary plus x + y+ 2

- Subtract right operand from the left or unary minus x - y- 2

* Multiply two operands x*y

/ Divide left operand by the right one (always results into float) x/y

x % y (remainder of
% Modulus - remainder of the division of left operand by the right
x/y)

Floor division - division that results into whole number adjusted to the left in
// x // y
the number line

x**y (x to the power


** Exponent - left operand raised to the power of right
y)

Example 1: Add Two Numbers

# This program adds two numbers


num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


Print(sum)

Example 2- Program to add two numbers taking input from user

num1 = input('Enter first number: ')


num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)
print (sum)

enter first number 5.2

enter second number 6.6

input( )

print()

Example 3-

# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user


# 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 %0.2f' %area)

example 4

program to check whether a number is positive,negative or zero using if else

num = float(input("Enter a number: "))


if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

program to check whether a number is positive,negative or zero using


nested if

num = float(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Example 5- to check whether the number is even or odd

# Python program to check if the input number is odd or even.


# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print(num,"is Odd")

Example 6

# Python program to find the largest number among the


three numbers

# change the values of num1, num2 and num3


# for a different result
num1 = 10
num2 = 14
num3 = 12

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)


Example 7

# Multiplication table (from 1 to 10) in Python

num = 12

num = int(input("Display multiplication table of?


"))

for i in range(1, 11):


print(num, 'x', i, '=', num*i)

Q)Using compound Boolean expression write a Python


program to print the numbers which are divisible by 7 and
multiples of 5 between m and n where m and n are positive
integers.
nl=[]
for x in range(1500, 2701):
if (x%7==0) and (x%5==0):
nl.append(str(x))
print (','.join(nl))
Q)Using compound Boolean expression write a Python
program to print the numbers which are divisible by 7 and
multiples of 5 between m and n where m and n are positive
integers
lower=int(input("Enter the lower range:"))
upper=int(input("Enter the upper range:"))
for i in range (lower,upper+1):
if(i%7==0 and i%5==0):
print(i)

q) Write a Python program to print the following output:


*
**
***
**
*
rows = 3
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print(" ")
for i in range(rows + 1, 0, -1):
for j in range(1, i - 1):
print("*", end=' ')
print(" ")

q)Write a Python program to read a number and check for prime. If


not, raise an arithmetic error to display as not prime.

num = int(input("Enter a number: "))

if num > 1: 0,1,1,2,3


for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
#print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")

else:
print(num,"is not a prime number")

q) Write the output for the following program segment:


for i in range(2,8,1):
if i==5:
break
print (i,)
print “Done”

Q)Give the output for the following expression evaluation in


Python: (i) -4**2 (ii) 2**3**2 (iii) 45%0

what will be the output of the following code


i=5
while i<0:
print (i)
i=1
else:
print “Sorry”’
q) Write a Python program to print the following pattern.
54321
4321
321
21
1

rows = 5
for i in range(0, rows + 1):
for j in range(rows - i, 0, -1):
print(j, end=' ')
print()

q) write a python program to print the multiplication table


using while loop
n=int(input(“enter a number”))
count=1
while count<=10:
p=n*count
print(p)
count=count+1
Use of break and continue
for item in range(1,6):
if item==3:
break
print(item)
print("done")

for item in range(1,6):


if item==3:
continue
print(item)
print("done")

q) Give the output for the following program segment: for c


in “PYTHON”: print (c) else: print (“Done”) What will be the
output if print (c) is followed by a break statement in the for
loop?
Print(c)
break
P
Y
T
H
O
N
Done

If break statement is added,then execution stops at first


iteration and prints only the first character.

Q) Write a Python program using function to convert an


integer to a string.
In Python an integer can be converted into a string using the
built-in str() function.

Example- int
num = 10
# check  and print type of num variable
print(type(num))

# convert the num into string


converted_num = str(num)
# check  and print type converted_num variable
print(type(converted_num))

multiplication using while loop


num=int(input(“enter a number”))
count=1
while count<=10:
p=num*count
print(p)
count=count+1

Function example
def pri_Twice(bruce):
print(bruce, bruce)
pri_Twice('Spam')
pri_Twice(5)
pri_Twice(3.14159)
pri_Twice('Spam'*4)

You might also like