Python Programming – Unit 1 Full Notes with Examples
Introduction to Python
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in
1991. Features: - Simple and easy to learn - Interpreted (line by line execution) - Object-Oriented - Portable (runs
on multiple OS) - Huge library support
Installing Python
Download from python.org, install Anaconda for Data Science. Verify installation using: python --version. IDEs:
IDLE, VS Code, PyCharm, Jupyter Notebook.
Basic Syntax
Python code uses indentation instead of braces. - Indentation = 4 spaces - Statements do not require ';' -
Comments: # for single line, ''' ''' for multi-line
Examples:
# Example 1
print("Hello, World!")
# Example 2
x = 5
y = 10
print("Sum =", x+y)
Interactive Shell
Open terminal, type python, run line by line. Useful for testing small code snippets.
Examples:
>>> 2+3
5
>>> print("Python")
Python
Writing & Running Script
Save as .py file, run using: python filename.py Example (first.py): print("Welcome to Python")
Examples:
# first.py
print("Welcome to Python Programming")
# Run in terminal:
# python first.py
Data Types
int, float, str, bool, complex Example: a=10, b=3.14, c="Python", d=True, e=3+4j
Examples:
a = 10
b = 3.14
c = "Python"
d = True
e = 3+4j
print(type(a)) # int
print(type(b)) # float
print(type(c)) # str
print(type(d)) # bool
print(type(e)) # complex
Variables & Assignments
Variables store values in memory. No need to declare type. Rules: Start with letter/underscore, case-sensitive.
Example: x=10, name="Vineet", pi=3.14 Multiple assignment: a,b,c = 1,2,3
Examples:
x = 100
name = "Vineet"
pi = 3.14159
# Multiple assignment
a, b, c = 1, 2, 3
print(a, b, c)
# Swapping values
a, b = b, a
print("After swap:", a, b)
Operators
Operators allow performing arithmetic, logical, relational operations.
Examples:
x, y = 7, 3
# Arithmetic
print(x+y) # 10
print(x-y) # 4
print(x*y) # 21
print(x/y) # 2.333...
print(x//y) # 2
print(x%y) # 1
print(x**y) # 343
# Relational
print(x > y) # True
print(x == y) # False
# Logical
print(x > 5 and y < 5) # True
# Membership
print('P' in "Python") # True
print('z' not in "Python") # True
Expressions
Expressions combine values, variables, operators.
Examples:
result = (10 + 5) * 2
print(result) # 30
exp = 2**3 + 4/2 - 1
print(exp) # 9.0
Selection Statements
Python supports if, if-else, if-elif-else.
Examples:
num = -5
if num > 0:
print("Positive")
else:
print("Negative")
marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
Loops
Python supports for and while loops.
Examples:
# For loop example
for i in range(1, 6):
print("Square of", i, "is", i*i)
# While loop example
n = 5
while n > 0:
print("Countdown:", n)
n -= 1
Control Statements
Control statements modify loop flow: break, continue, pass.
Examples:
for i in range(5):
if i == 2:
continue
if i == 4:
break
print(i)
# pass example
def func():
pass
String Manipulation
Python supports string indexing, slicing, and built-in functions.
Examples:
s = "hello world"
print(s.upper()) # HELLO WORLD
print(s.title()) # Hello World
print(s[0]) # h
print(s[-1]) # d
print(s[0:5]) # hello
print(s.replace("world", "Python")) # hello Python
File Handling
Python supports file operations like reading, writing, appending.
Examples:
# Write file
f = open("test.txt", "w")
f.write("Hello Vineet\nWelcome to Python")
f.close()
# Read file
f = open("test.txt", "r")
print(f.read())
f.close()
# Read line by line
with open("test.txt", "r") as f:
for line in f:
print(line)