You already know how to store your data in the memory. But what's the point of storing it anyway? The answer is data manipulation. In all the applications, the data is manipulated to perform different actions. For example, when you apply online for admission to a school or college, your age is automatically calculated based on your date of birth. Have you ever wondered how it is done? The answer is using operators. Moreover, when you perform an addition in a calculator, you enter the numbers as strings. However, since the strings cannot be added, your data is converted to integers before performing any operation on them. This is what we call Type Conversion.
In this article, we will learn how to convert one type of data into another and perform different operations to manipulate data in Python. Let's get started.
Type Conversion in Python
Converting one type of data into another is called type conversion. This process is useful in various situations where the user provides one type of data, but it has to be converted to another type in order to manipulate it. You can convert integers to floats, floats to integers, strings to integers, integers to strings, and vice versa. Python can perform 2 types of conversions:
- Implicit Type Conversions
- Explicit Type Conversions
Implicit Conversions
In implicit conversions, the data is automatically converted to another type to perform certain operations. Usually, the conversion from a smaller type to a bigger type is implicit. For example, when an integer is added to a float value, the integer number is automatically converted to float.
Example:
first_number = 10;
second_number = 20.3;
result = first_number + second_number
print(result)
Output:
30.3
The above output shows that 10 was converted to float, i.e., 10.0, and then the addition was performed, so the result is also a float number.
Explicit Conversion
Explicit conversions refer to the conversions that programmers perform manually using some built-in functions. It is usually required when you need to convert data from a bigger type to a smaller type. The following are some common functions used for explicit type conversion:
int()
: Converts to integer.float()
: Converts to float.str()
: Converts to string.bool()
: Converts to bool.
Example:
number = 10
# Checking the type of number variable
print("Value: ", number, "Type:", type(number))
# Converting to float
converted_to_float = float(number) # 10 converted to 10.0.
# Rechecking the type of number variable.
print("Value:", converted_to_float, "Type:", type(converted_to_float))
float_number = 22.4
# Checking the type of float_number variable
print("Value: ", float_number, "Type:", type(float_number))
# Converting to int
converted_to_int = int(float_number) #22.4 converted to 22.
# Rechecking the type of float_number
print("Value:", converted_to_int, "Type:", type(converted_to_int))
Output:
Value: 10 Type:
Value: 10.0 Type:
Value: 22.4Type:
Value: 22 Type:
Example:
string_number = "10"
#Before Conversion
print("Value: ", string_number, "Type: ", type(string_number))
#After Conversion
int_number = int(string_number)
print("Value: ", int_number, "Type: ", type(int_number))
Output:
Value: 10 Type:
Value: 10 Type:
Operators in Python
Now that you understand type conversions in Python, it's time to explore some basic operators. Following are some types of basic operators:
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Relational/Comparison Operators
- Membership Operators
Arithmetic Operators
These are the basic arithmetic operators that include addition, subtraction, multiplication, division, and modulus.
- Addition (+): Adds two or more values.
- Subtraction (-): Finds the difference between two or more values.
- Multiplication (*): Product of two or more values.
- Division (/ or //): Divide two values.
- Modulus (%): Gives the remainder after dividing two values.
- Exponentiation (**): Raises a number to the given power.
Example:
number_1 = 30
number_2 = 20
# Add numbers
Addition = number_1 + number_2 # returns 50
# Subtract numbers
Subtraction = number_1 - number_2 # returns 10
# Multiply numbers
Product = number_1 * number_2 # returns 600
# Divide numbers
# Float division
Quotient = number_1 / number_2 # returns 1.5
# Integer division
Quotient = number_1 // number_2 # returns 1
# Find Reminder of the Numbers
Remainder = number_1 % number_2 # returns 10
# Raise a number to a certain power
Exponent = number_1 ** 2 # returns 900
Logical Operators
These operators are used to combine the results of two or more conditional statements.
and
: Returns true if all the conditions are true.or
: Returns true even if one condition is true.not
: Reverses the result of a condition. Turns true to false and vice versa.
Example:
condition_1 = True
condition_2 = False
print(condition_1 and condition_2) # Returns False
print(condition_1 or condition_2) # Returns True
print(not condition_1) # Returns False
Assignment Operators
These operators are used to assign a value to a variable.
- Equals to (=): Assigns a value to a variable.
- Plus Equals (+=): Adds something to the value and reassigns the new value to the variable.
- Minus Equals (-=): Subtracts something from the value and assigns the new value to the variable.
- Multiply Equals (*=): Multiplies something to the value and assigns the new value to the same variable.
- Divide Equals (/=): Divides the value with a number and assigns the new value to the same variable.
- Mod Equals (%=): Divides the value with a number and assigns the remainder to the same variable.
Example:
value = 100
value += 10 # New Value = 110
print(value)
value -= 10 # New Value = 100
print(value)
value *= 3 # New Value = 300
print(value)
value /= 10 # New Value = 30.0 (float division)
print(value)
value %= 10 # New Value = 0.0 (float remainder)
print(value)
Relational or Comparison Operators
As their name depicts, these operators are used to compare two or more values. They always return a Boolean value, i.e., True or False.
- Greater Than (>): Checks if a number is greater than the other number.
- Less Than (<): Checks if a number is lesser than the other number.
- Greater Than or Equals to (>=): Checks if a number is greater than or equal to the other number.
- Less Than or Equals To (<=): Checks if a number is lesser than or equal to the other number.
- Equals To (==): Checks if a number is equal to another number.
- Not Equal to (!=): Checks if a number is not equal to another number.
Example:
print(5 > 4) # Returns True
print (5 < 4) # Returns False
print(5 >= 5) # Returns True
print(5 <= 4) # Returns False
print(5 == 4) # Returns False
print(5 != 4) # Returns True
Membership Operators
These operators are used to check if a value exists in a sequence or list.
in
: Returns True if the value exists in the sequence.not in
: Returns True if the value does not exist in the sequence.
Example:
introduction = "This is the introduction to membership operators. "
print("introduction" in introduction) # Returns True
print("Arithmetic" not in introduction) # Returns True
Operator Precedence
Like mathematics, there is a specific order in which the operators are manipulated. The multiplication and division are performed before addition and subtraction. To change this order, parentheses are used. Parentheses have the highest precedence.
The operators and type conversions are the core concepts that not only come in handy in small applications but are widely used in advanced programming. Therefore, we have covered all the important topics related to these. In the next article, we will learn Conditional Statements in Python using these operators.