Operators

Python Operators

Python describes a set of operators that can be used to manipulate data values. The operators range from assignment operators and mathematical operators (such as addition and subtraction) to relational operators and equality operators.

Assignment operator

The assignment operator assigns the value of the operand on the right to the operand on the left.

num = 10 # The value of 10 is assigned to variable num

a = 20      # The value of 20 is assigned to variable a

b = a        # The value of variable a is assigned to variable b

Arithmetic operators

Arithmetic operators perform mathematical operations on numeric operands.

The addition(+), subtraction(-) and the multiplication(*) operators work the same way as they do in mathematics.

The difference is with division techniques. There are three types of division in Python.

Firstly,

This is known as classical division. Classical division always returns a floating point number.

To discard the decimal part, we can go with floor division denoted by a double slash(//) as follows:

On the other hand, if you want to find the remainder of a division, then you have to use the % operator  as follows:

Augmented assignment operators

You can combine assignment operators with other operators as a shorthand method of assigning the result of an expression to a variable. For example, the following two examples have the same result:

Similarly,

Comparison operators

Comparison operators test for equality or difference between operands and return a True or False(bool) value. A complete list of the Python comparison operators is given below:

==Equal toa==b
!=Not equal toa!=b
<Less thana<b
<=Less than or equal toa<=b
>Greater thana>b
>=Greater than or equal toa>=b

For example,

Logical operators

Logical operators evaluate a logical expression for truthiness or falseness. There are three logical operators:

  • and
  • or
  • not

When we talk about truthy or falsy values, there are a lot of possibilities. For example,

False is a falsy value

0(and 0.0) is a falsy value

An empty string(”) is a falsy value

An empty list([ ]) is a falsy value

An empty dictionary or set({ }) is a falsy value

An empty tuple(()) is a falsy value

On the contrary,

True is a truthy value

Any number(int or float) other than 0(or 0.0) is a truthy value

Any non-empty string is a truthy value

Any non-empty list is a truthy value

Any non-empty dictionary or set is a truthy value

Any non-empty tuple is a truthy value

and operator

The logical and operator is a binary operator which means that it acts upon two operands. It checks whether both the operands(or values) are truthy or not.

For example,

The and operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.

or operator

The logical or operator checks whether one of the operands(or values) is truthy or not.

For example,

The or operator returns the value of the first truthy operand encountered when evaluating from left to right, or the value of the last operand if they are all falsy.

Interview Questions

Q. What are the three types of division in Python, and how do they differ?

Ans.

Classical Division (/): Returns a floating-point number, even if the operands are integers. Example: 10 / 3 = 3.3333

Floor Division (//): Returns the quotient as an integer, discarding any remainder. Example: 10 // 3 = 3

Modulo Division (%): Returns the remainder of a division. Example: 10 % 3 = 1

Q. Explain the concept of augmented assignment operators and provide two examples?

Ans. Augmented assignment operators combine an arithmetic operation with assignment. They offer a concise way to update a variable’s value.

Examples:

  • x += 5 is equivalent to x = x + 5
  • y *= 2 is equivalent to y = y * 2

Q. Which operator would you use to check if two variables have the same value? How would you check if they are not equal?

Ans. To check for equality, use the == operator (e.g., a == b).

To check for inequality, use the != operator (e.g., a != b).

Q. What is the difference between the and and or logical operators?

Ans.

and: Returns True only if both operands are truthy. Otherwise, it returns the first falsy value encountered or the last value if all are truthy.

or: Returns True if at least one operand is truthy. Otherwise, it returns the last falsy value encountered or the last value if all are falsy.

Q. Provide examples of falsy values in Python?

Ans

  • False
  • 0 (and 0.0)
  • Empty strings (“”)
  • Empty lists ([])
  • Empty dictionaries ({})
  • Empty sets ({})
  • Empty tuples (())

Coding Problems

Q. Calculate the area of a triangle with base = 10 and height = 8.

Ans

base = 10

height = 8

area = (base * height) / 2

print(area)  # Output: 40.0

Q. Given the variable x = 5, increment its value by 3 using an augmented assignment operator.

Ans

x = 5

x += 3

print(x)  # Output: 8

Q. Given two variables a = 10 and b = 5, calculate their sum, difference, product, and quotient.

Ans

a = 10

b = 5

sum = a + b

difference = a – b

product = a * b

quotient = a / b

print(sum)        # Output: 15

print(difference) # Output: 5

print(product)    # Output: 50

print(quotient)   # Output: 2.0

Q. Determine if 15 is greater than or equal to 10 and store the result (True/False) in a variable named result.

Ans

result = 15 >= 10

print(result)  # Output: True

Q. Given p = True and q = False, evaluate the expression p and q.

Ans

p = True

q = False

result = p and q 

print(result)  # Output: False 

Q. Given the strings str1 = “hello” and str2 = “world”, create a new string by concatenating them and store it in a variable named combined.

Ans

str1 = “hello”

str2 = “world”

combined = str1 + str2

print(combined)  # Output: helloworld