Indentation refers to the spaces at the beginning of a code line, without proper indenting the Python generating and the code will not get compiled. Whitespace is used for indentation.
Indentation telling to Python interpreter that the group of statements belongs to a particular block of code in other words highlight the blocks of code. A block is a grouping of statements for a specific purpose.
In C, C++, Java use braces { }
to define a block of code.
All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.
Python does not know which statement to execute next or which statement belongs to which block.
Ex:-
>>>if 5 > 2:
print("Five is greater than two!")
output :-
Five is greater than two!
Ex:-
>>>if 5 > 2:
print("Five is greater than two!")
- output:-:-
%Run a1.py
Traceback (most recent call last):
File "C:\Users\panka\AppData\Local\Programs\Thonny\lib\ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "C:\Users\panka\OneDrive\Documents\a1\a1.py", line 1
if 5 > 2:
^
IndentationError: unexpected indent
EX:-
a = input('Enter the easiest programming language: ') //statement 1
if a == 'python': //statement 2
print('Yes! You are right') //statement 3
else: //statement 4
print('Nope! You are wrong') //statement 5
output:-
%Run a1.py
Traceback (most recent call last):
File "C:\Users\panka\OneDrive\Documents\a1\a1.py", line 1
a = input('Enter the easiest programming language: ') //statement 1
^
SyntaxError: invalid syntax
Example :-
a = input("Enter the easiest programming language: ")
if a == "python" :
print("Yes! You are right")
else:
print("Nope! You are wrong")
output :-
>>> %Run a1.py
Enter the easiest programming language: python
Yes! You are right
>>> %Run a1.py
Enter the easiest programming language: c
Nope! You are wrong
The number of spaces is up to you as a programmer, but it has to be at least one.
You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:
Example :-
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
output:-
File "C:\Users\panka\AppData\Local\Programs\Thonny\lib\ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "C:\Users\panka\OneDrive\Documents\a1\a1.py", line 3
print("Five is greater than two!")
^
IndentationError: unexpected indent
===============================================================
Atoms
Atoms are the most basic elements of expressions. The simplest atoms are identifiers or literals. Forms enclosed in reverse quotes or in parentheses, brackets, or braces are also categorized syntactically as atoms.
The syntax for atoms is:
atom: identifier | literal | enclosure
enclosure: parenth_form|list_display|dict_display|string_conversion
===============================================================
0 Comments