Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Operators

Python Programming

The values that operator uses for performing computation are called operands. Various operators used in Python are described as follows

Operators

AU : Jan.-18, May-19, Dec.-19, Marks 12

• Operands are special symbols that are used in computations. For example +, - * and / are used for performing arithmetic operations.

• The values that operator uses for performing computation are called operands. Various operators used in Python are described as follows

 

1. Arithmetic Operators

• These operators are used for performing arithmetic operations.


For example

>>> 10+20

30

>>> 20-10

10

>>> 10/2

5.0

>>> 5*10

50

>>> 2**3

8

>>> 9//2

4

>>> 

 

2. Comparison Operators

The operators compare the values and establish the relationship among them.



3.  Logical Operators

There are three types of logical operators and, or, not.

For example

>>> a= Ture

>>> b= False

>>> a and b

False

>>> a or b

True

>>> not a

False

>>> 

 

4. Bitwise Operators

Bitwise operators work on the bits of the given value. These bits are binary numbers i.e. 0 or 1.

For example: The number 2 is 010, 3 is 011.


 

5.  Assignment Operators

The assignment operator is used to assign the values to variables. Following is a list of assignment operators.


Example and Meaning

Similarly *=, /= operators are used for performing arithmetic multiplication and division operation.

 

Example 3.2.1 Write a program in Python to print area and perimeter of a circle

Solution :

areaDemo.py

print("Enter radius os a circle: ")

r = float(input())

PI = 3.14

area= PI*r*r

perimeter = 2.0*PI*r

print("Area of Circle = ", area);

print("Perimeter of Circle = ",perimeter)

Output


32/areaDemo.py

Perimeter of circle - 62.800000000000004

 

Example 3.2.2 Write a program to find area and perimeter of parallelogram.

Solution : Formula for finding area and perimeter of parallelogram is

Area= b*h

Perimeter = 2*b+2*w

We can write the python program using above formula

areaDemo.py

print("Enter height(h):")

h = float(input())

print("Enter base(b): ")

b = float(input()) print("Enter width(w): ")

w = float(input())

area= b*h


perimeter = (2.0*b)+(2.0*w)

print("Area of Parallelogram = ",area);

print("Perimeter Parallelogram = ",perimeter)

Output

Enter height(h):

10

Enter base(b):

20 Enter width(w):

15

Area of Parallelogram = 200.0

Perimeter Parallelogram = 70.0

>>> 

 

Example 3.2.3 Write a program to convert Fahrenheit to Celsius.

Solution: We will use the following formula for conversion

Celsius = (Fahrenheit - 32)/1.8

Temp.py

print("Enter Fahrenheit ")

f = float(input())

c = (f-32)/1.8

print("Celsius = ",c)

Output

Enter Fahrenheit

95.5

Celsius = 35.2777

 

6. Membership Operators

There are two types of membership operators – in and not in

These operators are used to find out whether a value is a member of a sequence such as string or list.

Operator : Description

in : It returns True if a sequence with specified value is present in the object.

Not in : It returns true if a sequence with specified value is not present in the object.

Following screenshot of Python shell shows the use of in and non in operator.

Explanation :

1) In example, we have created a list of colors.

2) The members of color list are "red","blue" and "green"

3) As “blue” is member of color list, it returns True for while testing with membership operator in operator.

4) As "yellow" is not a member of color list, it return False for in operator and True for not in operator.

 

7. Identity Operators

The 'is' operator returns true if both the operand point to same memory location. Similarly 'is not operator returns true if both the operand point to different memory location.


Similarly,

>>> print(color1 is not color2)

True

Explanation : In above demonstration,

1) We have created two lists color1 and color2.

 2) Although the contents of the lists are exactly the same, their memory locations are different. Hence color1 is color2 becomes False.

3) As color3 is a new variable to which we assign color1, then they point to same memory location. Hence color1 is color3 returns True.

 

8. Modulus Operator

The % operator is a modulo operator that gives the remainder from the division of first argument by second.

For example –

>>> 10%3

1

>>> 10.10%3.3

0.20000000000000018

>>> 

The operator // is used for floor division. This division returns the integral part of the quotient.

For example -

>>> 10//3.5

2.0

 

Example 3.2.4 Write a program in Python to convert given time into minutes and seconds. For example - if user inputs 260 seconds then the output should be 4 minute and 20 seconds. (Use // and % operators)

Solution :

time Demo.py

print("Enter time: ")

time=float(input())

minutes = time//60

seconds = time % 60

print("Minutes are: ",minutes)

print("Seconds are: ",seconds)

Output


 

9. String Operators

• String is collection of characters.

• In python it is possible to perform the concatenation and repetition operations on strings using the operators like + and *.

For example

>>> str1= "Hello"

>>> str2 = "friend"

>>> str1 + str2

'Hellofriend'

>>> "welcome"*2

'welcomewelcome!

Review Questions

1. Appraise the arithmetic operators in Python with an example.

AU : Jan.-18, Marks 12

2. State about logical operators available in Python language with example.

AU : May-19, Marks 2

3. Summarize the precedence of mathematical operators in Python.

AU : Dec.-19, Marks 8

 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Python Programming - Operators