#### Python Variables and Operators

Python Variables are nothing because of the reserved memory areas to save the values. In other words, it is the name of the memory location where the data is stored. And if once the variable is stored, a separate space is allocated in the memory. Moreover, we can define the variable name using the combination of letters, number, and underscore.

##### Assigning values to the variables

There is no explicit declaration to store the memory. And we can easily define a variable using the ‘=’operator.

a = 20

b = 30

Print a

20

##### Can we re-declare a variable?

Yes, we can redeclare a variable. But the Variable stores the most recent value.

Example:

x = ’10’

print (‘x’)

X=’abc’

print (‘x’)

10

ABC

##### Constants

These are fixed values that cannot be altered.

Example:

pi= 22.7

Gravity =9.8

Place these values separate files like constant.py

Now we can execute this by the following command

Import constant

print (constant.pi)

print (constant. Gravity)

So after running this command, we would get the following output

22.7

9.8

##### Operators

Operators can be defined as the constructs, which can manipulate the value of operands. Suppose the expression 9 – 4 = 5. Here, 9 and 4 are known as operands and – is known as an operator.

##### Types of Operator

In Python language, the following are the operators that are supported.

1. Arithmetic Operators
2. Assignment Operators
3. Bitwise Operators
4. Comparison (Relational) Operators
5. Identity Operators
6. Logical Operators
7. Membership Operators
8. Boolean Operators Let us have a look at all the above Python operators one by one.

##### Arithmetic Operators in Python

Assume variable x holds 30 and variable y holds 30, then:

Operators Description Example
(+) Addition It is a binary operator that adds values on either side of the operator. x + y = 60
(-) Subtraction It is a binary operator that subtracts the right-hand operand from the left-hand operand. x-y = 0
(*) Multiplication It is a binary operator that multiplies values on either side of the operator. x * y = 900
(/) Division It is a binary operator that divides left-hand operand by right-hand operand. y / x = 1
(%) Modulus It is a binary operator that split the left-hand operand through the right-hand operand and returns the remainder. y % x = 0
(**) Exponent It is a binary operator that performs exponential (power) calculation on operators. x**y =30 to the power 30
(//) Floor Division It is a floor Division operator. The operands division in which the outcome is the quotient and the numeric after the decimal point are deleted. The outcome is floored and rounded away from zero in the negative operands. 7//2 = 3 and 5.0//2.0 = 2.0 -11//3 = -4, -11.0//3 = -4.0
##### Assignment Operators in Python

In the below example, let us assume variable x holds a value of 10, and variable y holds a value of 20. Variable z is the outcome of the operand.

Operators Description Example
= It assigns values from right side operands to left side operand. z = x + y assigns value of x + y into z which is equal to 30.
+= Add AND It inserts the value of right operand to the value of the left operand and create the outcome to left operand. z += x is similar to z = z+ x.
-= Subtract AND It subtracts the value of the right operand from the left operand’s value and creates the outcome to the left operand. z -= x is similar to z = z – x.
*= Multiply AND It multiplies the right operand’s value with the left operand’s value and assigns the result to the left operand. z *= x is similar to z = z * x.
/= Divide AND It split the value of the left operand with the value of the right operand and create the outcome to the left operand z /= x is similar to z = z / x.
%=Modulus AND It creates modulus on the values using two operands and creates the outcomes to the left operand. z %= x is similar to z = z % x.
**=Exponent AND It executes exponential (power) evaluation on the operators and creates the outcomes to the left operand. z **= x is similar to z = z ** x.
//= Floor Division It executes floor division on the operators and creates the outcome to the left operand. z //= x is similar to z= z // x.
##### Bitwise Operators in Python

Bitwise operators are operators that operate on the bits and implement bit by bit functions. For example, if variable x = 60; and variable y = 13; then their similar binary format will be as follows.

Python language provides the following Bitwise operators.

Operators Description Example
& Binary AND Binary AND Operator replica a bit to the outcome if it is displayed in both operands. (x & y) will give the result as 12. (0000 1100 in binary).
| Binary OR Binary OR Operator replica a bit if it is displayed in either operand. (x | y) will give the result as 61. (0011 1101 in binary).
^ Binary XOR Binary XOR Operator replica the bit if it is a collection in one operand but not both. (x ^ y) will give the result as 49. (0011 0001 in binary).
~ Binary Ones Complement Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. (~x) will give the result as -61. (1100 0011 in binary). 2’s complement form because of a signed binary number
<< Binary Left Shift In the Binary Left Shift operator, the left operand’s value is changed left by the several bits defined by the right operand. x <<2 will give the result as 240 (1111 0000 inbinary).
>> Binary Right Shift In the Binary Right Shift operator, the left operand’s value is changed right by the several bits defined by the right operand. x >>2 will give the result as 15 (0000 1111 in binary).
##### Comparison (Relational) Operators in Python

Comparison operators in Python language compare the values on either side and decide whether the relationship is true or false. They are also called relational operators.

In the below example, variable x holds 20, and variable y has 30.

Operators Description Example
== For this relational operator, if the two operand’s value is equal, then the condition becomes true. (x == y) is false, as both have different values.
!= If the two operand’s value is not equal for this relational operator, then the condition becomes true. (x != y) is true as both have different values
<> For this relational operator, if two operand’s value are not equal, then condition becomes true. (x <> y) is true. This is similar to (!=) operator.
> For this relational operator, if the left operand’s value is greater than the right operand’s value, then the condition becomes true. (x > y) is false as the value of x is less than the value of y.
< For this relational operator, if the left operand’s value is less than the right operand’s value, then the condition becomes true. (x < y) is true as the value of x is less than the value of y.
>= For this relational operator, if the left operand’s value is greater than or equal to the right operand’s value, then the condition becomes true. (x >= y) is false as the value of x is neither greater nor equal to the value of y.
<= For this relational operator, if the left operand’s value is less than or equal to the right operand’s value, then the condition becomes true. (x <= y) is true as the value of x is less than the value of y. Although they are not equal, yet the result is true as the first condition is true.
##### Identity Operators in Python

Python language has two identity operators (is and is not). These are those operators that compare the memory areas of two objects. Both of the identity operators are defined as follows.

Operators Description Example
Is This identity operator calculates to true if the variables on either side of the operator point to a similar object (memory location reference). Therefore, it calculates to false. x is y. In this case, the results are 1 if ref(x) equals ref(y).
is not This identity operator evaluates to false if the variables on either side of the operator point to the same object (memory location reference). Otherwise, it evaluates to true x is not y. In this case, the result is 1 if ref(x) is not equal to ref(y).
##### Logical Operators in Python

Python supports three logical operators and, or and not.

Operators Description Example
and (Logical AND) If both the operands are true, then the condition becomes true. If x and y are true, then the condition becomes true else false.
or (Logical OR) If any of the two operands are non-zero, then the condition becomes true. If x or y is true, then the condition becomes true else false.
not (Logical NOT) It can retransform the logical state of its operand. If x is true, then Not (x) will be false and vice-versa.
##### Membership Operators in Python

In Python language, the membership operators test for membership in a series, including lists, tuples, or strings. Both of the membership operators are defined follows:

Operators Description Example
In This membership operator evaluates to true if it finds that a variable is a member in the specified sequence, and otherwise, it evaluates to false. x in y. In this case, the outcomes are 1 if x is a member of series y.
not in This membership operator calculates to true if it does not discover a variable is a member in the defined series, and therefore, it calculates to false. x not in y, the result is 1 if x is not a member of series y.
##### Operators Precedence in Python

The following table lists all operators from the highest precedence to the lowest precedence in Python language.

Operators Description
** Exponentiation (raise to the power)
~ + – Ones complement, unary plus and minus.
* / % // Multiply, divide, modulo, and floor division.
>> << Right and left bitwise shift.
& Bitwise ‘AND’.
^| Bitwise exclusive `OR’ and regular `OR’.
<= < > >= Comparison operators.
<> == != Equality operators.
= %= /= //= -= += *= **= Assignment operators
##### Python Boolean Operators

When you use or, it will either return the first value in the expression if it’s true, else it will blindly return the second value. I.e. or is equivalent to:

def or_(a, b):

if a:

return a

else: return b

For and, it will return its first value if it’s false, else it returns the last value:

def and_(a, b):

if not a:

return a

else: return b

##### Short-circuit evaluation

Python minimally evaluates Boolean expressions.

>>> def true_func(): …

print(“true_func()”) …

return True …

>>> def false_func(): …

print(“false_func()”) …

return False … >>>

true_func()

or

false_func()

true_func()

True

>>>

false_func()

or

true_func()

false_func()

true_func()

True

>>>

true_func()

and

false_func()

true_func()

false_func()

False

>>>

false_func()

and

false_func()

##### Operator Precedence

Python operators have a set order of precedence, which determines what operators are evaluated first in a potentially ambiguous expression. For instance, in the expression 3 * 2 + 7, first 3 is multiplied by 2, and then the result is added to 7, yielding 13. The expression is not evaluated the other way around, because * has a higher precedence than +.

Example:

>>> a, b, c, d = 2, 3, 5, 7

>>> a ** (b + c)

# parentheses 256

>>> a * b ** c

# exponent: same as `a * (b ** c)`

7776 >>> a + b * c / d

# multiplication / division: same as `a + (b * c / d)`

4.142857142857142

Enroll Yourself In Live Training: Python Training