Operators in Python and how to use them, example codes
Table of Contents
Operators in Python:
Operators in Python- Python knows essentially the same operators as most other programming languages. We would like to point out some special features:
- Division: The operator / behaves differently in Python 2 and Python 3. In Python 2, if a and b are integers, a / b division returns the result of an integer. In Python3, however, there is always a floating point division carried out! If you explicitly want integer division, you must use the // operator.
- Link assignment and calculation: Assignments can be made with basic arithmetic operations get connected. This means that a = a + 1 can also be formulated in the form a + = 1. This short notation is not only permitted for the basic arithmetic operations, but for almost all Python operators. In contrast to other programming languages but a++ and a– in the sense of a = a + 1 and a = a-1 are not permitted.
- Multiple comparisons: With the comparison operators <,> etc., multiple comparisons are also possible. For example, 10 <= x <= 20 tests whether x has a value between 10 and 20. Internally, all comparisons are linked with a logical And, i.e., 10 <= x <= 20 corresponds to 10 <= x and x <= 20.
- Compare content: == tests whether two expressions have the same content, i.e. for example, whether the variable x has the value 3 (if x == 3: …) or whether the character string s matches ‘abc’ (if s == ‘abc’: …).
- Compare objects: In contrast, the operator a is b checks whether the Variables a and b refer to the same object. Even if a == b. So a is b corresponds to a deeper equality than a == b.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Table 1: Arithmetic, string and comparison operators
operator | Function |
+ – | sign |
+ – * / | Basic arithmetic; / returns float results in Python 3! |
// | integer division (20 // 6 results in 3.) |
% | Remainder of the whole number division (20% 6 results in 2.) |
** | Exponential function or superscript (2 ** 8 results in 256.) |
+ * | Connect or multiply strings (‘ab’ * 2 results in ‘abab’.) |
% | Format character string (printf syntax) |
= | Assignment (var = 3) |
+= | Assignment and addition (var + = 3 corresponds to var = var + 3.) |
-= | Assignment and subtraction |
*= | Assignment and multiplication |
/= | Assignment and division |
== | Test equality (if a == 3: …) |
!= | Test inequality |
< > <= >= | smaller, larger, smaller-equal, greater-equal |
Is | test whether two variables point to the same object |
is not | test whether two variables point to different objects |
Table 2:Binary and logical operators
operator | Function |
& | | “binary and” and “binary or” |
^ | binary exclusive-or |
~ | binary not |
<< | Shift binary to the left (2 << 4 results in 32.) |
>> | shift binary to the right (768 >> 2 results in 192.) |
Or | logical or |
And | logical and |
not | logical not |
Table 3: Operators for sets
operator | Function |
| | Association: {1, 2} | {2, 3} results in {1, 2, 3}. |
– | Difference: {1, 2} – {2, 3} yields {1}. |
& | Intersection: {1, 2} & {2, 3} returns {2}. |
^ | Symmetrical difference: {1, 2} ^ {2, 3} yields {1, 3}. |
Python Assignments
Ordinary assignments of the type variable = expression exist in every programming language. However, Python also knows a few unusual variants. For example, you can assign the same content to several variables at once. Python works from right to left. That is, the example below will be performed the following assignments in sequence: c = 16, then b = c and finally a = b. Finally, all three variables point to the same object in memory that the represents integer 16.
a = b = c = 16 # assign the same value to three variables
If you have multiple variables in compact notation different values you can use lists or tuples. For example the following assignments work:
1 2 3 4 5 |
a, b = 2, 3 [e, f, g] = [7, 8, 9] e, f, g = [7, 8, 9] # equivalent |
You can also use this type of assignment to determine the content of two variables to swap. For most other programming languages, you would need a third, temporary variable.
x, y = y, x # Swap x and y
A special variant of list and tuple assignments concerns the case that there are fewer variables on the left than there are list items on the right. In this case you can prefix a variable with an asterisk. Of these variables then all surplus elements are assigned as a list, like the following example shows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> a, * b, c = [1, 2, 3, 4, 5] >>> a 1 >>> b [2, 3, 4] >>> c 5 |
Extended sequence unpacking
This type of assignment is only available from Python 3 and is called extended Sequence Unpacking. The mechanism can also be used for strings:
1 2 3 4 5 6 7 8 9 |
>>> a, * b = '123456' >>> a '1' >>> b ['2', '3', '4', '5', '6'] |
Make sure that a maximum of one variable is marked with an asterisk. The number of elements on the right side of the expression must be at least as large as the number of variables not marked by the asterisk. The star variable is then assigned an empty list, ie []. Lie to A ValueError occurs before a few list elements or characters.
1 2 3 4 5 6 7 |
>>> * a, b, c = [1, 2] >>> a [] >>> * a, b, c = [1] |
Traceback (most recent call last): …
ValueError: need more than 1 values to unpack