Saturday, September 29, 2018

Operators and Type Casting In Python

Arithmetic expressions in any language comprise operands and operators. If operands are of the same data type, then the resulting value is also of that type. However, addition of two int data types can produce a long integer.

In an arithmetic expression, generally, the rule of Bracket, Of, Division, Multiplication, Addition, and Subtraction (BODMAS) is followed, and operators have their own precedence order. Exponentiation enjoys a higher precedence order, while addition and subtraction have a lower precedence order.
The decreasing precedence order is as follows:
  • Exponent
  • Unary negation
  • Multiplication, division, modulus
  • Addition, subtraction

The arithmetic operation involving both integer and floating point numbers is called mixed mode arithmetic. When each operand is of a different data type, the resulting value is of the more general type, and float is the more general type.


x = 11

w = x/2.0

print (w)

The output will be 5.5

the less general type integer, 11, is temporarily and automatically converted into float 11.0. Then, the calculation is performed. It is called mixed mode conversion.

Type conversions

Often in our program, it is necessary for us to convert from one data type to another, such as from an integer to a string. This is known as type casting. There are three built-in functions in Python that allow us to do type casting. These are the int(), float(), and str() functions.

The int() function in Python takes in a float or an appropriate string and converts it to an integer. To change a float to an integer, we can type int(6.712987). We’ll get 6 as the result (anything after the decimal point is removed). To change a string to an integer, we can type int("8") and we’ll get 8.

Can you  type int("Hello") or int("5.22321")? Just try and see the result.

The float() function takes in an integer or an appropriate string and changes it to a float. For instance, if we type float(5) or float("5"), we’ll get 2.0. If we type float("8.09109"), we’ll get 8.09109 which is a float and not a string since the quotation marks are removed.

The str() function on the other hand converts an integer or a float to a string. For instance, if we type str(3.1), we’ll get "3.1".

The screen below for type_casting.py program shows the typecasting



Some more operators in Python

Python supports the following types of operators:
  • Arithmetic operators.
  • Comparison operators
  • Assignment operators
  • Bitwise operators
  • Logical operators
  • Membership operators
  • Identity operators
Here is a brief summary about these operators.

Arithmetic operators

Arithmetic expressions comprise operands and operators:

  • ** Exponent:  Performs exponential (power) calculations on operands
  • * Multiplication: Performs multiplication between operands
  • / Division: Performs division between operands
  • % Modulus: Performs modulus division between operands
  • + Addition: Performs addition between operands
  • - Subtraction: Performs subtraction between operands
We have already discussed the precedence order of the operators.

Comparison operators

Like any other language, Python also supports comparison operators. Comparison
operators return True or False:

1. == Checks the equality
2. < Returns True if the left-hand side operand is less than the right-hand side operand
3. > Returns True if the left-hand side operand is greater than the right-hand side operand
4. <= Returns True if the left-hand side operand is less than or equal to the right hand side operand
5. >= Returns True if the left-hand side operand is greater than or equal to the right hand side operand
6. != Returns True if the left-hand side operand is not equal to the right-hand side operand
7. <> Returns True if the left-hand side operand is not equal to the right-hand side operand

The following screen shot shows comparison operators and their outputs



Comparison between different data types:
  • Numbers are compared arithmetically
  • Strings are compared as per the alphabetical order, using the numeric equivalents
  • Tuples and lists are compared according to the alphabetical order using the comparison of corresponding elements
Bitwise operators

Python supports bitwise operations AND, OR, complementary operations.

1. | Performs binary OR operation
2. & Performs binary AND operation
3. ~ Performs binary XOR operation
4. ^ Performs binary one's complement operation
5. << Left shift operator: The left-hand side operand bit is moved left by the number specified on the right-hand side
6. >> Right shift operator: The left-hand side operand bit is moved right by the number specified on the right-hand side

The screenshot below illustrates the various usages of bitwise operators:



Logical operators

Python supports logical operators AND, OR, and NOT:

  • and Returns True if both the right-hand and left-hand sides of the operator are true
  • or Returns True if any side, either the right-hand side or the left-hand side, of the operator is true
  • not If condition in the not operator is True, the not operator makes it False 

The screenshot below illustrates the various usages of logical operators:



Membership operators

Python has two membership operators to test the membership in a sequence, such as a
string, list, tuple, and others:
  • in Returns True if the specified operand is found in the sequence
  • not in Returns True if the specified operand is not found in the sequence
See the screen shot below-


Identity operators

Given in the table are the two identity operators:
  • is Returns True if two variables point to the same object and False, otherwise
  • is not Returns False if two variables point to the same object and True, otherwise

The following screenshot shows that the value can be same but id can be different. It returns True if id is the same:






Here, as both x and y contain the same value (10), id() returns the same value for both. However, when x and y are provided with the same list, id() returns different values. This could be attributed to the fact that when x and y have 10 assigned to them, it essentially means that both are pointing to the same memory address, which has a value of 10, while in the case of a list, this could be different. Why different? That is because lists are immutable, which means they can't be changed. So, when we assign the same list to y, it means that a new memory address is blocked again for a new list. The id() function returns the identity of an object. This is an integer (or long integer), which is guaranteed to be unique and constant for this object during its lifetime.

Python supports same memory allocation for integers only up to 256. It will be clearer with the following screenshot:



You may notice once we pass values above 256 for x and y, both are given different
memory allocations by the interpreter, but if the values are 256, then the same memory
allocation is provided for both x and y.
Share:

0 comments:

Post a Comment