Tuesday, October 14, 2025

Introduction to Python Basics

Python is a versatile, high-level programming language known for its readability and simplicity. It's widely used for web development, data analysis, automation, and more. Python code is executed line by line, and indentation (usually spaces or tabs) is crucial for defining code blocks like functions or loops—unlike languages that use braces {}.

To get started, you'll need Python installed (download from python.org). You can run code in a terminal, IDE like VS Code, or online REPLs like Replit.Let's dive into the key concepts you mentioned: syntax overview, variables, data types, and functions. I'll include simple examples with their outputs for clarity.1. Basic Syntax
  • Comments: Use # for single-line comments (ignored by Python).
  • Statements: Each line is typically one statement, ending with a newline. Use ; to separate multiple on one line if needed.
  • Indentation: Use 4 spaces to indent code blocks (e.g., inside functions).
  • Case-sensitive: Variablevariable.
Example of a simple "Hello, World!" program:
python
print("Hello, World!")
Output:
Hello, World!
2. VariablesVariables store data and are created when you assign a value using =. No need to declare types upfront—Python infers them (dynamic typing). Variable names should be descriptive, start with a letter or underscore, and use lowercase with underscores (snake_case).Example:
python
# Assigning variables
age = 25
name = "Alice"
print(f"My name is {name} and I am {age} years old.")  # f-string for formatting
Output:
My name is Alice and I am 25 years old.
You can reassign variables:
python
age = 25
age = age + 1  # Now age is 26
print(age)
Output:
26
3. Data TypesPython has built-in types for different kinds of data. You can check a type with type().Common ones:
  • int: Whole numbers (e.g., 42).
  • float: Decimal numbers (e.g., 3.14).
  • str: Text (strings, in quotes: "hello" or 'hello').
  • bool: True or False.
  • list: Ordered, mutable collection [1, 2, 3].
  • dict: Key-value pairs {"key": "value"}.
Example:
python
# Data types
integer = 42
floating_point = 3.14
string = "Python"
boolean = True
list_example = [1, 2, 3]
dictionary = {"key": "value"}

print(f"Integer: {integer}, Float: {floating_point}, String: {string}, Boolean: {boolean}")
print(f"List: {list_example}, Dict: {dictionary}")
Output:
Integer: 42, Float: 3.14, String: Python, Boolean: True
List: [1, 2, 3], Dict: {'key': 'value'}
Operations:
  • Arithmetic: +, -, *, /, // (floor division), % (modulo), ** (exponent).
  • Strings: Concatenate with + or f-strings.
  • Lists: Access by index (e.g., list_example[0] is 1).
4. FunctionsFunctions are reusable blocks of code defined with def, followed by the name, parameters in (), and a colon :. The body is indented. Use return to output a value. Call functions with ().Example:
python
# Define a function
def add_numbers(x, y):
    return x + y  # Returns the sum

# Call the function
result = add_numbers(5, 3)
print(f"5 + 3 = {result}")
Output:
5 + 3 = 8
Default parameters:
python
def greet(name="World"):
    return f"Hello, {name}!"

print(greet("Alice"))  # Hello, Alice!
print(greet())         # Hello, World!
Output:
Hello, Alice!
Hello, World!
Next StepsPractice by writing small scripts! Experiment with conditionals (if/else), loops (for/while), and error handling (try/except). Resources: Official Python tutorial (docs.python.org/3/tutorial/) or freeCodeCamp's Python course.

Lecture Notes: Optimising Numerical Code

Lecture Notes: Optimising Numerical Code Prerequisites: Basic Python programming Understanding of NumPy arrays and vector ...