Python Basics: Comments, Errors, Variables, Reserved Words,
and Indentation
1. Comments in Python
Comments are notes for humans, ignored by the Python interpreter.
Single-line comment → use #
# This is a single-line comment
print("Hello World")
Multi-line comment → use triple quotes ''' or """
""" This is a multi-line comment """
print("Hello again")
2. Error Messages in Python
Errors happen when Python cannot understand or execute code.
Syntax Error – wrong code structure
print("Hello" # Missing closing parenthesis
Name Error – using an undefined variable
print(x) # x not defined
Type Error – wrong operation on incompatible types
print("2" + 2) # Can't add string + integer
Value Error – right type, wrong value
int("abc") # Can't convert letters to integer
Index Error – accessing out-of-range list index
mylist = [1, 2, 3] print(mylist[5])
3. Variables in Python
Variables are names that store values.
Examples:
x = 10 name = "Priyanka" pi = 3.14
Rules for naming:
■ Can contain letters, digits, and _
■ Must start with a letter or _
■ Cannot start with a digit
■ Cannot be a reserved word
Dynamic typing example:
x = 5 # integer x = "five" # now string
4. Reserved Words in Python
These are keywords with special meaning – you cannot use them as variable names.
False, None, True, and, as, assert, 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
5. Indentation in Python
Indentation refers to spaces at the beginning of a line. In Python, indentation is very important
because it defines code blocks instead of curly braces {}.
Example with correct indentation:
if True: print("Hello") print("Indented block")
Incorrect indentation will raise an IndentationError:
if True: print("Hello") # ■ No indentation – Error