Python Core Foundation - Module 1
This comprehensive guide covers Python fundamentals from beginner to advanced level, forming the
foundation for Machine Learning and Data Science. Each section provides core concepts, examples,
and project guidance.
0. Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It
supports multiple paradigms including procedural, object-oriented, and functional programming.
Why Python?
1. Easy to learn and read due to clear syntax.
2. Vast ecosystem of libraries (NumPy, Pandas, Scikit-learn).
3. Platform independent – works on Windows, macOS, and Linux.
4. Used in diverse fields such as AI, Web Development, Automation, and Data Science.
Python Installation & IDE
Install Python from [Link]. Popular IDEs include PyCharm, VS Code, and Jupyter Notebook for
data science tasks.
Install Jupyter using pip:
pip install notebook ipykernel
Difference Between Python and Other Languages
Unlike languages like Java or C++, Python does not require variable declaration, semicolons, or explicit
type definitions. Its indentation defines code blocks instead of braces `{}`.
# Example
if True:
print("Python uses indentation") # Indentation defines scope
Semicolons, Indentation, and Comments
Python statements don't require semicolons, though they can be used to separate multiple statements
on one line.
x = 5; y = 10; print(x + y)
Use `#` for single-line comments and triple quotes for multi-line comments.
# This is a single-line comment
'''
This is a multi-line comment
'''
1. Python Basics Refresh
Variables and Rules
Variables store data. Naming rules: must start with a letter or underscore, case-sensitive, no spaces.
name = 'Alice'
age = 25
_is_valid = True
Input and Output
name = input("Enter your name: ")
print("Hello", name)
Data Types
Common types: int, float, str, bool, list, tuple, dict, set. Immutable types: int, str, tuple. Mutable: list, dict,
set.
Slicing
text = "Python"
print(text[0:3]) # Pyt
print(text[::-1]) # Reverse
Truthiness
In Python, values like 0, '', [], {}, None evaluate to False. All others are True.
F-Strings
name = 'Alice'
age = 25
print(f'My name is {name} and I am {age} years old.')
Lists, Dictionaries, and Sets
my_list = [1, 2, 3]
my_dict = {'a': 1, 'b': 2}
my_set = {1, 2, 3}
Nested Data Structures
data = {'users': [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]}
2. Control Flow & Comprehensions
If, Elif, Else
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
For Loop
for i in range(5):
print(i)
Comprehensions
Comprehensions provide a concise way to create sequences or mappings.
# List comprehension
squares = [x**2 for x in range(5)]
# Dict comprehension
square_dict = {x: x**2 for x in range(5)}
# Set comprehension
unique = {x for x in [1, 2, 2, 3]}
Nested Comprehensions
matrix = [[j for j in range(3)] for i in range(3)]
Each of these concepts is reinforced with mini-projects and problem-solving exercises to solidify
understanding.