Mixing types. Type conversion in operators. The rules
Contents
- 1. What does the term “type mixing” mean in operators?
- 2. Rules of type conversion in Python
- 3. Built-in functions int(), float() of forced type conversion. Examples
- 4. What categories of operators (operations) can be used when mixing and converting types?
- 5. Is it possible in Python to sum a number with a string?
- Related topics
Search other websites:
1. What does the term “type mixing” mean in operators?
When using binary operators (such as +, –, *, /, etc.), it is possible that both operands (numbers) have different types. If, when executing a certain operator, both operands have different types, then this case is called type mixing.
For example
# type conversion and type mixing a = 3 # mixing of integer and real type, result - real type # in the operation of multiplication * operands have different types b = a * 5.7 # b = 17.1
In the example in the operation of multiplication (*), the operand a on the left has an integer type, and the operand 5.7 on the right of has a real type.
Other cases of mixing types are possible in Python, as the following example shows.
# mixing of real and complex type, the result is a complex type b = 17.1 c = b / (2+3j) # c= (2.630769230769231-3.946153846153847j) # mixing integer and complex type d = 5 * (1.2+3.8j) # d= (6+19j)
⇑
2. Rules of type conversion in Python
If in some operation, the operands have different types (case of mixing types), then the Python interpreter automatically performs the following actions:
- first, the types of operands are converted to the type of the most complex operand;
- then operations are performed on these operands in accordance with the rules of this complex type.
The following are numerical types in descending order of difficulty:
- complex numbers (highest complexity);
- real numbers;
- integer numbers.
Based on this, the following conclusions can be drawn:
- if an integer and a real number take part in the operation, then the integer will be converted into a real one and the result will be a real type number;
- if a real and complex number takes part in the operation, then the real number will be converted to a complex number and the result will be of a complex type. The same applies to combining integer and complex numbers.
For example, if you look at a piece of code
a = 5 b = 3.2 + a
in the line
b = 3.2 + a
situation arises type mixing. In this case, first an object a of an integer type is converted to an object of a real type with a value of 5.0, then summation (+) is applied for two objects of a real type, which are the number 3.2 and the number 5.0. A result of type 8.2 is written to object b.
⇑
3. Built-in functions int(), float() of forced type conversion. Examples
The Python interpreter performs automatic type conversion in expressions. Such a conversion is quite convenient. However, a situation is possible when you need to force convert from one type to another.
To do this, Python implements functions that perform the conversion of integer and real types:
- int() – cuts off the fractional part of a real number;
- float() – converts an integer to real.
Examples.
# function int() a = 8.423 b = int(a) # b = 8 - integer number b = int(7.2-1.3302) # b = 5 - integer number # function float() - converts an integer to real a = 35 b = float(a) # b = 35.0 - real number
⇑
4. What categories of operators (operations) can be used when mixing and converting types?
When using binary operators (operations), a situation is possible when the operands are of different types. Python language allows such use of operands. Type mixing is allowed in the following categories of operators:
- arithmetic (mathematical) operators +, –, *, /, %, //, **;
- comparison operators ==, !=, <, >, >=, <=.
For example.
# arithmetic operators a = 2.8 # real type b = 15 # integer type c = a - b # c = -12.2, real type c = a % b # c = 2.8 - real type # comparison operators d = a>b # d = False, mixed integer and real types a = 7+3j # a - complex type d = a==7+3j # d = True # complex and real types are mixed d = a==15.8 # d = False
⇑
5. Is it possible in Python to sum a number with a string?
No, it is not. Calculation operations in expressions concern only numerical types: integer, real, complex.
For example, an attempt to sum (multiply, divide, subtract, etc.) an integer with a string will cause an error with the corresponding message
TypeError: can only concatenate str (not "int") to str
⇑
Related topics
- Operators (operations) for working with numerical objects. Operator priority table
- Mathematical (arithmetic) operators. Examples
- Comparison operators
- Bitwise operators
⇑