Typecasting and Operator Precedency

Document
Image description

Type Casting

Typecasting is one of the key feature in python. Type casting is a fundamental concept in Python that involves converting one data type to another.
There are two primary methods:

Typecasting
Implicit Typecasting
Explicit Typecasting

In this blog we will learn about how to typecast different variables or data.

Implicit Type Casting

Python automatically performs implicit type casting in certain situations to prevent data loss. For instance, when dividing two integers, Python implicitly converts the result to a float to maintain precision.

For ex - While dividing two integers number the interpreter implicitly converts the variable x into float


     x = 5
     y = 2
     div = x / y
     print(div)  # Output: 2.5
                

Explaination:

Here we have assigned the values to variable x and y
The variable x has the value 5 and the variable y has the value 2
Now in the next line of code we have defined a variable Div in which we have assign the divison of x/y
Now here the interpreter will implicitly convert the x variable into float data type
In the last line of code we have print the output which is in the float data type

Explicit Type Casting

In explicit type casting, the programmer manually converts a data type using built-in functions like int(), float(), str(), bool(), and tuple(). This is often necessary when working with user input or when precise data type conversions are required.

For example -> if we use the input in-built function in python by default it gives the value in string data type so a user can do explicit type casting by using any of the functions.


    a = input("Enter a number: ") 
    b = int(a) 
    print(b)
                    

In this example, the input() function returns a string. To perform arithmetic operations, we explicitly convert the input string to an integer using the int() function.

Explaination:

In these codes of line two variables are defined which are taking input from the users the variable a and the variable b
here in output we can see that when we print the variable a the output is in string variable
But in next line of code for variable b the user has explicity type cast the input into integer
Hence we can see a integer output.

Operator Precedence and Associativity

Operator Precedence:

In Python, operator precedence determines the order in which operators are evaluated in an expression containing multiple operators. When the Python interpreter encounters such an expression, it evaluates the operators in a specific, hierarchical order.

Operator Associativity:

While operator precedence helps resolve the order of different operators, associativity comes into play when an expression contains operators of the same precedence level. Associativity defines the direction (left-to-right or right-to-left) in which these operators are evaluated.

Python Operator Precedence and Associativity Table

Operator Precedence Associativity
() [] {} Highest Left-to-right
** High Right-to-left
*, /, //, % High Left-to-right
+, - Medium Left-to-right
<<, >>, & Medium Left-to-right
^, | Low Left-to-right
<, <=, >, >=, ==, != Low Left-to-right
not Low Right-to-left
and Low Left-to-right
or Lowest Left-to-right
Follow on LinkedIn

Comments

Popular posts from this blog

Recursion In Python

Tkinter and Api

Sets in python