CSE1021 – Problem Solving and Programming
UNIT II – PYTHON (Detailed Notes with Examples)
1. Introduction to Python
Python is an interpreted, interactive, high-level, object-oriented programming language. Developed
by Guido van Rossum in 1991 at CWI, Netherlands. Named after ‘Monty Python’s Flying Circus’. It
is known for its simplicity and readability.
Example:
print('Hello, World!')
Output: Hello, World!
2. Features of Python
• Easy to learn and use • Portable and open-source • Interpreted and interactive • Object-oriented
and extensible • Scalable and suitable for beginners
Example:
a, b = 5, 10
print(a + b) # Output: 15
3. Data Types in Python
Python supports various data types such as int, float, string, list, tuple, and dictionary.
Example:
x = 10 # int
y = 3.14 # float
name = "Ayush" # string
print(type(x), type(y), type(name))
Output: <class 'int'> <class 'float'> <class 'str'>
4. Strings
Strings are immutable sequences of characters enclosed in quotes.
Example:
text = "Python Programming"
print(text[0]) # Output: P
print(text[0:6]) # Output: Python
print([Link]()) # Output: PYTHON PROGRAMMING
5. Lists
Lists are ordered, mutable sequences enclosed in square brackets [].
Example:
fruits = ["apple", "banana", "cherry"]
[Link]("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
print(fruits[1]) # Output: banana
6. Tuples
Tuples are ordered, immutable sequences enclosed in parentheses ().
Example:
tup = (1, 2, 3, 4)
print(tup[2]) # Output: 3
# tup[1] = 5 → Error (Tuples are immutable)
7. Dictionaries
Dictionaries are unordered, mutable collections of key-value pairs enclosed in {}.
Example:
student = {'name': 'Ayush', 'age': 20, 'branch': 'CSE'}
print(student['name']) # Output: Ayush
student['age'] = 21
print(student) # Output: {'name': 'Ayush', 'age': 21, 'branch': 'CSE'}
8. Variables and Identifiers
Variables are named locations used to store data values. Python does not require declaration.
Example:
x = 10
y = 20
name = "Python"
print(x + y, name)
Output: 30 Python
9. Operators
Python supports various operators for performing operations on variables and values.
Example:
a, b = 10, 5
print(a + b) # Arithmetic → 15
print(a > b) # Relational → True
print(a and b) # Logical → 5 (True)
print(a is b) # Identity → False
10. Functions
Functions are reusable blocks of code that perform specific tasks.
Example:
def greet(name):
print("Hello,", name)
greet("Ayush")
Output: Hello, Ayush
11. Modules
A module is a file containing Python definitions and functions. Modules can be imported using
'import'.
Example:
import math
print([Link](25)) # Output: 5.0
print([Link]) # Output: 3.141592653589793