Python Data Conversion, Type Casting
Python Operators And Python Booleans
· The process of converting numeric data from one type to another is called as type conversion.
· To convert from integer to float, we use float() function.
To convert from integer to complex, we use the complex() function.
Example:
num1 = -25
num2 = float(num1)
num3 = complex(num1)
print(num2)
print(num3)
Output:
-25.0
(-25+0j)
· To convert from float to integer, we use int() function. int() function rounds of the given number to the nearest integer value.
To convert from float to complex, we use the complex() function.
Example:
num1 = 8.4
num2 = int(num1)
num3 = complex(num1)
print(num2)
print(num3)
Output:
8
(8.4+0j)
Note: complex numbers cannot be converted to integer or float.
Type Casting
Similar to type conversion, type casting is done when we want to specify a type on a variable.
Example:
str1 = "7"
str2 = "3.142"
str3 = "13"
num1 = 29
num2 = 6.67
print(int(str1))
print(float(str2))
print(float(str3))
print(str(num1))
print(str(num2))
Output:
7
3.142
13.0
29
6.67
Python Operators
Python has different types of operators for different operations. They are as follows:
Arithmetic operators:
Arithmetic operators are used to perform arithmetic/mathematical operations.
Name | Operator | Example |
Addition | + | a+b |
Subtraction | - | a-b |
Multiplication | * | a*b |
Division | / | a/b |
Exponential | ** | a**b |
Modulus | % | a%b |
Floor Division | // | a//b |
Assignment operators:
These operators are used to assign values to variables.
Name | Evaluated As |
= | a=b |
+= | a+=b or a=a+b |
-= | a-=b or a=a-b |
*= | a*=b or a=a*b |
**= | a**=b or a=a**b |
/* | a/=b or a=a/b |
//= | a//=b or a=a//b |
%= | a%=b or a=a%b |
&= | a&=b or a=a&b |
|= | a|=b or a=a|b |
^= | a^=b or a=a^b |
>>= | a>>=b or a=a>>b |
<<= | a<<=b or a=a<<b |
Bitwise operators:
Bitwise operators are used to deal with binary operations.
Name | Operator | Example |
Bitwise AND | Bitwise AND | a & b |
Bitwise OR | Bitwise OR | a | b |
Bitwise NOT | Bitwise NOT | ~a |
Bitwise XOR | Bitwise XOR | a ^ b |
Bitwise right shift | Bitwise right shift | a>> |
Bitwise left shift | Bitwise left shift | b<< |
Comparison operators:
These operators are used to compare values.
Name | Operator | Example |
Equal | == | a==b |
Not Equal | != | a!=b |
Less Than | < | a>b |
Greater Than | > | a<b |
Less Than or Equal to | <= | a>=b |
Greater Than or Equal to | >= | a<=b |
Identity operators:
Name | Example | Evaluated As |
is | a is b | Returns True if a and b are same |
is not | a is not b | Returns True if a and b are not same |
Logical operators:
These operators are used to deal with logical operations.
Name | Operator | Example |
AND | and | a=2 and b=3 |
OR | or | a=2 or b=3 |
NOT | not | Not(a=2 or b=3) |
Membership operators:
Name | Example | Evaluated As |
in | a in b | Returns True if a is present in given sequence or collection |
not in | a not in b | Returns True if a is not present in given sequence or collection |
Operator Precedence in Python:
Name | Operator |
Parenthesis | () |
Exponential | ** |
Complement, unary plus, unary minus | ~ , +, - |
Multiply, divide, modulus, floor division | *, /, %, // |
Addition, subtraction | +, - |
Left shift and right shift operators | <<, >> |
Bitwise and | & |
Bitwise or and xor | ^, | |
Comparison operators | <, >, >=, <= |
Equality operators | ==, != |
Assignment operators | =, %=, /=, //=, -=, +=, *= , **= |
Identity operators | is, is not |
Membership operators | in, not in |
Logical operators | and, or, not |
Python Booleans
Boolean consists of only two values; True and False.
Why are Boolean’s needed?
Consider the following if-else statement:
x = 13
if(x>13):
print("X is a prime number.")
else:
print("X is not a prime number.")
Is it True that X is greater than 13 or is it False?
· Thus Booleans are used to know whether the given expression is True or False.
· bool() function evaluates values and returns True or False.
Here are some examples where the Boolean returns True/False values for different datatypes.
None:
print("None: ",bool(None))
Output:
None: False
Numbers:
print("Zero:",bool(0))
print("Integer:",bool(23))
print("Float:",bool(3.142))
print("Complex:",bool(5+2j))
Output:
Zero: False
Integer: True
Float: True
Complex: True
Strings:
#Strings
print("Any string:",bool("Nilesh"))
print("A string containing number:",bool("8.5"))
print("Empty string:" ,"")
Output:
Any string: True
A string containing number: True
Empty string: False
Lists:
print("Empty List:",bool([]))
print("List:",bool([1,2,5,2,1,3]))
Output:
Empty List: False
List: True
Tuples:
#Tuples
print("Empty Tuple:",bool(()))
print("Tuple:",bool(("Horse", "Rhino", "Tiger")))
Output:
Empty Tuple: False
Tuple: True
Sets and Dictionaries:
print("Empty Dictionary:",bool({}))
print("Empty Set:",bool({"Mike", 22, "Science"}))
print("Dictionary:",bool({"name":"Lakshmi", "age":24 ,"job":"unemployed"}))
Output:
Empty Dictionary: False
Empty Set: True
Dictionary: True
0 Comments