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
if 3 < 5:
print("5 is greater then 3") # here we get an indentation
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
from = "India"
File "", line 1
from = "India"
^
SyntaxError: invalid syntax
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:
# Python Indentation
def sample():
new = "Hello" # indented with 4 spaces
print(new)
sample()
# Python Keywords
continue = "Coding" # will give error
print(yield)
Python Operators
# Arithmetic Operators - +, -, *, /, %, //, **
num1 = 20
num2 = 3
print(num1+num2)
print(num1-num2)
print(num1*num2)
print(num1/num2)
print(num1%num2)
print(num1//num2)
print(num1**num2)
# Assignment Operators - =, +=, -=, *=, /=, //=, %=, **=, &=, >>=, <<=
num1 = 20
num1 += 3
print(num1)
# Comparison operators - ==, !=, <, >, <=, >=
num1 = 20
num2 = 5
print(num1<=num2)
# Logical Operators - and, or, not
num1 = 20
num2 = 5
print(num1<30 and num2>3) # and means both conditions must be true for getting True
print(num1==30 or num2>3) # or means either one must be true to get True
print(not(num1!=num2))
# Identity Operators - is, is not
num1 = ['a', 'b']
num2 = ['a', 'b']
num3 = num1
print(num1 is num3)
print(num1 is num2)
print(num1 is not num3)
print(num1 is not num2)
# Membership operator - in, not in
num1 = [1, 2, 3, 4, 5]
print(5 not in num1)
# Bitwise operator - &, |, >>, <<, ~, ^
num1 = 5
num2 = 6
print(num1>>2)
# Operator Precedence
Test - solve 5*4+4-5/6(3**2)**3
You can copy and run this code