Python Basics
Indentation,Comments, Tokens
April 19, 2026
Indentation - Indentation is used to define blocks of code. It indicates to the Python
Interpreter that a group of statements belong to the same block.
• Indentation is created using tabs or spaces (Use of four spaces is the commonly
accepted convention).
• Python expects the indentation level to be consistent within the same block.
Inconsistency causes an IndentationError.
Examples
print("I have no Indentation") #NoError
<press tab> print("I have tab Indentation") #IndentationError:unexpected indent
Comments - It describes why a piece of code was written. Python Interpreter does
not execute comments.
#This is a single line comment in python
1. Block comments example
# increase price by 5%
price = price * 1.05
2. Inline comments example
price = price * 1.05 # increase price by 5%
Python does not support Multiline comments. Instead you can use multiline doc-
strings. Multiline docstrings starts (“ ” “) and end (“ “ “) with triple quotes.
Continuation of statements
1. Explicit continuation - A long statement can span multiple lines by using the
backslash (\) character.
total = 10 + 20 + 30 + \
40 + 50
print(“Total=”,total)
2. Implicit continuation - Using brackets. Safer and cleaner to use this.
total = (10 + 20 + 30 +
40 + 50)
print(“Total=”,total)
Tokens (Lexical Unit) in Python - Smallest individual unit in a program. Python
interpreter breaks every line of code into tokens before executing it.
Types of Tokens:
2
(i) Keywords (ii) Identifiers (Names) (iii) Literals (Values) (iv) Operators (v) Punc-
tuation
(i) Keywords - Reserved word in the programming language having a special meaning.
In current version, there are 35 keywords.
RULE :
• You can’t use them as variable names.
• You can’t change the case of letters, you’ve to write it as it is.
Words like – if, else, elif, for, while, break, continue – used for logic & loops.
Words like – def, class – used to create functions & classes.
*** To find current keyword list, use the code:
import keyword
print([Link])
(ii) Identifiers - The name you give to a variable, function or object.
x=5
y=10
sum = x+y #x, y, sum are identifiers
print(sum)
RULE :
DO’s -
• can contain Letters (a-z, A-Z), Numbers (0-9), Underscore (_).
• Must start with a Letter or Underscore. Example : _Name1 = “xyz”, _2name
= “xyz” (both are valid)
DONT’s -
• No spaces allowed. Example : My Name = “xyz” (invalid)
• Can’t start with a number. Example : 1Name = “xyz” (invalid)
• Can’t use Python Keywords. Example : if=10 (invalid)
• Hyphen (-) & all special characters (except Underscore) are invalid. Example
: My-name@ = “xyz” (invalid)
(iii) Literals - Actual value/data you assigned to a variable.
x=10
name=”xyz” #x, name, y are Identifiers
3
y=3.5 #10, xyz, 3.5 are Literals
Types of Literals :
Numeric Literals - integer, float, complex (Example - 10, -9, 3.14, -0.001, 4+6j).
Also supports different Number Bases (Example - deci1=42, deci2=-5, octal=0o12,
binary=0b1010, hexadecimal=0xA).
Leading 0s is decimal int literals are not permitted (a=042 is invalid).
To increase readabilty, a=1,000 (invalid) ; a=1_000 (valid).
String Literals - Text enclosed in quotes (Example - “Hello” , ’Python’).
Single Line Strings - The strings that terminate in single line. Example - print(“He
said”,’Hello’)
Multi Line Strings - The strings that are spread across multiple lines. Example -
a=’Hello \
World’
len(a)
Escape Sequence - Python allows you to have some characters that can’t be typed di-
rectly from keyboard (Example- backspace, tabs, newlines, characters with octal/hex
etc in string values). Escape Sequence is represented by a backslash (\) character.
print(“Hello\nWorld”) #NewLine
print(“Hello\tWorld”) #HorizontalSpacing
print(“Helloo\b”) #Backspace- Removes the previous character
print(“It\’s Python”) #Single-quoted strings
print(“He said \”Hello\””) #Double-quoted strings
Boolean Literals - Logical values (Example - True – any value except zero, False –
only zero).
Boolean Literals are case sensitive. (True, False) – valid ; (true,false) – invalid
Commonly used in conditions & comparisons. print(5 > 3) #True ; print(3 == 7)
#False
Special Literals - Represents no value (Example - None).
It is different from 0 (number), False (Boolean), “ “ (Empty String)
print (type(None)) #Nonetype
Collection Literals - Group of Values (Example - List, Tuple, Dictionary, Set).
4
(iv) Operators - Operators are used to perform operations on values and variables.
Arithmetic Operators - Basic mathematical operations like + - * / % ** //
Precedence (PEM/DA/S)– () —> ** —–> * , /, //, % —–> +, -
Operators with the same precedence are evaluated from Left to Right (Except the **
(Exp.)i.e Right to Left)
a = 15
b=6
print(“Addition:”, a + b)
print(“Subtraction:”, a - b)
print(“Multiplication:”, a * b)
print(“Division:”, a / b)
print(“Modulus:”, a % b)
print(“Exponentiation:”, a ** b)
print(“Floor Division:”, a // b)
Comparison Operators - By comparing values, it either returns True or False accord-
ing to the condition.
a = 15
b=6
print(a > b) #True
print(a < b) #False
print(a == b) #False
print(a != b) #True
print(a >= b) #True
print(a <= b) #False
Logical Operators - Perform Logical AND, Logical OR and Logical NOT operations.
It is used to combine conditional statements.
Precedence - Logical NOT —> Logical AND ——> Logical OR
a = True
b = False
print(a and b)
5
print(a or b)
print(not a)
Bitwise Operators - Act on bits and perform bit-by-bit operations. These are used
to operate on binary numbers.
Assignment Operators - These are used to assign values to the variables.
count = 0
count += 2 #count = count+2
print(count)
Membership Operators – “in” and “not in” are the membership operators that are
used to test whether a value or variable is in a sequence or not.
(v) Punctuation - Define structure ( (),{},[] )
Separate items (, ;)
Indicate blocks (:)
Access Members (.)