Python type casting and operators
Let us see about Python type casting and operators.
We have the below topics covered as part of Python learning course.
Watch on YouTube: https://youtu.be/cit6jKwKY1o
Python type casting
We know Python is not strict with the data type declaration of variables. What makes the need of type casting? Let us see below
# weather.txt { "id": 2345, "city": "Chennai", "weather": "20", "unit": "Celsius" }
Consider there is a weather API, upon calling it whose response will be in JSON (JavaScript Object Notation) format.
The JSON response has key & value for "weather":"20"
Which is of type text/string.
But in the python program, we need it to be number so that we can convert it from Celsius to Fahrenheit or vice-versa.
In such cases we have to go for casting the data type.
# Program to read file containing JSON object for weather # In order to read and parse JSON, we need to use the json module from python import json weathers = [] # declaring empty list weather_value = None # Will be using this variable to store the type casted value print("Started Reading JSON file") with open('weather.txt') as f: # Reading file for jsonObj in f: # reading lines from the file weather = json.loads(jsonObj) # parsing and storing the line weathers.append(weather) # adding the line to the list print("Printing each JSON Decoded Object") for weather in weathers: # Traversing through the list print(weather) print(f"Type of weather is: {type(weather['weather'])}") weather_value = int(weather["weather"]) # Type casting from string to int # print(weather["id"], weather["city"], weather["weather"], weather["unit"]) print(f"{weather_value}° C") print(type(weather_value)) fahrenheit = (weather_value * 9/5) + 32 print(f"Fahrenheit {fahrenheit}")
Python Operators
Following are the types of operators available in Python programming.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations.
""" Python Arithmetic Operators Arithmetic operators are used with numeric values to perform common mathematical operations: -------------------------------------------------------------- | Operator | Name | Example | |---------------|--------------------|-----------------------| | + | Addition | x + y | | - | Subtraction | x - y | | * | Multiplication | x * y | | / | Division | x / y | | % | Modulus | x % y | | ** |Exponential (power) | x ** y | | // | Floor division | x // y | -------------------------------------------------------------- """
Relational operators
Relational or Comparison operators, which compares the value on either side and decide the relation among them.
""" -------------------------------------------------------------------------------------------- | Operator | Description | | == | equals operator check if both sides are same. | | != | not equals operator check if both sides are not same. | | <> | not equals operator check if both sides are not same. same as != | Python3 Does not Support <> | > | if left side value is greater than right. | | < | if left side value is lesser than right. | | >= | if left side value is greater and equal to the right. | | <= | if left side value is lesser and equal to the right. | ------------------------------------------------------------------------------------------ Lets see some examples below Assume variable x holds 3 and variable y holds 6, """
Assignment Operators
Assignment operators are used to assign value to a variable.
""" ----------------------------------------------------- | Operator | Example | Similar to | |-------------------|---------------|---------------| | = | x = 5 | x = 5 | Assign value from right to left side | += | x += 5 | x = x + 5 | It adds right & left operand and assign to left side | -= | x -= 5 | x = x - 5 | It subtracts right & left operand and assign to left side | *= | x *= 5 | x = x * 5 | It multiplies right & left operand and assign to left side | /= | x /= 5 | x = x / 5 | It divides left with right operand and assign to left side | %= | x %= 5 | x = x % 5 | It takes modulo using two operand and assign to left | //= | x //= 5 | x = x // 5 | It performs floor division on operands and assign to left | **= | x **= 5 | x = x ** 5 | It performs exponential (power) calc on operands & assign to left | &= | x &= 5 | x = x & 5 | Bitwise AND | |= | x |= 5 | x = x | 5 | Bitwise OR | ^= | x ^= 5 | x = x ^ 5 | Bitwise XOR | >>= | x >>= 5 | x = x >> 5 | Bitwise right shift | <<= | x <<= 5 | x = x << 5 | Bitwise left shift ---------------------------------------------------------- """
Logical Operators
Logical operators are used in combining conditional statements.
""" ------------------------------------------------------------------------------------------ | Operator | Description | Example | |---------------|---------------------------------------------------|----------------------| | and | Returns True if both statements are true | x < 5 and x < 10 | | or | Returns True if one statements is true | x < 5 or x < 10 | | not | Returns False if the result is true or vice-versa |not(x < 5 and x < 10) | |------------------------------------------------------------------------------------------| """
Bitwise Operators
Bitwise operators are used for comparing Binary (i.e., 0’s and 1’s) numbers. See below table for the available bitwise operators.
""" Bitwise Operators ----------------------------------------------------------------- | Operator | Example | Operator name | |-------------------|---------------|---------------------------| | & | x = x & y | Bitwise AND | | | | x = x | y | Bitwise OR | | ~ | x = x ~ y | Binary Ones Complement | | ^ | x = x ^ y | Bitwise XOR | | >> | x = x>>y | Bitwise right shift | | << | x = x<<y | Bitwise left shift | ----------------------------------------------------------------- """

When x and y value is 1 or True then the result will be 1 or True.

When any one (x or y) values are True the result will also be True.
Membership Operators
Membership operators, checks for membership in a sequence (strings, lists, or tuples).
There are two membership operators, See below.

Identity Operators
Identity operators compare the memory location of two objects.
There are two types of Identity Operators (is and not is), See below

