Python Programming Fundamentals:
Comprehensive Study Notes
Computer Science — Core syntax and concepts for beginners
Chapter 1: Getting Started with Python
Python is a high-level, interpreted programming language known for its readable syntax and broad
standard library. It is widely used in web development, data science, automation, scientific computing,
and general-purpose scripting. Unlike compiled languages, Python code is executed line by line by an
interpreter, which makes it easier to test and debug incrementally.
Python is dynamically typed, meaning you do not need to declare a variable's type explicitly — the
interpreter infers the type from the value assigned to it. It is also strongly typed, meaning it will not
silently convert incompatible types (for example, adding a string and an integer directly raises an error
rather than producing unpredictable results).
1.1 Your First Program
The traditional first program in any language simply displays a message to the screen. In Python this is
done with the built-in print() function, for example: print("Hello, World!"). Python's minimal syntax means
a complete, runnable program can consist of a single line.
Chapter 2: Variables and Data Types
2.1 Core Data Types
Every value in Python has a type, and understanding these types is fundamental to writing correct
programs.
• int: whole numbers, such as age = 25, with no fixed size limit.
• float: numbers with decimal points, such as price = 9.99.
• str: text data, enclosed in single or double quotes, such as name = "Alice".
• bool: logical values, either True or False.
• NoneType: represents the absence of a value, written as None.
2.2 Type Conversion
Python provides built-in functions to convert between types, such as int(), float(), and str(). This is
commonly needed when reading user input, since the input() function always returns a string, even if the
user types a number.
2.3 Naming Conventions
Good variable names improve code readability. Python style conventions (outlined in the PEP 8 style
guide) recommend lowercase words separated by underscores for variables and functions
(snake_case), and CapitalizedWords (PascalCase) for class names.
Chapter 3: Operators and Expressions
3.1 Arithmetic Operators
Python supports the standard arithmetic operators: addition (+), subtraction (-), multiplication (*), division
(/), floor division (//, which discards the remainder), modulo (%, which returns the remainder), and
exponentiation (**).
3.2 Comparison and Logical Operators
Comparison operators (==, !=, <, >, <=, >=) evaluate to a boolean value. Logical operators (and, or, not)
combine boolean expressions, enabling more complex conditional logic.
Chapter 4: Control Flow
4.1 Conditional Statements
Conditional statements let a program make decisions. The if statement executes a block of code only if a
condition is true; elif provides additional conditions to check; and else provides a fallback if none of the
previous conditions matched.
Python uses indentation (rather than braces or keywords) to define code blocks, which is a distinctive
feature of the language's syntax and enforces a consistent, readable style across codebases.
4.2 Loops
Loops let a program repeat an action multiple times.
• for loops: iterate over a sequence, such as a list, string, dictionary, or a range of numbers
generated by range().
• while loops: repeat a block of code as long as a specified condition remains true, useful when the
number of iterations isn't known in advance.
• break: immediately exits the nearest enclosing loop.
• continue: skips the rest of the current iteration and moves to the next one.
• else clause on loops: an often-overlooked feature where the else block runs only if the loop
completes without hitting a break statement.
Chapter 5: Data Structures
5.1 Lists
A list is an ordered, mutable collection that can hold items of different types. Lists support indexing
(accessing an item by position), slicing (extracting a sub-sequence), and a rich set of methods such as
append(), remove(), sort(), and reverse().
5.2 Tuples
A tuple is similar to a list but immutable — once created, its contents cannot be changed. Tuples are
often used for fixed collections of related values, such as coordinate pairs, and their immutability makes
them usable as dictionary keys, unlike lists.
5.3 Dictionaries
A dictionary stores data as key-value pairs, providing fast lookups by key rather than by numeric
position. Dictionaries are extremely common in Python for representing structured data, such as a record
with named fields.
5.4 Sets
A set is an unordered collection of unique elements. Sets are useful for removing duplicates from a
collection and for performing mathematical set operations such as union, intersection, and difference.
Chapter 6: Functions
Functions let you package reusable logic into a named, callable block, improving code organization and
reducing repetition. A function is defined using the def keyword, followed by a name, a parenthesized list
of parameters, and an indented block of code.
Functions can return a value using the return keyword; if no return statement is reached, the function
implicitly returns None. Parameters can have default values, allowing callers to omit them, and Python
also supports *args (for an arbitrary number of positional arguments) and **kwargs (for an arbitrary
number of keyword arguments), which give functions considerable flexibility.
Variables defined inside a function are local to that function and are not accessible outside it, a concept
known as scope. This helps prevent unintended interactions between different parts of a program.
Chapter 7: Object-Oriented Programming Basics
A class is a blueprint for creating objects that bundle together data (called attributes) and behavior
(called methods). Classes are defined with the class keyword, and objects are created by 'calling' the
class as though it were a function.
The special __init__ method runs automatically whenever a new object is created, and is typically used
to set up the object's initial attributes. Inside class methods, the first parameter is conventionally named
self and refers to the specific object instance the method is being called on.
Inheritance allows one class to extend another, inheriting its attributes and methods while adding or
overriding functionality — a mechanism that supports code reuse and the modeling of hierarchical
relationships between types of objects.
Chapter 8: Common Pitfalls and Best Practices
8.1 Common Mistakes
Even experienced programmers occasionally run into these Python-specific pitfalls:
• Mixing tabs and spaces, which can cause indentation errors since Python's blocks are
whitespace-sensitive.
• Modifying a list while iterating directly over it, which can cause items to be skipped or processed
twice.
• Confusing '=' (assignment) with '==' (equality comparison).
• Using a mutable default argument (like a list or dictionary) in a function definition, which is shared
across all calls rather than freshly created each time.
• Forgetting that string and tuple objects are immutable, so operations like 'reversing' them return
new objects rather than modifying the original.
8.2 Best Practices
A few habits go a long way toward writing maintainable Python code:
• Follow PEP 8 style conventions for consistent formatting.
• Write descriptive variable and function names rather than single letters.
• Break large functions into smaller, single-purpose functions.
• Use comments and docstrings to explain non-obvious logic.
• Write small tests as you go rather than only testing the whole program at the end.
Chapter 9: Review and Practice
Practice Questions
Test your understanding with these questions:
• 1. What's the difference between a list and a tuple, and when would you choose one over the
other?
• 2. How does a while loop differ from a for loop in terms of use cases?
• 3. What does 'self' refer to inside a class method?
• 4. Why can mutable default arguments cause unexpected bugs?
• 5. Explain the difference between *args and **kwargs.
• 6. What is the purpose of the __init__ method in a class?