Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Unit 2 – Python Programming  ·  Lesson 04

Python Basics: Tokens, Variables, Types

Why Python for Informatics Practices?

Informatics Practices is all about working with data — collecting it, storing it, analysing it and presenting it. Python is the most popular language for data work because it is simple to read, free, and has powerful libraries such as NumPy (numbers), Pandas (tables) and Matplotlib (charts), which you will use in Classes XI and XII.

Python basicsClass XILists, dictionariesClass XINumPyClass XIPandas + MatplotlibClass XII
Your Python journey in IP

Execution modes

Interactive mode

  • Python shell with >>> prompt
  • Type one statement, see the result immediately
  • Like a calculator; nothing is saved

Script mode

  • Write the program in a .py file
  • Save and run it (F5 in IDLE)
  • Used for complete programs
Two ways to run Python
Python shell
>>> 45.5 * 3
136.5
>>> 'IP' * 4
'IPIPIPIP'
>>> 100 / 8
12.5

First program

Python
# Shop bill – our first IP program
item = 'Notebook'
price = 45.5
qty = 3
bill = price * qty
print('Item:', item)
print('Bill: Rs.', bill)
Item: Notebook Bill: Rs. 136.5

Tokens: the smallest units

Just as a sentence is made of words and punctuation, a Python statement is made of tokens.

tokens →bill=price*qty+20
bill = price * qty + 20 → 7 tokens
Token typeMeaningExamples
KeywordReserved word with fixed meaningif, else, for, while, True, None
IdentifierName given by the programmerbill, price, total_marks
LiteralFixed value20, 45.5, 'Pen', True
OperatorSymbol that does an operation+ - * / == and
PunctuatorSymbols that structure code( ) [ ] { } , : . ' #
Five kinds of tokens

Keywords and identifiers

Keywords are reserved words (35 in modern Python) — you cannot use them as names. Identifiers are names for variables, functions and so on. Rules for identifiers: letters, digits and underscore only; cannot start with a digit; cannot be a keyword; case-sensitive.

Valid identifiers

  • price
  • total_bill
  • _tax
  • item2
  • Qty (different from qty)

Invalid identifiers

  • 1item – starts with digit
  • total bill – space
  • while – keyword
  • rate% – symbol
  • gst-rate – hyphen
Name check

Literals

A literal is a raw value written directly in a program.

LiteralExamples
Numeric – integer25, -7, 0o17 (octal), 0x1F (hex), 0b101 (binary)
Numeric – float3.14, -0.5, 2.5e3
Numeric – complex3+2j
String'Pen', "Delhi", '''multi-line'''
BooleanTrue, False
SpecialNone
Python
print(0o17, 0x1F, 0b101)          # octal, hex, binary integer literals
print(2.5e3, 3 + 2j)              # float in exponent form, complex
print('It\'s', "say \"hi\"")       # escape quotes with backslash
print('Line1\nLine2\tTabbed')      # \n new line, \t tab
print(True, False, None)          # boolean and special literals
15 31 5 2500.0 (3+2j) It's say "hi" Line1 Line2 Tabbed True False None

Escape sequences start with a backslash: \n new line, \t tab, \\ backslash, \' and \" quotes.

Variables and assignment

A variable is a name that refers to a value stored in memory. It is created the first time you assign to it. Python is dynamically typed:

Python
score = 50
print(score, type(score))
score = 'fifty'          # same name, now a string (dynamic typing)
print(score, type(score))
50 <class 'int'> fifty <class 'str'>
Python
a = b = c = 100          # same value to many variables
x, y, z = 'Red', 'Green', 'Blue'
print(a, b, c)
print(x, y, z)
print(id(a) == id(b))    # both names refer to one object
100 100 100 Red Green Blue True
a, b, cthree namesrefer to100one int object
a = b = c = 100 – all names refer to the same object

Comments

Comments explain code to humans and are ignored by Python. Start a comment with #. Write comments for formulas and important steps, e.g. # GST at 18% is added to the price.

Data types

🔢
Numberint (25), float (45.5), complex (3+2j)
✅
BooleanTrue / False – result of conditions
🔤
String (str)Text in quotes: 'Notebook'
📋
ListOrdered, changeable: [45, 102, 8]
📍
TupleOrdered, fixed: (28.61, 77.21)
📒
DictionaryKey: value pairs: {'runs': 102}
🔘
SetUnique items: {1, 2, 3}
∅
NoneNo value
Python data types
Python
item, price, qty, in_stock = 'Notebook', 45.5, 3, True
print(type(item), type(price), type(qty), type(in_stock))
runs = [45, 102, 8]                  # list
player = {'name': 'Virat', 'runs': 102}   # dictionary
location = (28.61, 77.21)            # tuple
print(runs, player['runs'], location[0])
print(type(runs), type(player), type(location))
<class 'str'> <class 'float'> <class 'int'> <class 'bool'> [45, 102, 8] 102 28.61 <class 'list'> <class 'dict'> <class 'tuple'>

Mutable types can be changed after creation (list, dictionary, set); immutable types cannot (int, float, bool, str, tuple).

Summary & practice

  • Python is the main language for data work (NumPy, Pandas, Matplotlib).
  • Tokens: keywords, identifiers, literals, operators, punctuators.
  • Variables are created by assignment; Python is dynamically typed.
  • Types: int, float, complex, bool, str, list, tuple, dict, set, None.
✅ Practice

1) Count the tokens in avg = (a + b) / 2. 2) Identify the type of each literal: '2026', 2026, 20.26, False. 3) Write a program that stores the name, price and quantity of two items and prints the total bill.

Answers: 1) 9 tokens: avg = ( a + b ) / 2. 2) str, int, float, bool.

Frequently Asked Questions

What are tokens in Python?
Tokens are the smallest meaningful units of a program: keywords, identifiers, literals, operators and punctuators (delimiters such as brackets, commas and colons).
What is a literal?
A fixed value written directly in the code, such as 25, 3.14, "Hello", True or None.
What is dynamic typing?
In Python the type of a variable is decided by the value assigned to it at run time, and the same variable can later hold a value of another type.
← Class 11 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicOperators, Input-Output & Debugging Your progress: 4 / 20 (20%) · Lesson 5 / 20 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free