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.
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
.pyfile - Save and run it (F5 in IDLE)
- Used for complete programs
>>> 45.5 * 3 136.5 >>> 'IP' * 4 'IPIPIPIP' >>> 100 / 8 12.5
First program
# Shop bill – our first IP program
item = 'Notebook'
price = 45.5
qty = 3
bill = price * qty
print('Item:', item)
print('Bill: Rs.', bill)Tokens: the smallest units
Just as a sentence is made of words and punctuation, a Python statement is made of tokens.
| Token type | Meaning | Examples |
|---|---|---|
| Keyword | Reserved word with fixed meaning | if, else, for, while, True, None |
| Identifier | Name given by the programmer | bill, price, total_marks |
| Literal | Fixed value | 20, 45.5, 'Pen', True |
| Operator | Symbol that does an operation | + - * / == and |
| Punctuator | Symbols that structure code | ( ) [ ] { } , : . ' # |
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
pricetotal_bill_taxitem2Qty(different fromqty)
Invalid identifiers
1item– starts with digittotal bill– spacewhile– keywordrate%– symbolgst-rate– hyphen
Literals
A literal is a raw value written directly in a program.
| Literal | Examples |
|---|---|
| Numeric – integer | 25, -7, 0o17 (octal), 0x1F (hex), 0b101 (binary) |
| Numeric – float | 3.14, -0.5, 2.5e3 |
| Numeric – complex | 3+2j |
| String | 'Pen', "Delhi", '''multi-line''' |
| Boolean | True, False |
| Special | None |
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 literalsEscape 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:
score = 50 print(score, type(score)) score = 'fifty' # same name, now a string (dynamic typing) print(score, type(score))
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
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
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))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.
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.
