Difference between revisions of "Python - Operators, Expressions"
(→Operators) |
|||
Line 5: | Line 5: | ||
=Operators= | =Operators= | ||
The following are a basic set of operators that most will intuitively know what they do. | The following are a basic set of operators that most will intuitively know what they do. | ||
− | * Arithmetic operators: < | + | * Arithmetic operators: <code>+ - * / //</code> |
** Note that / is floating point division (3/2 is 1.5), while // is integer division (3//2 is rounded down to 1). | ** Note that / is floating point division (3/2 is 1.5), while // is integer division (3//2 is rounded down to 1). | ||
− | * Assignment operators: < | + | * Assignment operators: <code> =</code> |
− | * Comparison operators: < | + | * Comparison operators: <code> < <= == != >= ></code> |
** Note that == tests if two values are equal, != tests if they are not equal (3 != 2 will be True, 3 == 2 will be False). | ** Note that == tests if two values are equal, != tests if they are not equal (3 != 2 will be True, 3 == 2 will be False). | ||
− | * Logical operators: < | + | * Logical operators: <code>and or not</code> |
− | * Membership operators: < | + | * Membership operators: <code>in, not in</code> |
==More Operators== | ==More Operators== | ||
The following are more operators. These may not be obvious. Some examples are given, but you may need to read through your Python text to understand these. | The following are more operators. These may not be obvious. Some examples are given, but you may need to read through your Python text to understand these. | ||
− | * Arithmetic operators: < | + | * Arithmetic operators: <code>% **</code> |
** % is remainder (10 % 3 is 1, 17 % 3 is 2), and ** is exponentiation (10**3 is 1000, 2**3 is 8). | ** % is remainder (10 % 3 is 1, 17 % 3 is 2), and ** is exponentiation (10**3 is 1000, 2**3 is 8). | ||
− | * Assignment operators: < | + | * Assignment operators: <code> += -= *= /= //= %= **= &= |= ^= >>= <<=</code> |
− | ** Each of these is shorthand. For example, < | + | ** Each of these is shorthand. For example, <code>x += 3</code> is a shorthand for <code>x = x + 3</code> |
− | * Identity operators: < | + | * Identity operators: <code>is, is not</code> |
− | ** Test whether two objects are the same, not just whether the values are the same. For simple variables/expressions, < | + | ** Test whether two objects are the same, not just whether the values are the same. For simple variables/expressions, <code>is</code> will be the same as ==, but for lists/tuples/dictionaries <code>is</code> only gives True if the two things being compared are actually the same data in memory. |
− | * Bitwise operators: < | + | * Bitwise operators: <code>& | ^ ~ << >></code> |
** These operate on the bits of a number. You need to understand binary before you can understand these. Examples: 12 & 8 evaluates to 8, 12 | 7 evaluates to 15, 12 ^ 8 evaluates to 4, ~7 evaluates to -8 (same as -7-1), 3 << 2 evaluates to 12, 12 >> 1 evaluates to 6. | ** These operate on the bits of a number. You need to understand binary before you can understand these. Examples: 12 & 8 evaluates to 8, 12 | 7 evaluates to 15, 12 ^ 8 evaluates to 4, ~7 evaluates to -8 (same as -7-1), 3 << 2 evaluates to 12, 12 >> 1 evaluates to 6. |
Revision as of 21:41, 31 December 2019
See also Python Programming - Getting Started
Following are terse descriptions for Python3 operators. For more information, see w3schools for a bit more explanation and python.org for the language reference.
Operators
The following are a basic set of operators that most will intuitively know what they do.
- Arithmetic operators:
+ - * / //
- Note that / is floating point division (3/2 is 1.5), while // is integer division (3//2 is rounded down to 1).
- Assignment operators:
=
- Comparison operators:
< <= == != >= >
- Note that == tests if two values are equal, != tests if they are not equal (3 != 2 will be True, 3 == 2 will be False).
- Logical operators:
and or not
- Membership operators:
in, not in
More Operators
The following are more operators. These may not be obvious. Some examples are given, but you may need to read through your Python text to understand these.
- Arithmetic operators:
% **
- % is remainder (10 % 3 is 1, 17 % 3 is 2), and ** is exponentiation (10**3 is 1000, 2**3 is 8).
- Assignment operators:
+= -= *= /= //= %= **= &= |= ^= >>= <<=
- Each of these is shorthand. For example,
x += 3
is a shorthand forx = x + 3
- Each of these is shorthand. For example,
- Identity operators:
is, is not
- Test whether two objects are the same, not just whether the values are the same. For simple variables/expressions,
is
will be the same as ==, but for lists/tuples/dictionariesis
only gives True if the two things being compared are actually the same data in memory.
- Test whether two objects are the same, not just whether the values are the same. For simple variables/expressions,
- Bitwise operators:
& | ^ ~ << >>
- These operate on the bits of a number. You need to understand binary before you can understand these. Examples: 12 & 8 evaluates to 8, 12 | 7 evaluates to 15, 12 ^ 8 evaluates to 4, ~7 evaluates to -8 (same as -7-1), 3 << 2 evaluates to 12, 12 >> 1 evaluates to 6.