TOPIC: Python Indentation, Keywords and Operators
Indentation:
A logical block of code starts with indentation and ends with first indented line. It is used to
define the scope or block of the code. Python uses indentation instead of curly braces or keywords
like "begin" and "end" to indicate the start and end of a block. The standard convention in Python
is to use four spaces for each level of indentation. Indentation helps in improving code readability
and makes it easier to understand the flow and structure of the program. The use of proper
indentation enhances collaboration among programmers working on the same project by making it easier
to read and understand each other's code.
Example
The indentation of 4 spaces defines that the print function is working for the if statement only
Keywords or Reserved Words:
They are the words whose meanings are already known to the compiler and also called as Reserved
Words. Python has 35 keywords and they are as follows:
False | None | True | and | as |
---|
assert | async | await | break | class |
---|
continue | def | del | elif | else |
---|
except | finally | for | from | global |
---|
if | import | in | is | lambda |
---|
nonlocal | not | or | pass | raise |
---|
return | try | while | with | yield |
---|
These keywords cannot be used as a variable and it will give a syntax error as
We won't be able to use these keywords except for the purpose they are created for. We cannot use these keywords as variable names, function names, or identifiers for other purposes in your code
Operators:
Operators are used to perform operations on variables and values.
Operator | Name | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponentiation | x ** y |
// | Floor Division | x // y |
Operation | Example |
---|---|
= | x = 5 |
+= | x += 3 |
-= | x -= 3 |
*= | x *= 3 |
/= | x /= 3 |
|= | x |= 3 |
%= | x %= 3 |
//= | x //= 3 |
**= | x **= 3 |
&= | x &= 3 |
>>= | x >>= 3 |
<<= | x <<= 3 |
Operator | Name | Example |
---|---|---|
== | Equal | x==y |
!= | Not Equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than equal to | x >= y |
<= | Less than equal to | x <= y |
Operator | Functions | Example |
---|---|---|
and | returns true if both statements are true | x < 5 and x> 10 |
or | returns true if one of the statements is true | x < 5 or x < 4 |
not | reverse then result; the result returns False if the result is True | x < 5 or x < 4 |
Operator | Description | Example |
---|---|---|
is | returns True if both variable are the same object | x = ['a', 'b'] y = ['a', 'b'] z = x print(x is z) # returns true print(x is y) # returns False print(x==y) # returns True |
is not | returns True id both variable are not the same object | print(x is not z) # returns False print(x is not y) # returns True print(x != y) # returns False |
Operator | Description | Example |
---|---|---|
in | returns True if a sequence with the specified value is presented in the object |
x = ['a', 'b'] print('b' in x) |
not in | returns True if a sequence with the specified value is not present in the object |
print('c' not in x) |
Operator | Name | Description | Example |
---|---|---|---|
& | AND | sets each bit to 1 if both bits are 1 | x & y |
| | OR | sets each bit to 1 if one of the two bits is 1 | x | y |
^ | XOR | sets each bit to 1 if only one of two bits is 1 | x ^ y |
~ | NOT | inverts all the bits | ~x |
<< | Zero fill left side | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
x << 2 |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left and let the rightmost bits fall off |
x >> 2 |
Operator Precedence:
Operator precedence describes the order in which operators are performed
Operator | Description |
---|---|
() | parenthesis |
** | exponentiation |
+x, -x, ~x | unary plus, unary minus, bitwise NOT |
*, /, //, % | Multiplication, Division, Floor Division, Modulus |
+, - | Addition, Subtraction |
<<,>> | bitwise left and right shift |
& | bitwise AND |
^ | bitwise XOR |
| | bitwise OR |
==, !=, >, >=, <, <=, is, is not, in, not in | comparison, identity and membership |
not | logical NOT |
and | logical AND |
or | logical OR |
Code as discussed in the video:
You can copy and run this code