Let’s discuss what is indentation in python?
Indentation in Python
- Python does not support explicit grouping of statements using { } like in C, C++, Java etc.
- Indentation in python is the leading whitespace(spaces and tabs) at the beginning of the logical line.
- Indentation in python is used to compute the level of the line for grouping of statements.
- As per google, indentation in python or indent is an empty space at the beginning of a line to signal the start of new paragraph. Many computer programming language have adopted this technique to designate “paragraphs” or other logical blocks in the program.
- Tabs in indentation are replaced by spaces (as per tab value defined, can set in option menu- Configure IDLE – font/tabs – indentation width).
- Total spaces before first character determines the indentation of that line.
- Maximum level of indentation in python may vary platform to platform.
How Python Understand Indents ?
- The indentation levels of consecutive lines are used to generate INDENT and DEDENT using stack.
- Initially before reading first line, single zero is pushed on the stack which is never popped.
- Indentation in python have levels that are always in increasing order from bottom to top.
- At the beginning of each logical line, its indentation level is compared to the top of the stack.
1. If it is equal, no INDENT or DEDENT generated.
2. If it is larger, it is pushed on the stack and one INDENT is generated.
3. If it is smaller, all numbers on the stack that are larger are popped and DEDENT is generated. - At the end of the file, a DEDENT is generated for each number remaining on the stack which is larger than zero.
- It generates error in case first line indented or unexpected or inconsistent indent occurs.
Indentation in Python Example:
a = 10
b = 5
c = 15
if a > b:
if a > c:
print(a, "is larger")
else:
print(c, "is larger")
else:
print("ok")
Output:
15 is larger
Keywords in Python
- Python has reserved some words that programmer cannot use as any identifier i.e. name of variable or function.
- Such reserved words are called keywords of language.
- Python keywords (in ascending order) and their meaning tabulated below.
Keyword | Meaning/Purpose |
and | A logical operator |
as | To create an alias |
assert | For debugging |
break | To break execution sequence and transfer control out of loop |
class | To define a class |
continue | To continue to the next iteration of loop |
def | To define new function |
del | To delete an object |
elif | Used in if statement to indicate else if |
else | Used in if…else |
except | Specifies what to do when an exception occurs |
False | Boolean value |
Finally | Used in exception handling |
for | To create a loop |
from | To import specific part of a module |
global | To declare global variable |
if | To define conditional statement |
import | To import a module |
in | Checks if value present in list, tuple etc. |
is | To test if two variables are equal |
lambda | To create an anonymous function |
None | Null value |
Nonlocal | To declare non-local variable |
not | Logical operator |
or | Logical operator |
pass | A null statement that does nothing |
raise | To raise an exception |
return | Returns control and value to calling routine |
True | Boolean value |
try | To make try..except statement |
while | To create a while loop |
with | To simplify exception handling |
yield | To end a function, return a generator |
Escape Sequences in Python
- \ is a special character called escape sequence character.
- Python made following escape sequence characters available for output formatting.
Escape Sequence | Use / Purpose |
\ | New line (explicit line joining) |
\\ | Prints single backslash |
\’ | Prints single quote |
\” | Prints double quote |
\a | ASCII audible Bell |
\b | ASCII Backspace |
\f | ASCII form feed |
\n | ASCII line feed |
\r | ASCII carriage return |
\t | ASCII horizontal tab |
\v | ASCII vertical tab |
\000 | Character with octal value 000 |
\xhh | Character with hexadecimal value hh |
\uxxxx | Character with 16-bit value xxxx |
\uxxxxxxxx | Character with 32-bit value xxxxxxxx |
Example of Escape Sequences
print("one\
two\
three")
Output:
one two three
print("\\ is single slash")
Output:
\ is single slash
print("\' is single quote")
Output:
'is single quote
print("\" is double quote")
Output:
"is double quote
print("Line1\nLine2")
Output:
Line1
Line2
print("Line1\rLine2")
Output:
Line1
Line2
print("One\tTwo")
Output:
One Two
print("\101\102\103")
Output:
ABC
print("\x6e\x6f")
Output:
no
Note that, (for above 2 example)
ASCII value of A is 65 and octal equivalent of 65 is 101
ASCII value of n and o are 110 and 111 respectively and their hex equivalent are 6E and 6F.
It is guiding we the Africa Ethiopian poor people’s at least to say ‘we are the world ‘