B.
Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
History of Python
Created by Guido van Rossum in the late 1980s and released in 1991.
Inspired by the ABC language and designed for code readability and simplicity.
Name comes from the TV show “Monty Python’s Flying Circus”, not the snake.
Open-source and community-driven.
Major versions:
o Python 2.x (legacy)
o Python 3.x (current and future)
Features of Python
Simple and Easy to Learn
Open Source and freely available
High-level Language
Interpreted Language
Platform Independent
Extensive Libraries (e.g., NumPy, pandas, TensorFlow)
Supports Multiple Programming Paradigms:
o Object-Oriented
o Procedural
o Functional
Automatic Memory Management
Large Community Support
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Basic Syntax in Python
Python has a simple, clean, and readable syntax that emphasizes readability and reduced complexity.
Below are the key elements of Python syntax:
1. Case Sensitivity
Python is case-sensitive, which means that identifiers like Variable, variable, and VARIABLE
are all treated differently.
Example:
name = "Alice"
Name = "Bob"
print(name) # Output: Alice
print(Name) # Output: Bob
Note: Avoid using variable names that differ only by case to prevent confusion.
2. Indentation
Indentation is mandatory in Python to define blocks of code.
No use of {} like in C, C++, or Java.
Recommended indentation: 4 spaces (PEP 8 standard)
All statements in a block must be indented at the same level.
Example:
x = 10
if x > 5:
print("Greater than 5") # Indented block
print("Inside if block")
print("Outside if block") # Not part of if block
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Incorrect Example:
x = 10
if x > 5:
print("Error") # ❌ This will raise an IndentationError
Note: Mixing tabs and spaces can lead to errors. Stick to either all spaces or all tabs (spaces are
preferred).
3. No Curly Braces ({})
Unlike many other languages, Python does not use {} to indicate blocks of code.
Python uses indentation instead to define the start and end of blocks like:
o if, else, elif
o for, while
o def (functions), class
4. Comments in Python
Comments are used to make the code more understandable and are ignored by the interpreter.
Single-line Comment - Starts with a # symbol.
# This is a single-line comment
x = 5 # Assigning value to x
Multi-line Comments - Python does not have a true multi-line comment, but you can use
triple-quoted strings (either ''' or """) that are not assigned to any variable as a workaround.
'''
This is a
multi-line comment
using single quotes
'''
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
"""
This is also a
multi-line comment
using double quotes
"""
Note:
Technically, triple quotes are multi-line strings, but when not used as a docstring or assigned
to a variable, they act as comments.
Use them sparingly for commenting out blocks of code or providing long explanations.
Additional Notes
Line Continuation: Use \ to continue a statement across lines.
total = 1 + 2 + 3 + \
4+5
Python Statements can be:
o Simple: one statement per line.
o Compound: combining multiple statements with ;
a = 5; b = 10; print(a + b)
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Variables and Data Types
1. Variables
Variables are used to store data in memory. In Python:
You don’t need to declare a variable type.
The type is inferred automatically based on the assigned value.
Python is dynamically typed.
Rules for Naming Variables:
Must begin with a letter (a-z, A-Z) or an underscore (_).
Can contain letters, numbers, and underscores (_).
Cannot start with a number.
Case-sensitive (Age, age, and AGE are different).
Avoid using Python keywords as variable names (like for, if, class, etc.).
Example:
x = 10
name = "Alice"
pi = 3.14
is_active = True
2. Data Types in Python
Python has various built-in data types, categorized as follows:
Numeric Types
int: Integer (e.g., 5, -10, 0)
float: Floating-point number (e.g., 3.14, -0.99)
complex: Complex number (e.g., 3 + 4j)
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Example
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
Text Type
str: String (e.g., "hello", 'Python')
Example
name = "Alice"
Boolean Type
bool: Can be either True or False
Example
flag = True
is_valid = False
Sequence Types
list: Ordered, mutable collection
Example
fruits = ["apple", "banana", "cherry"]
tuple: Ordered, immutable collection
Example
coordinates = (10, 20)
range: Represents a sequence of numbers
Example
r = range(5) # 0 to 4
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Mapping Type
dict: Key-value pairs
Example
student = {"name": "Alice", "age": 20}
Set Types
set: Unordered collection of unique items
Example
s = {1, 2, 3}
frozenset: Immutable version of set
Example
fs = frozenset([1, 2, 3])
Binary Types
bytes: Immutable byte sequences
Example
b = b"hello"
bytearray: Mutable version of bytes
Example
ba = bytearray(b"hello")
memoryview: Used to access internal data of objects that support buffer protocol.
3. Type Conversion
Python allows explicit type conversion, also called type casting.
Example:
a = int("5") # String to integer
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
b = float(3) # Integer to float
c = str(25) # Integer to string
Common Functions:
int()
float()
str()
list(), tuple(), set(), dict() for sequence types
Operators
1. Arithmetic Operators: Used for basic mathematical calculations.
Example
a = 10
b=3
print(a + b) # Addition: 13
print(a - b) # Subtraction: 7
print(a * b) # Multiplication: 30
print(a / b) # Division: 3.333...
print(a // b) # Floor Division: 3
print(a % b) # Modulus: 1
print(a ** b) # Exponentiation: 1000
2. Comparison Operators: Used to compare values. Return True or False.
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Example
x=5
y = 10
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
3. Assignment Operators: Used to assign values and perform compound operations.
Example
x = 10
x += 5 # Same as x = x + 5
print(x) # 15
x -= 3 # x = x - 3
x *= 2 # x = x * 2
x /= 4 # x = x / 4
print(x)
4. Logical Operators: Used to combine boolean expressions.
Example
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
5. Bitwise Operators: Used to perform binary operations.
Example
a=5 # 0101 in binary
b=3 # 0011 in binary
print(a & b) # 1 (0001)
print(a | b) # 7 (0111)
print(a ^ b) # 6 (0110)
print(~a) # -6 (inverts bits)
print(a << 1) # 10 (1010)
print(a >> 1) # 2 (0010)
6. Membership Operators: Check for presence in sequences like lists, strings, etc.
Example
my_list = [1, 2, 3]
print(2 in my_list) # True
print(4 not in my_list) # True
7. Identity Operators: To check whether two variables refer to the same object in memory.
Example
x = [1, 2]
y=x
z = [1, 2]
print(x is y) # True (same object)
print(x is z) # False (same value, different object)
print(x is not z) # True
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University
B.Tech and 3rd Semester
Python Programming
(BES00007)
2025-26
Precedence
Operators Description
Level
1 (Highest) () Parentheses (grouping)
2 ** Exponentiation
3 +x, -x, ~x Unary plus, minus, bitwise NOT
Multiplication, Division, Floor Division,
4 *, /, //, %
Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise Shift Left, Shift Right
7 & Bitwise AND
8 ^ Bitwise XOR
9 | Bitwise OR
==, !=, >, <, >=, <=, is, is not, in, not
10 Comparison, Identity, Membership
in
11 not Logical NOT
12 and Logical AND
13 or Logical OR
14 (Lowest) =, +=, -=, *=, /=, //=, %=, **=, &= Assignment
Ayantika Das
Assistant Professor
Dept of CSE
Brainware University