# Operators and expressions¶

In Python most of the lines you will write will be expressions. Expressions are made of operators and operands. An expression is like 2 + 3 .

## Operators¶

Operators are the symbols which tells the Python interpreter to do some mathematical or logical operation. Few basic examples of mathematical operators are given below:

>>> 2 + 3
5
>>> 23 - 3
20
>>> 22.0 / 12
1.8333333333333333


To get floating result you need to the division using any of operand as floating number. To do modulo operation use % operator

>>> 14 % 3
2


## Example of integer arithmetic¶

The code

#!/usr/bin/env python3
days = int(input("Enter days: "))
months = days / 30
days = days % 30
print("Months = %d Days = %d" % (months, days))


The output

2
4
16


## Expressions¶

Generally while writing expressions we put spaces before and after every operator so that the code becomes clearer to read, like

a = 234 * (45 - 56.0 / 34)


One example code used to show expressions

#!/usr/bin/env python3
a = 9
b = 12
c = 3
x = a - b / 3 + c * 2 - 1
y = a - b / (3 + c) * (2 - 1)
z = a - (b / (3 + c) * 2) - 1
print("X = ", x)
print("Y = ", y)
print("Z = ", z)


The output

$./evaluationexp.py X = 10 Y = 7 Z = 4  At first x is being calculated. The steps are like this 9 - 12 / 3 + 3 * 2 -1 9 - 4 + 3 * 2 - 1 9 - 4 + 6 - 1 5 + 6 - 1 11 - 1 10  Now for y and z we have parentheses, so the expressions evaluated in different way. Do the calculation yourself to check them. ## Type Conversions¶ We have to do the type conversions manually. Like float(string) -> float value int(string) -> integer value str(integer) or str(float) -> string representation >>> a = 8.126768 >>> str(a) '8.126768'  ## evaluateequ.py¶ This is a program to evaluate 1/x+1/(x+1)+1/(x+2)+ … +1/n series upto n, in our case x = 1 and n =10 #!/usr/bin/env python3 sum = 0.0 for i in range(1, 11): sum += 1.0 / i print("%2d %6.4f" % (i , sum))  The output $ ./evaluateequ.py
1 1.0000
2 1.5000
3 1.8333
4 2.0833
5 2.2833
6 2.4500
7 2.5929
8 2.7179
9 2.8290
10 2.9290


In the line sum += 1.0 / i what is actually happening is sum = sum + 1.0 / i.

This is a program to evaluate the quadratic equation

#!/usr/bin/env python3
import math
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
c = int(input("Enter value of c: "))
d = b * b - 4 * a * c
if d < 0:
print("ROOTS are imaginary")
else:
root1 = (-b + math.sqrt(d)) / (2.0 * a)
root2 = (-b - math.sqrt(d)) / (2.0 * a)
print("Root 1 = ", root1)
print("Root 2 = ", root2)


## salesmansalary.py¶

In this example we are going to calculate the salary of a camera salesman. His basic salary is 1500, for every camera he will sell he will get 200 and the commission on the month’s sale is 2 %. The input will be number of cameras sold and total price of the cameras.

#!/usr/bin/env python3
basic_salary = 1500
bonus_rate = 200
commision_rate = 0.02
numberofcamera = int(input("Enter the number of inputs sold: "))
price = float(input("Enter the total prices: "))
bonus = (bonus_rate * numberofcamera)
commision = (commision_rate * numberofcamera * price)
print("Bonus        = %6.2f" % bonus)
print("Commision    = %6.2f" % commision)
print("Gross salary = %6.2f" % (basic_salary + bonus + commision))


The output

\$ ./salesmansalary.py
Enter the number of inputs sold: 5
Enter the total prices: 20450
Bonus        = 1000.00
Commision    = 2045.00
Gross salary = 4545.00