1. Python — Introduction and Features
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. It emphasises code readability and simplicity.
Key Features of Python
| Feature | Meaning |
|---|---|
| Interpreted | Executed line by line — no separate compilation step needed |
| Dynamically typed | No need to declare variable type — Python infers it automatically |
| High-level | Close to human language — readable and easy to write |
| Open source & free | Free to use, modify and distribute |
| Platform independent | Runs on Windows, Linux, macOS — write once, run anywhere |
| Large standard library | Built-in modules for maths, file I/O, data handling (NumPy, Pandas, Matplotlib) |
Python Execution Modes
| Mode | How it works | When to use |
|---|---|---|
| Interactive Mode | Type one statement at a time in Python shell; instant output after each line | Testing small expressions, quick calculations |
| Script Mode | Write complete program in a .py file; run all at once |
Writing full programs, saving work |
2. Variables and Assignment
A variable is a named memory location that stores a value. In Python, a variable is created the moment you assign a value to it — no declaration needed.
# Creating variables
name = 'Priya' # str
age = 16 # int
marks = 92.5 # float
passed = True # bool
# Multiple assignment
a = b = c = 0 # all three get value 0
x, y, z = 10, 20, 30 # simultaneous assignment
Rules for Variable Names (Identifiers)
- Must begin with a letter (a–z, A–Z) or underscore (_) — not a digit.
- Can contain letters, digits, and underscores — no spaces or special characters.
- Case-sensitive:
marks,MarksandMARKSare three different variables. - Cannot be a Python keyword (reserved word) like
if,for,while,True,None.
| Valid | Invalid | Reason Invalid |
|---|---|---|
student_name |
1name |
Starts with a digit |
_total |
my name |
Contains a space |
marks2 |
for |
Python keyword |
totalMarks |
my-var |
Contains hyphen (–) |
3. Data Types in Python
Python has several built-in data types. The four core types for IP are:
| Data Type | Keyword | Description | Examples |
|---|---|---|---|
| Integer | int |
Whole numbers (positive, negative, zero) — no decimal point | 10, -5, 0, 1000 |
| Float | float |
Numbers with a decimal point (real numbers) | 3.14, -2.5, 0.0, 1.0 |
| String | str |
Sequence of characters enclosed in quotes (single, double, or triple) | 'Priya', "hello", '123' |
| Boolean | bool |
Logical values — only two possible values | True, False |
# Checking data type using type()
print(type(10)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type('hello')) # <class 'str'>
print(type(True)) # <class 'bool'>
Important Note: Dynamic Typing
In Python, a variable's type is determined by the value assigned — and can change:
x = 10 # x is int
x = 'hello' # now x is str — same variable, different type
x = 3.14 # now x is float
4. Operators in Python
Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division (always float) | 10 / 3 | 3.3333... |
// | Floor Division (integer quotient) | 10 // 3 | 3 |
% | Modulo (remainder) | 10 % 3 | 1 |
** | Exponentiation (power) | 2 ** 8 | 256 |
Comparison (Relational) Operators
Return True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 7 > 3 | True |
< | Less than | 2 < 5 | True |
>= | Greater than or equal | 10 >= 10 | True |
<= | Less than or equal | 3 <= 5 | True |
Logical Operators
Combine multiple conditions — also return True or False.
| Operator | Rule | Example | Result |
|---|---|---|---|
and | True only if BOTH conditions are True | True and False | False |
or | True if AT LEAST ONE condition is True | True or False | True |
not | Reverses the boolean value | not True | False |
Operator Precedence (BODMAS for Python)
From highest to lowest priority:
** > * / // % > + - > == != > < >= <= > not > and > or
print(2 + 3 * 4) # 14 (not 20 — * before +)
print(2 ** 3 + 1) # 9 (** first: 8 + 1)
print(10 % 3 + 2) # 3 (% first: 1 + 2)
print((2 + 3) * 4) # 20 (parentheses override)
5. Input and Output
Output — print()
# Basic print
print('Hello, World!') # Hello, World!
print('Marks:', 95) # Marks: 95
# sep and end parameters
print(1, 2, 3, sep='-') # 1-2-3
print('Hello', end=' ')
print('World') # Hello World (on same line)
# f-string (formatted string literal)
name = 'Aryan'
marks = 95
print(f'Student: {name} Marks: {marks}') # Student: Aryan Marks: 95
Input — input()
input() always returns a string — you must convert it to the required type.
# input() always returns str
name = input('Enter your name: ') # str
age = int(input('Enter age: ')) # convert to int
gpa = float(input('Enter GPA: ')) # convert to float
# Common mistake — forgetting to convert
x = input('Enter number: ') # x = '5' (string!)
print(x + 3) # ERROR: can't add str and int
print(int(x) + 3) # 8 (correct)
6. Type Conversion
Python allows converting values from one data type to another using built-in functions:
| Function | Converts to | Example | Output |
|---|---|---|---|
int() | Integer | int('42'), int(3.9) | 42, 3 (truncates) |
float() | Float | float('3.14'), float(5) | 3.14, 5.0 |
str() | String | str(100), str(3.14) | '100', '3.14' |
bool() | Boolean | bool(0), bool(1), bool(''), bool('hi') | False, True, False, True |
Key rule for bool(): 0, 0.0, '' (empty string), None, and empty collections → False. Everything else → True.
Key rule for int(float): int(3.9) gives 3 — it truncates (removes decimal part), it does NOT round.
7. Comments and Indentation
# This is a single-line comment — ignored by Python
"""
This is a multi-line comment (docstring).
Used to document functions and programs.
"""
# Indentation is MANDATORY in Python (4 spaces or 1 tab)
if 10 > 5:
print('Yes') # indented block
print('Done') # back to main level
Unlike most languages, Python uses indentation (whitespace) to define code blocks — not curly braces {}. Inconsistent indentation causes an IndentationError.

