Learn Python

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics, known for its readability and ease of use.


What is Python?

Python is a high-level, general-purpose programming language known for its simplicity and readability. It’s widely used in fields ranging from web development and data analysis, to scientific computing, automation, and HPC workflows.

Python Basics

Variables

Variables in Python are dynamic, meaning you don’t have to declare their data type.

name = "Chloe"
age = 30
pi = 3.14159

Data Types

Despite not needing to declare the data type of a variable, Python still associates every value with a data type. These data types determine how Python treats and processes data.

Type

Description

Example

int

Integer numbers

42

float

Decimal numbers

3.14

str

String of characters

"hello"

bool

Boolean logic

True

list

Ordered, changeable

[1. 2, 3,]

tuple

Ordered, unchangeable

(1, 2)

dict

Key-value pairs

{"name": "Chloe"}

set

Unordered unique values

{1, 2, cat, dog}

Conditional Statements

Conditional statements are control mechanisms that allow a program to execute different blocks of code based on whether a specific condition is met.

if x > 0:
    print("positive")
elif x < 0:
    print("negative")
else:
    print("zero")

If

The if statement is the most basic type of conditional statement. It executes a block of code only if the associating condition is true.

if condition:
    # code to execute if the condition is true

If-Else

The if-else statement provides an alternate path of execution if the associated condition is false.

if condition:
    # code to execute if condition is true
else
    # code to execute if the condition is false

If-Elif-Else

The if-elif-else statement allows for the checking of multiple conditions. If the if condition is false, the elif conditions are checked in order, and if neither the if or the elif conditions are true, the else block is exexecuted.

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition1 is fasle, and condition2 is true
else:
    # code to execute if all preceding conditions are false

Loops

A loop is a control flow mechanism that allows code to be executed repeatedly, continuing repetition for a specified number of times or until a condition is met.

For

The for loop is used for iterating over a sequence of objects. The loop executes the code block once for each item in the sequence.

colors = ["red", "blue", "yellow"]
for color in colors:
    print(color)

While

The while loop is used to execute a code block repeatedly as long as the given condition remains true. The loop continues until the condition is false.

count = 0
while count < 5:
    print(count)
    count += 1

Functions

Functions let you reuse blocks of code and structure logic clearly.

def greet(name):
    return f"Hello, {name}!"

Some functions come built into Python and are commonly used like, print() or len(). A complete list of built-in Python functions can be found here

Working with Libraries

Python libraries are a collection of pre-written, reusable code modules that extend Python’s functionality and allow programmers to perform various tasks without having to write code from scratch.

  • The Python Standard Library is a collection of modules and packages that are included with the Python installation.

  • The Python Package Index (PyPI) is an additional active collection of hundreds of thousands of components, from individual programs and modules to packages and entire application development frameworks. PyPI acts as a central hub where developers can share and find Python packages and libraries. PyPI integrates with the pip package manager, making it simple to install packages directly from the index.

Use import to add built-in or external libraries to your code base:

import math
print(math.sqrt(16))

Some common libraries include:

  • numpy - numerical computing

  • scipy - scientific tools

  • matplotlib - plotting

  • pandas - dataframes


Resources