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, Marks and MARKS are 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
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (always float)10 / 33.3333...
//Floor Division (integer quotient)10 // 33
%Modulo (remainder)10 % 31
**Exponentiation (power)2 ** 8256

Comparison (Relational) Operators

Return True or False.

Operator Meaning Example Result
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than7 > 3True
<Less than2 < 5True
>=Greater than or equal10 >= 10True
<=Less than or equal3 <= 5True

Logical Operators

Combine multiple conditions — also return True or False.

Operator Rule Example Result
andTrue only if BOTH conditions are TrueTrue and FalseFalse
orTrue if AT LEAST ONE condition is TrueTrue or FalseTrue
notReverses the boolean valuenot TrueFalse

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()Integerint('42'), int(3.9)42, 3 (truncates)
float()Floatfloat('3.14'), float(5)3.14, 5.0
str()Stringstr(100), str(3.14)'100', '3.14'
bool()Booleanbool(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.