Python Programming
From fundamentals to practical programming
Original educational study guide
Python Programming | Page 1
1. Python Foundations
Python syntax
Python uses indentation to define blocks. Statements are generally written one per line, while expressions can
be combined freely.
Variables refer to objects. Common built-in types include int, float, bool, str, list, tuple, set and dict.
Input and output
print() displays information. input() reads text from the user and returns a string, so numerical input often needs
conversion with int() or float().
Operators
Arithmetic operators include +, -, *, /, //, %, and **. Comparison operators produce Boolean values. Logical
operators and, or and not combine conditions.
Python Programming | Page 2
2. Control Flow
if statements
Use if, elif and else to choose between alternatives. Conditions can be combined with Boolean operators.
A good condition is explicit and easy to test.
Loops
for loops iterate through an iterable such as a list, string or range. while loops continue while a condition
remains true.
break exits a loop; continue skips to the next iteration.
Nested control flow
Nested loops are useful for grids and pairwise comparisons but can become expensive. Always consider the
number of iterations.
Python Programming | Page 3
3. Functions
Defining functions
Functions are created with def. Parameters provide input and return sends a result back to the caller.
A function should ideally have one clear responsibility and a descriptive name.
Scope
Variables created inside a function are local unless explicitly handled otherwise. Avoid unnecessary global
state because it makes programs harder to reason about.
Default and keyword arguments
Default values make arguments optional. Keyword arguments make calls easier to read and reduce mistakes
when several parameters have similar meanings.
Python Programming | Page 4
4. Core Data Structures
Lists
Lists are ordered and mutable. Common methods include append, extend, insert, remove, pop, sort and
reverse. Slicing creates a view-like sequence result rather than changing the original list.
Tuples and sets
Tuples are ordered but immutable. Sets store unique elements and support union, intersection and difference
operations.
Dictionaries
Dictionaries map keys to values. They are useful for lookups, counting and representing structured records.
Python Programming | Page 5
5. Strings
String operations
Strings are immutable sequences. Useful operations include lower, upper, strip, split, join, replace and find.
f-strings provide readable formatting such as f'{name}: {score}'.
Indexing and slicing
Indexing starts at zero. Negative indices count from the end. Slices use start:stop:step and exclude the stop
index.
Text processing
For robust text processing, normalize case and whitespace before comparisons when the task permits it.
Python Programming | Page 6
6. Files and Exceptions
File handling
Use open() with a context manager: with open(path) as f:. This closes the file automatically.
Modes include r for reading, w for writing and a for appending.
Exceptions
try/except handles expected runtime problems. finally can perform cleanup. Do not hide every error with a
broad except; handle specific exceptions where possible.
Debugging
Read tracebacks from the bottom upward. Check the line reported, inspect values, and reproduce the smallest
failing example.
Python Programming | Page 7
7. Object-Oriented Basics
Classes and objects
A class defines attributes and methods shared by objects. __init__ initializes instance state. self refers to the
current instance.
Inheritance
Inheritance allows a class to extend another class, but composition is often simpler when the relationship is not
a true 'is-a' relationship.
Special methods
Methods such as __str__, __repr__ and __len__ integrate user-defined classes with Python's built-in behavior.
Python Programming | Page 8
8. NumPy Introduction
Why NumPy
NumPy provides efficient multidimensional arrays and vectorized numerical operations. It is a core tool in
scientific Python and data science.
Shapes and axes
A 2D array has rows and columns. shape describes dimensions; axis arguments determine which dimension an
operation reduces.
Broadcasting
Broadcasting allows arrays with compatible shapes to participate in element-wise operations without manually
copying values.
Python Programming | Page 9
9. Good Programming Practice
Readable code
Use descriptive names, small functions and comments for non-obvious reasoning. Avoid comments that merely
repeat the code.
Testing
Test normal cases, boundary cases and invalid inputs. Assertions are useful for checking assumptions during
development.
Efficiency
Choose appropriate data structures and avoid unnecessary repeated work. Big-O analysis helps estimate how
runtime grows with input size.
Python Programming | Page 10
Practice Questions
1. What does input() return by default?
2. Difference between // and /?
3. What does break do?
4. What does continue do?
5. Why use functions?
6. Difference between list and tuple?
7. What is a dictionary used for?
8. What does slicing stop:stop mean?
9. Why use with open(...) as f?
10. What is an exception?
11. What is a NumPy array?
12. What is broadcasting?
Answer Key / Self-check
1. A string.
2. // performs floor division; / performs true division.
3. Exits the current loop.
4. Skips to the next iteration.
5. Reuse logic, improve structure and testability.
6. Lists are mutable; tuples are immutable.
7. Fast key-to-value lookup and structured mappings.
8. The stop index is excluded.
9. It ensures the file is closed automatically.
10. A runtime event that interrupts normal execution.
11. An efficient multidimensional numerical array.
12. Compatible arrays can interact element-wise without explicit replication.
Final Revision Checklist
• Review the definitions before memorizing formulas.
• Work through the examples without looking at the solution first.
• Write down assumptions whenever a formula depends on them.
• Check units, dimensions, shapes and boundary cases in numerical work.
• Practice explaining each concept in your own words.
Python Programming | Page 11