Try the Python Shell
The Python interpreter (Python Shell) shows the >>> prompt when it is ready for a command — this is Interactive Mode. Type an expression below and see it evaluate immediately, the same way it would in IDLE.
Python Interactive Shell
● liveIntroduction & Program Execution
Python is free, open-source, and platform-independent — the interpreter executes code line by line. It can be downloaded from python.org and written using an interpreter, an IDE, or an online platform. There are three ways to write and run a Python program.
Interactive Modei
REPL (Read–Eval–Print–Loop). Type a statement at the >>> prompt and it executes immediately. Good for testing single-line statements — but nothing is saved.
Scripting Modeii
Write multiple statements and save the file with a .py extension using an editor such as IDLE, and run it with Run → Run Module (F5).
Online Platform Modeiii
Write and run Python in a web browser with no local installation — e.g. Google Colab, Replit, Kaggle Notebooks, Deepnote, Binder.
Advantages of Online Platforms
No installation required · runs in the browser · instant output · works on any operating system.
Limitations of Online Platforms
Needs an internet connection · limited file handling · not suitable for large projects.
Keywords & Identifiers
A keyword is a reserved word with a special meaning in Python — all keywords except None, True and False are lowercase. An identifier is a name used to define a variable, function, class, or module.
Table 4.1 — Python Keywords
Rules for Naming Identifiers
Rules
Only letters, digits, or underscore allowed · must not start with a digit · Python keywords cannot be used · no whitespace or special characters · case-sensitive (Grade, grade, GRADE are all different).
Valid vs Invalid
Valid: totalamount, score1, value, basic_pay
Invalid: user@name (special character), return (keyword), total value (whitespace), 2A (starts with digit)
Variables & Data Types
Variables are symbolic names used to store data that may change during execution — assigned using =. Python is dynamically typed, so a variable's data type changes automatically when reassigned. A data type specifies what kind of value a variable can hold.
Classification of Python Data Types
Numericint / float / complex
Integer (whole numbers, e.g. 27, -36), Float (fractional, e.g. 3.5, -12.75), Complex (a + bj — real part a, imaginary part b).
Booleanbool
One of two logical values: True or False.
Sequencestr / list / tuple
Ordered values accessed by index (starting at 0).
String — immutable characters.
List — mutable, in [ ].
Tuple — immutable, in ( ).
NoneNoneType
Represents the absence of a value — nothing, null, or empty result.
Setset
An unordered collection of unique elements in { } — mutable, duplicates are eliminated, not accessed by index.
Mappingdict
Dictionary — stores key–value pairs in { }, values accessed via their key. Mutable; keys can be added or removed.
Mutable vs Immutable
Mutable objects
Value can be changed after creation — List, Set, Dictionary.
Immutable objects
Value cannot be modified after creation — Numeric, String, Boolean, Tuple.
Operators
Operators are special symbols that carry out operations on values or variables: Arithmetic, Assignment, Relational, Logical, Identity, and Membership operators.
4.7.1 Arithmetic Operators
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition / Concatenation | Adds two numbers, or joins two strings | 5 + 2 = 7 · 'New'+'Delhi' = 'NewDelhi' |
| - | Subtraction | Subtracts two numbers | 5 - 2 = 3 |
| * | Multiplication | Multiplies two numbers, or repeats a string by an integer | 5 * 2 = 10 · 'Hi'*3 = 'HiHiHi' |
| / | Division | Divides two numbers | 5 / 2 = 2.5 |
| // | Floor Division | Returns floored (rounded down) integer quotient | 5//2 = 2 · -5//2 = -3 |
| % | Modulus | Returns remainder after division | 5 % 2 = 1 |
| ** | Exponentiation | Raises base to the power of exponent | 5**2 = 25 |
4.7.2 Assignment Operators
| Operator | Name | Same as | Example |
|---|---|---|---|
| = | Assignment | — | a = 10 |
| += | Addition Assignment | a = a + 5 | a=10 → a+=5 → 15 |
| -= | Subtraction Assignment | a = a - 5 | a=10 → a-=5 → 5 |
| *= | Multiplication Assignment | a = a * 5 | a=10 → a*=5 → 50 |
| /= | Division Assignment | a = a / 5 | a=10 → a/=5 → 2.0 |
| //= | Floor Division Assignment | a = a // 5 | a=10 → a//=5 → 2 |
| %= | Modulus Assignment | a = a % 5 | a=10 → a%=5 → 0 |
| **= | Exponent Assignment | a = a ** 5 | a=10 → a**=5 → 100000 |
4.7.3 Relational Operators
| Operator | Name | Example (a=8, b=5) |
|---|---|---|
| == | Equal | a==b → False |
| != | Not equal | a!=b → True |
| < | Less than | a<b → False |
| > | Greater than | a>b → True |
| <= | Less than or equal | a<=b → False |
| >= | Greater than or equal | a>=b → True |
4.7.4 Logical Operators
| Operator | Name | Description | Example (a=20, b=10) |
|---|---|---|---|
| and | Logical AND | True only if both expressions are True | a>b and b<a → True |
| or | Logical OR | True if at least one expression is True | a<b or b>a → False |
| not | Logical NOT | Reverses the result | not(a<b) → True |
4.7.5 Identity Operators (used to check whether two variables refer to the same object in memory)
| Operator | Description | Example |
|---|---|---|
| is | True if two variables refer to the same object in memory | x=[1,2,3]; y=[1,2,3]; x is y → False (different objects) |
| is not | True if two variables do NOT refer to the same object | z=x; x is not z → False (same object) |
4.7.6 Membership Operators (used to check whether a value or variable is found in a sequence)
| Operator | Description | Example |
|---|---|---|
| in | True if a value is found in a sequence | a=[10,20,30]; 10 in a → True |
| not in | True if a value is NOT found in a sequence | text='Python'; 'P' not in text → False |
Precedence of Operators
An expression is a combination of values, variables, and operators. When an expression has more than one operator, precedence decides which operation happens first. Precedence level 1 is the highest priority.
| Level | Operator(s) | Description |
|---|---|---|
| 1 | ( ) | Parentheses — group expressions, override normal precedence |
| 2 | ** | Exponentiation |
| 3 | +, - | Unary plus, unary minus |
| 4 | *, /, //, % | Multiplication, division, floor division, modulus |
| 5 | +, - | Addition, Subtraction |
| 6 | ==, !=, >, <, >=, <=, is, is not, in, not in | Relational, identity, and membership operators |
| 7 | not | Logical NOT |
| 8 | and | Logical AND |
| 9 | or | Logical OR |
Input and Output Statements
Python uses the built-in functions input() and print() to take data from the keyboard and display output on screen.
Input Statement
variable = input("Prompt message") — always returns a string value.
Output Statement
print("Display message") — can print strings, numbers, or the values of variables.
Quick Recap
Tap a question to reveal the answer.
Who developed the Python programming language, and when?
Guido van Rossum, in 1991.
What does the symbol >>> represent?
The Python prompt — it indicates the interpreter is ready to accept a command (Interactive Mode).
What symbol begins a single-line comment, and what extension is used to save a Python program?
Single-line comments begin with #. Python programs are saved with the .py extension.
What is the // operator used for?
Floor division — it returns the floored (rounded-down) integer quotient of a division, e.g. -5//2 = -3.
Which data type stores key–value pairs, and which collection type is unordered?
Dictionary (dict) stores key–value pairs. Set is the unordered collection among List, Tuple, Set, and String.
Which operator is used to check object identity?
is (and its negation is not) — they check whether two variables refer to the same object in memory, not just equal values.
Identify the error: 1stNumber=50
Invalid identifier — it starts with a digit. Identifiers must begin with a letter or underscore.
What is the output of: a=5, b=2, print(a % b)?
1 — the modulus operator returns the remainder of 5 divided by 2.
Python Comments
Comments explain notes within the code for programmers to read and understand. The Python interpreter completely ignores comments during execution — they exist purely for internal documentation.
Single-line Comment
Begins with #. Anything written after # on that line is ignored.
Multi-line Comment
Python has no dedicated symbol — a string in triple quotes (''' or """) acts as a comment if it is not assigned to a variable or used in code.