Chapter 1: Introduction to Basic Python Programming - Variables and Datatypes

Data & AI Transformation Lead • 15+ years of experience in AI-driven Quality Engineering, Data Analytics, and Test Automation. • Expertise in AI/ML, Data Testing, Cloud Automation, ETL Pipelines, and Observability tools. • Hands-on leader in AI-driven automation, predictive analytics, and cloud data validation. • Strong technical proficiency in Snowflake, DBT, Power BI, Python, AWS, SQL, and MLOps pipelines. • Proven success in reducing defects, optimizing performance, and streamlining data workflows. • Expertise in integrating security into CI/CD pipelines, automated security scanning, vulnerability assessment, and compliance-driven quality engineering. • Expertise in JMeter, BlazeMeter, Snowflake SQL Profiler, optimizing application & data performance testing for high-scale environments. • Strong expertise in SAFe, Scrum, and Kanban methodologies, ensuring seamless collaboration between development, testing, and operations teams.
Excercise 1 :
Create a variable
monthly_savings, equal to10andnum_months, equal to4.Multiply
monthly_savingsbynum_monthsand assign it tonew_savings.Print the value of
new_savings.
In Python, semicolons at the end of a statement are optional and usually omitted unless multiple statements are written on a single line.
Excercise 2:
| Category | Data Type | Layman Explanation | Example | Max Value | Min Value | ||||||
| Numeric | int | Whole numbers without decimal points | x = 10 | No fixed limit (limited by system memory) | No fixed limit (negative) | ||||||
| Numeric | float | Numbers with decimal points | y = 3.14 | 1.8e308 | -1.8e308 | ||||||
| Numeric | complex | Numbers with real and imaginary parts | z = 2 + 3j | Depends on float part | Depends on float part | ||||||
| Boolean | bool | Either True or False | flag = True | 1 (True) | 0 (False) | ||||||
| Text | str | Sequence of characters or text | name = "Alice" | No fixed limit | 0 (empty string) | ||||||
| Sequence | list | Ordered collection you can change | numbers = [1, 2, 3] | No fixed limit | 0 elements | ||||||
| Sequence | tuple | Ordered collection you cannot change | coords = (5, 10) | No fixed limit | 0 elements | ||||||
| Sequence | range | Represents a range of numbers | r = range(5) | Depends on range bounds | 0-length range | ||||||
| Set | set | Unordered collection of unique values | s = {1, 2, 3} | No fixed limit | 0 elements | ||||||
| Set | frozenset | Immutable version of set | fs = frozenset([1, 2, 3]) | No fixed limit | 0 elements | ||||||
| Mapping | dict | Key-value pairs like a dictionary | person = {"name": "Bob"} | No fixed limit | 0 pairs | ||||||
| Binary | bytes | Immutable sequence of bytes (e.g., images, files) | b = b"hello" | Depends on system memory | 0 bytes | ||||||
| Binary | bytearray | Mutable sequence of bytes | ba = bytearray([65, 66]) | Depends on system memory | 0 bytes | ||||||
| Binary | memoryview | View over binary data for efficiency | mv = memoryview(b"hello") | Depends on buffer | 0-length view | ||||||
| None | NoneType | Represents no value or empty state | x = None | Only one value: None | Only one value: None | ||||||
| Custom | class | User-defined type (custom object) | class Car: pass | Depends on class | Depends on class |
🔹 1. Dynamic Typing
Python is dynamically typed, meaning you don’t need to declare data types.
x = 5 # int x = "Hi" # now it's a str!
🔹 2. Mutability
| Data Type | Mutable? |
list | ✅ Yes |
dict | ✅ Yes |
set | ✅ Yes |
str | ❌ No |
tuple | ❌ No |
frozenset | ❌ No |
bytes | ❌ No |
bytearray | ✅ Yes |
🧠 Why it matters: Mutable types can be changed after creation, affecting performance and behavior when passed into functions.
🔹 3. Type Conversion
You can convert between types easily:
int("5") # 5 str(100) # "100" list("hello") # ['h', 'e', 'l', 'l', 'o']
🔹 4. Memory Efficiency
tupleis more memory-efficient thanlist.Use
frozensetwhen you don’t want the set to change.range()is memory-efficient for large loops as it generates numbers on demand (lazy evaluation).
🔹 5. NoneType Usage
Noneis used for default values in functions or to represent “no value”.def greet(name=None): if name: print("Hi", name) else: print("Hi there!")
🔹 6. Identity vs Equality
Use
==to compare values,isto check identity (same memory location).a = [1, 2] b = a a == b # True a is b # True
🔹 7. Custom Types
You can define your own data types using
class:class Car: def __init__(self, brand): self.brand = brand
