Python_Environment_Setup

1

Python Environments and Tools

▶

Before writing any Python code, you need a place to write and run it. A Python environment is the combination of a Python interpreter and the tools around it — such as an editor, notebook interface, or terminal — that lets you author, execute, and debug programs. Choosing the right environment for the task at hand makes the learning and development experience significantly smoother.

  • Visual Studio Code (Recommended for this class) VS Code is a free, lightweight, and highly extensible code editor developed by Microsoft that runs on Windows, macOS, and Linux. The official Python extension adds syntax highlighting, IntelliSense code completion, linting, and integrated debugging, making it a full-featured Python IDE with minimal setup. Code is written in .py files and can be run directly inside VS Code using the built-in terminal or the Run button, giving immediate feedback without leaving the editor. VS Code also supports Jupyter notebooks natively, so you can work with .ipynb files alongside standard Python scripts in the same window.
  • Google Colab (Recommended for this class) Google Colab is a free, browser-based notebook environment that requires no installation — you simply sign in with a Google account and start writing Python immediately. Notebooks are organized into cells that can contain code, text, or images, making Colab an excellent tool for combining explanation with executable examples. Colab runs on Google's servers, so it provides free access to CPUs and GPUs without any local hardware requirements, which is particularly useful for data-intensive work. Files are saved directly to Google Drive, making sharing and collaboration straightforward and version history easy to recover.
  • Anaconda Anaconda is a free Python distribution bundled with over 250 pre-installed scientific and data science packages, including NumPy, pandas, and Matplotlib, so you rarely need to install libraries separately. It includes the Conda package and environment manager, which makes it easy to create isolated environments with different Python versions or package sets for different projects. Anaconda ships with Jupyter Notebook and JupyterLab, providing a notebook interface similar to Colab but running entirely on your local machine. The Anaconda Navigator desktop app offers a graphical interface for managing environments and launching tools, which is helpful for those who prefer not to use the command line.
  • Command Line / Terminal Python can be used directly from the operating system's command line — Terminal on macOS/Linux or Command Prompt/PowerShell on Windows — without any additional editor or IDE. Typing python or python3 launches the interactive Python REPL (Read-Eval-Print Loop), where you can type and execute expressions one at a time and see results immediately. Python script files (.py) are run from the terminal with the command python filename.py, making the command line a fast and universal way to execute programs. Understanding command-line usage is valuable regardless of which editor you prefer, because many real-world workflows, deployment processes, and automation tasks rely on running Python scripts from the terminal.
  • Other Environments of Note PyCharm is a dedicated Python IDE from JetBrains that offers deep code analysis, refactoring tools, and strong support for web frameworks like Django and Flask, making it popular in professional development. Jupyter Notebook (standalone) is the original notebook interface on which Colab is based; it runs locally and is widely used in data science, research, and education. Spyder is an IDE oriented toward scientific computing, bundled with Anaconda, and features a variable explorer and interactive console similar to MATLAB's interface. Thonny is a beginner-friendly IDE designed specifically for learning Python, with a simplified interface and a built-in debugger that shows step-by-step execution.
# --- Running Python from the command line ---

# Launch the interactive REPL:
#   $ python3
#   >>> print("Hello, world!")
#   Hello, world!

# Run a script file:
#   $ python3 my_script.py

# Check your Python version:
#   $ python3 --version
#   Python 3.11.4

# --- A minimal first script (save as hello.py) ---
print("Hello, world!")   # run with: python3 hello.py
NotesIntroduces the main environments used to write and run Python code. Visual Studio Code and Google Colab are the recommended tools for this class. Anaconda and command-line usage are also covered as important alternatives, along with a brief survey of other popular environments students may encounter.
2

Variables and Assignment

▶

Variables are one of the most fundamental building blocks in any programming language. In Python, variables act as named references that point to values stored in memory, and they can be created, updated, and reused throughout a program with minimal ceremony.

  • What Is a Variable? Variables are created the moment a value is assigned to them using the = operator — there is no need for a separate declaration step as in some other languages. The same variable can be reassigned to a different value or even a different type at any time, because Python is dynamically typed and does not lock a variable to a specific data type. Descriptive variable names make code more readable and easier to maintain, helping both the original author and future collaborators understand the purpose of stored data at a glance.
  • Naming Rules and Conventions Variable names must start with a letter or underscore and can contain letters, digits, and underscores — names beginning with a digit are invalid and will cause a syntax error. Names are case-sensitive, so score, Score, and SCORE are three distinct variables, each capable of holding a completely independent value. Python keywords such as if, for, and return are reserved and cannot be used as variable names, as doing so would conflict with the language's own syntax and raise an error.
  • Multiple Assignment and Unpacking Chained assignment sets multiple variables to the same value in a single line, for example x = y = z = 0 initialises all three to zero simultaneously. Tuple unpacking assigns each element of a sequence to a corresponding variable, as in a, b, c = 1, 2, 3, which is both concise and expressive. Multiple assignment improves code conciseness but should be used carefully to maintain clarity, since assigning too many variables on one line can make the code harder to read and debug.
# Simple assignment
name = "Alice"
score = 42

# Reassignment to a different type
score = "forty-two"

# Chained assignment
x = y = z = 0

# Tuple unpacking
first, second, third = 10, 20, 30
print(first, second, third)  # 10 20 30
NotesIntroduces how Python uses variables to store and reference data, covering naming conventions, assignment syntax, and how Python manages variable references in memory.
3

Core Data Types

▶

Python provides a rich set of built-in data types that cover the most common needs in everyday programming. Understanding the characteristics of each type — and when to use it — is essential for writing correct, efficient, and readable code.

  • Integers and Floating-Point Numbers Integers represent exact whole numbers with no size limit in Python, so there is no overflow for extremely large values the way there is in languages with fixed-width integers. Floats can represent decimal values but may produce small rounding errors due to binary representation, which is why financial applications often use the decimal module instead. Arithmetic between an int and a float produces a float result, as Python automatically widens the result to preserve precision.
  • Strings Strings are immutable, meaning their contents cannot be changed after creation — any operation that appears to modify a string actually produces a brand-new string object. Individual characters in a string can be accessed using index notation, e.g., text[0], and negative indices count from the end, so text[-1] retrieves the last character. String concatenation combines strings using the + operator, though for joining many strings together the join() method is far more efficient. f-strings allow variables and expressions to be embedded directly into string literals using curly-brace syntax, making formatted output concise and readable.
  • Booleans Boolean values are produced by comparison and logical operations, and they form the backbone of all conditional branching and loop control in Python. True and False are Python keywords and must be capitalized — writing true or false in lowercase will raise a NameError. Any Python object can be evaluated as a boolean in a conditional context, with empty containers, zero, and None being treated as falsy and most other values as truthy.
  • The None Type None is the only value of NoneType and is not the same as zero, False, or an empty string — it explicitly represents the absence of a value. Functions that lack a return statement implicitly return None, so calling such a function and using its return value without checking can lead to subtle bugs. None is commonly used to initialize variables that will be assigned a real value later, signalling clearly that the variable is intentionally empty at that point in the program.
  • Checking Data Types with type() type() accepts any Python object and returns its class, for example type(3.14) returns <class 'float'>. Knowing the type of a variable is essential before performing operations that require a specific type, since Python will raise a TypeError if incompatible types are used together without conversion. isinstance() is preferred over type() for type-checking in conditional statements, because it correctly handles inheritance and is considered more Pythonic.
# Integers and floats
whole = 100
decimal = 3.14
print(whole + decimal)      # 103.14  (float result)

# Strings
greeting = "Hello"
print(greeting[0])          # H
name = "World"
print(f"{greeting}, {name}!")

# Booleans
print(type(True))           # <class 'bool'>
print(bool(""))             # False
print(bool(42))             # True

# None
result = None
print(type(result))         # <class 'NoneType'>

# Type checking
print(isinstance(3.14, float))  # True
NotesCovers Python's fundamental built-in data types including integers, floats, strings, and booleans, explaining their characteristics and appropriate use cases.
4

Operators in Python

▶

Operators are the symbols that tell Python how to process and combine values. From basic arithmetic to logical decision-making, a solid understanding of Python's operator categories and precedence rules is essential for writing expressions that behave exactly as intended.

  • Arithmetic Operators The / operator always returns a float, while // performs integer (floor) division, discarding any remainder and returning the largest whole number not exceeding the result. The % operator returns the remainder of a division, making it especially useful for checking whether a number is even, odd, or divisible by some value. The ** operator raises a number to a power, so 2**8 evaluates to 256 and can be combined with fractional exponents to compute roots.
  • Comparison Operators Comparison operators always return either True or False, making them the natural tool for building conditions in if statements and while loops. The == operator checks value equality, while is checks object identity — two objects can be equal in value yet reside at different memory addresses. Comparisons can be chained in Python, so an expression like 1 < x < 10 is valid, readable, and equivalent to 1 < x and x < 10.
  • Logical Operators and returns True only if both operands are true, so a single falsy operand is enough to make the entire expression falsy. or returns True if at least one operand is true, and stops evaluating as soon as a truthy value is found. not inverts a boolean expression, turning True to False and vice versa, and is often used to make conditional checks read more naturally. Python uses short-circuit evaluation, stopping as soon as the outcome is determined, which can also be used deliberately to avoid errors in compound conditions.
  • Assignment Operators The = operator assigns the value on the right to the variable on the left and is not to be confused with the == equality check. Augmented operators like += combine an arithmetic operation with assignment in one step, so count += 1 is a concise equivalent of count = count + 1. Using augmented assignment operators improves code readability in iterative operations, such as accumulating totals or building strings in a loop.
  • Operator Precedence Expressions inside parentheses are always evaluated first, regardless of any other operators present, making parentheses the safest tool for enforcing a desired evaluation order. Exponentiation has higher precedence than multiplication or division, so 2 * 3**2 evaluates as 2 * 9 = 18, not 6**2 = 36. When operators share the same precedence level, they are evaluated left to right (left associativity), with the notable exception of the exponentiation operator, which is right-associative.
# Arithmetic
print(7 / 2)      # 3.5
print(7 // 2)     # 3
print(7 % 2)      # 1
print(2 ** 8)     # 256

# Comparison
x = 5
print(1 < x < 10)   # True
print(x == 5)        # True

# Logical with short-circuit
print(True or 1/0)   # True  (right side never evaluated)

# Augmented assignment
count = 0
count += 1
print(count)  # 1

# Precedence
print(2 * 3 ** 2)    # 18
print((2 * 3) ** 2)  # 36
NotesIntroduces the main categories of Python operators — arithmetic, comparison, logical, and assignment — explaining their syntax, behavior, and precedence rules.
5

Type Conversion

▶

Data in real programs rarely arrives in exactly the type needed for a given operation. Python handles many type mismatches automatically, but programmers must also know how and when to convert types explicitly — and how to deal gracefully with conversions that can go wrong.

  • Implicit Type Conversion Python automatically promotes int to float when they are used together in arithmetic, ensuring that no precision is lost in the resulting value. Implicit conversion only occurs when it is safe and does not result in data loss, so Python will never silently truncate or corrupt a value behind the scenes. Incompatible type combinations, such as adding a string to a number, raise a TypeError, because Python cannot make a safe assumption about the programmer's intent in such cases.
  • Explicit Type Conversion with Built-in Functions int() converts a compatible string or float to an integer, truncating any decimal portion — so int(3.9) yields 3, not 4. float() converts an integer or numeric string to a floating-point number, enabling decimal arithmetic even when the original value was whole. str() converts any value to its string representation, which is essential when concatenating numbers with text or writing output to a file. bool() converts a value to True or False based on its truthiness, following the same rules that Python applies when evaluating any object in a conditional context.
  • Common Conversion Errors and How to Avoid Them Converting a non-numeric string to int or float raises a ValueError, because Python cannot interpret arbitrary text as a number. Mixing incompatible types without conversion raises a TypeError, which is Python's way of signalling that the operation is not defined for the given combination of types. Wrapping conversions in try/except blocks allows programs to handle invalid input gracefully, displaying a helpful error message rather than crashing unexpectedly.
# Implicit conversion
result = 4 + 1.5
print(result)         # 5.5  (float)
print(type(result))   # <class 'float'>

# Explicit conversion
print(int(3.9))       # 3
print(float(7))       # 7.0
print(str(100))       # '100'
print(bool(0))        # False
print(bool("hello"))  # True

# Handling conversion errors
user_input = "abc"
try:
    number = int(user_input)
except ValueError:
    print(f"Cannot convert '{user_input}' to an integer.")

# TypeError example
try:
    result = "age: " + 30
except TypeError as e:
    print(e)  # can only concatenate str (not "int") to str
NotesExplains how Python handles conversion between data types, distinguishing between implicit coercion performed automatically by Python and explicit conversion performed by the programmer.