Unit I | Python Basics | 2024
Unit I
Introduction to Python Programming
Python — high-level, interpreted language by Guido van Rossum (1991). Supports procedural, OOP & functional paradigms.
Zen of Python: "Readability counts." → run
import this in shell ✨
1. Key Features
- Dynamic Typing — no need to declare variable types
- Interpreted — runs line by line via Python Virtual Machine (PVM)
- Portable — runs on Windows, macOS, Linux
- Interoperable — integrates with C/C++, Java, .NET
- Python 3.x is current (recommended over 2.x)
2. Data Types
| Type | Keyword | Example | Mutable? |
|---|---|---|---|
| Integer | int | x = 42 | No |
| Float | float | pi = 3.14 | No |
| String | str | 'hello' | No |
| Boolean | bool | True / False | No |
| List | list | [1, 2, 3] | Yes |
| Tuple | tuple | (10, 20) | No |
| Dictionary | dict | {'a': 1} | Yes |
| Set | set | {1, 2, 3} | Yes |
| NoneType | None | x = None | No |
🐍 data_types.py
# Integer
x = 5
print(type(x)) # <class 'int'>
# Float
y = 3.14
print(type(y)) # <class 'float'>
# String (immutable!)
s = 'hello'
print(s.upper()) # HELLO
# Boolean
flag = True
print(type(flag)) # <class 'bool'>
📝 String Notes
Strings are immutable — any "modification" creates a new string object.Supports raw strings
r'raw\n' and multi-line '''text'''
3. String Operations
Concatenation (+)
concat
s1 = "Hello"
s2 = "World"
print(s1 + s2)
# HelloWorld
Replication (*)
replicate
s = "Ha"
print(s * 3)
# HaHaHa
Useful String Methods
len(s)— length of strings.lower()/s.upper()— case conversions.strip()— remove leading/trailing whitespaces.split(',')— split into list','.join(lst)— join list into strings.find('x')— returns index, -1 if not founds.replace('a','b')— replace occurrencess[1:4]— slicing
4. Operators
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / // % ** | 10 // 3 = 3 |
| Comparison | == != < > <= >= | 5 > 3 → True |
| Logical | and or not | True and False → False |
| Membership | in, not in | 3 in [1,2,3] → True |
| Identity | is, is not | x is y → False |
| Bitwise | & | ^ ~ << >> | 5 & 3 = 1 |
| Assignment | = += -= *= /= %= **= | x += 5 |
Mixing Boolean + Comparison:
(num % 2 == 0) and (num > 10) — checks both conditions simultaneously!
5. Flow Control
if-elif-else
conditionals.py
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x equals 5")
else:
print("x is less than 5")
Loops
while loop
i = 0
while i < 5:
print(i)
i += 1
for loop
for i in range(5):
print(i)
# 0 1 2 3 4
break— exit loop immediatelycontinue— skip current iterationpass— do nothing (placeholder)
6. Modules & sys.exit()
importing.py
import math
print(math.sqrt(25)) # 5.0
from math import sqrt
print(sqrt(25)) # 5.0
import math as m
print(m.sqrt(25)) # 5.0
# Early exit
import sys
sys.exit(1) # exit with error code 1
⚠️ Standard vs Third-Party
Standard Library — pre-installed (math, os, sys, datetime…)Third-Party — install via
pip install package_name (e.g. requests, numpy)
7. Docstrings & Comments
docstring.py
def add(x, y):
'''Add two numbers and return result.'''
return x + y
print(add.__doc__)
# Add two numbers and return result.
# Single-line comment
'''
Multi-line comment
using triple quotes
'''