๐ Chapter 5: Python Fundamentals
This chapter lays the foundation for the Python you will write in Class 11: the Python character set, tokens (keywords, identifiers, literals, operators and punctuators), the basic structure of a Python program, variables and assignments, and simple input and output. Pay particular attention to tokens, the rules for identifiers, and variables โ the later chapters build on them.
5.1 Introduction
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. It is named after the British comedy series "Monty Python's Flying Circus" โ not the snake! ๐
Why Python?
Simple readable syntax, free & open source, platform independent, huge standard library, supports multiple paradigms
Feels like writing EnglishKey Facts
Creator: Guido van Rossum, Released: 1991, Type: Interpreted high-level language, Extension: .py
print("Hello")Python is an interpreted language, meaning the Python interpreter converts source code into machine code line by line at runtime, rather than compiling the whole program at once like C/C++.
5.2 Python Character Set
A character set is the set of valid characters that a language can recognize. A character is the smallest unit of a program.
Letters
Uppercase and lowercase alphabets
AโZaโzDigits
Numeric digits 0 to 9
0โ9Special Symbols
Symbols used in code for operations and punctuation
+ - * / % = ! & < > ( ) [ ] { }: ;Whitespaces
Characters that create space in code
Blank spacetabnewlinecarriage returnPython 3 supports the full Unicode character set, which means you can even use characters from Indian languages (like Bengali or Hindi) in strings and, in special cases, identifiers!
5.3 Tokens
The smallest individual unit in a Python program is called a Token (or lexical unit). The Python interpreter breaks every line of code into tokens before processing it.
flowchart TD
A[Python Tokens] --> B[Keywords]
A --> C[Identifiers]
A --> D[Literals]
A --> E[Operators]
A --> F[Punctuators]
class A normalNode
class B,C,D,E,F tryNode
5.3.1 Keywords
Keywords are reserved words that have special meaning to the Python interpreter. They cannot be used as variable names, function names, or any other identifier.
Python 3 has 35 keywords. All keywords are in lowercase except True, False, and None.
Sample Keywords
False, None, True, and, as, break, class, continue, def, elif, else, for, if, import, in, is, not, or, return, while and more
35 keywords totalMemory Trick
Keywords are VIPs of Python โ they already have a fixed job assigned
Can't hire if / for / class for a different roleprint, input, len, range are NOT keywords โ they are built-in functions. Students often confuse this in exams! You can (though should not) use them as variable names.
5.3.2 Identifiers (Names)
An identifier is the name given to entities like variables, functions, classes, modules, etc.
Allowed
Start with a letter or underscore, followed by letters/digits/underscores, case-sensitive, any length
age_namestudent1Not Allowed
Cannot start with a digit, cannot contain special symbols or spaces, cannot be a keyword
1namena@memy nameclassA typical question gives a list of 4โ5 names and asks "Which are valid identifiers?" Always check: starts with digit? has space? has special symbol? is it a keyword?
5.3.3 Literals / Values
A literal is a raw data value directly written in the source code.
Numeric Literals
Integer, float, and complex number values
103.143+5jString Literals
Text written inside quotes
'Hello'"Hi"'''multi-line'''Boolean Literals
Only two values, capital first letter
TrueFalseSpecial Literal
Represents absence of a value
NoneNumeric literals can further be written in different number systems:
| System | Prefix | Example |
|---|---|---|
| Binary | 0b or 0B |
0b1010 |
| Octal | 0o or 0O |
0o17 |
| Decimal | none | 25 |
| Hexadecimal | 0x or 0X |
0x1A |
A single character in Python (e.g. 'A') is not a separate data type โ it is simply treated as a string of length 1.
5.3.4 Operators
Operators are special symbols that carry out arithmetic or logical computation. The value(s) the operator acts on are called operands.
Arithmetic
Perform mathematical calculations
+ - * / // % **Relational
Compare two values
== != > < >= <=Logical
Combine conditional statements
and or notAssignment
Assign or update values
= += -= *= /=Membership
Test membership in a sequence
in not inIdentity
Compare memory identity
is is notDifference between / and //:
/โ True division โ always gives a float โ7 / 2โ3.5//โ Floor division โ gives the floor (integer part) โ7 // 2โ3
5.3.5 Punctuators
Punctuators are symbols used to organize sentence structure, indicate operations, and structure the program. Examples:
' " # \ ( ) [ ] { } @ , : . = ; += -= *= /=
Punctuators are not the same as operators โ a punctuator's main role is structural (grouping, separating, terminating), even though some symbols overlap (like =).
5.4 Barebones of a Python Program
A minimal Python program has a very simple, indentation-based structure โ no semicolons, no curly braces required.
# This is my first Python program
print("Hello, World!")
Indentation
Whitespace defines blocks of code instead of { }, must be consistent
Mandatory โ not optionalComments
Notes ignored by the interpreter
Single-line #multi-line ''' ... '''Statements
A logical line of code
Usually one statement per lineCase Sensitivity
Python treats uppercase and lowercase differently
Print โ printInconsistent indentation (mixing tabs and spaces) causes an IndentationError. Always use 4 spaces per indentation level consistently.
flowchart TD
A[Start Program] --> B[Comment / Docstring]
B --> C[Import Statements]
C --> D[Statements with proper Indentation]
D --> E[End Program]
class A normalNode
class B,C,D tryNode
class E normalNode
5.5 Variables and Assignments
A variable is a named location in memory used to store a value that can change during program execution.
5.5.1 Creating a Variable
In Python, you don't need to declare a variable's type separately โ a variable is created the moment you assign a value to it.
age = 17 # integer variable
name = "Riya" # string variable
height = 5.4 # float variable
Unlike C/C++, Python has no separate declaration statement. Assignment is declaration!
5.5.2 Multiple Assignments
Python allows assigning values to multiple variables in different flexible ways:
| Style | Syntax | Example |
|---|---|---|
| Same value to many variables | a = b = c = value |
a = b = c = 10 |
| Different values in one line | a, b, c = v1, v2, v3 |
a, b, c = 10, 20, 30 |
| Swapping without a third variable | a, b = b, a |
a, b = b, a |
Swapping two variables without a third variable using a, b = b, a can be asked as a one-mark or output-based question.
5.5.3 Variable Definition
A variable in Python is essentially a reference (name) bound to an object stored somewhere in memory. Python variables have:
| Property | Meaning |
|---|---|
| Name | The identifier used to refer to the variable |
| Value | The data currently stored |
| Type | Determined automatically from the value assigned |
| id() | Unique memory address identifier of the object |
x = 10
print(id(x)) # shows the memory address of the object 10
print(type(x)) # shows <class 'int'>
5.5.4 Dynamic Typing
Python is a dynamically typed language โ the type of a variable is decided at runtime, based on the value assigned, and can change if reassigned.
x = 10 # x is int
x = "Hello" # now x becomes str โ perfectly valid!
x = 3.14 # now x becomes float
This is different from statically typed languages like C/C++/Java, where the data type of a variable is fixed at the time of declaration and cannot change.
Memory Trick
Static is like a steel pipe with a fixed shape forever
Dynamic is like water taking the shape of its containerCommon Confusion
Dynamic typing does not mean "no data types"
Python is strongly typed too โ "5" + 5 still fails5.5.5 l-value and r-value โฌ ๏ธ
In an assignment statement, the = sign does not mean "equal to". It means "evaluate the right side, then store the result in the name on the left".
total = marks + 5
โโโโโ โโโโโโโโโ
l-value r-value
| l-value | r-value |
|---|---|
Appears on the left of = |
Appears on the right of = |
| Must be something that can store a value โ a variable name | Can be a literal, a variable or an expression that produces a value |
e.g. x in x = 10 |
e.g. 10, a + b, y * 2 |
a = 10 # a is the l-value, 10 is the r-value
b = a * 2 # b is the l-value, a * 2 is the r-value
a = a + b # the r-value (10 + 20) is worked out first, then stored in a
print(a, b)
Output:
30 20
10 = x and a + b = 20 both give SyntaxError: cannot assign to literal / cannot assign to expression. The left side must always be a name.
"What are l-value and r-value? Give an example." โ 2-mark question!
Answer: In an assignment statement, the l-value is the object on the left of = that receives a value (a variable name), and the r-value is the expression on the right of = whose value is assigned. In total = a + b, total is the l-value and a + b is the r-value.
5.6 Simple Input and Output
5.6.1 Reading Numbers
The built-in input() function always returns a string, even if the user types a number. To use it as a number, you must explicitly type-cast it.
age = input("Enter your age: ") # age is a string, e.g. "17"
age = int(input("Enter your age: ")) # age is now an integer 17
height = float(input("Enter height: ")) # for decimal numbers
Forgetting to type-cast input() before doing arithmetic โ TypeError: unsupported operand type(s). Always wrap with int() or float() when expecting numbers!
input() Key Facts
Always returns type str, takes an optional prompt message, pauses execution until Enter is pressed
input("Enter name: ")Type Casting Functions
Convert string input into the required type
int()float()str()5.6.2 Output Through print() Function
The print() function displays output on the screen. It has several useful optional parameters:
print("Hello", "World", sep="-", end="!\n")
# Output: Hello-World!
| Parameter | Purpose | Default |
|---|---|---|
sep |
Separator between multiple values | ' ' (space) |
end |
What to print at the end | '\n' (newline) |
*objects |
One or more values to print | โ |
Modern f-strings are the CBSE-recommended way to combine text and variables:
name = "Amit"
marks = 95
print(f"{name} scored {marks} marks")
Memory Trick
sep stands for spaces between words
end stands for ending punctuation of the print statementPractice predicting output for print() statements with custom sep and end โ these are typical 1โ2 mark output-based questions.
๐ Board Exam Practice Zone
1 Mark Questions
- Name the creator of Python.
- What will
type(True)return? - Is
input()output astr,int, orfloatby default? - What is the default value of
sepinprint()? - Which of these is a valid identifier:
2fruit,_fruit,for,fruit#1?
2 Mark Questions
- Differentiate between Keywords and Identifiers with one example each.
- Differentiate between
/and//operators with example. - What is dynamic typing? Explain with an example.
- Write the rules for naming a valid Python identifier.
3 Mark Questions
- Write a program to swap two numbers without using a third variable.
- Explain any three types of literals in Python with examples.
- Predict the output:
a, b, c = 5, 10, 15
print(a, b, c, sep="*", end="#")
4 Mark / Output-Based Questions
- Identify errors, if any, in the following code and rewrite it correctly:
1name = input(Enter name)
Age = int(input("Enter age"))
print(Name, "is", age, "years old")
- Write a program that takes two numbers as input and prints their sum, difference, product, and floor division result, each on a separate line.
Whenever a question mixes keywords vs identifiers or type-casting of input(), examiners are testing whether you actually understand the rules, not just memorized examples. Always apply the rule step-by-step rather than guessing.
Must Remember
Python has 35 keywords, input() always returns a string, identifiers cannot start with a digit or be a keyword, Python is dynamically typed
Indentation replaces { }Common Pitfalls
Confusing print/input with keywords, forgetting to type-cast numeric input, mixing tabs and spaces
A single character is just a string of length 1