Python Basics

A Python program is made up of code blocks, pieces of code executed as units by the Python interpreter (read more). Code blocks can be nested, e.g. the body of a function (block) within a script file (block).

script.py
# This is a code blockdef f():    # This is a nested code block (that does nothing)    pass

Code blocks define the scope of named objects, created through binding operations such as assignment. The scope defines were the named objects are available.

script.py
# An int object with value 1 is assigned the name aa = 1def f():    # A variable is defined within the scope of the function body    b = 2# Variable a, but not variable b, is defined here in the outer scope
Variables in Python can contain letters, numbers, and underscores, but can not start with a number. A common naming convention is to use underscores to separate words, as in my_variable.

Contrary to many other programming languages that use special characters to define the limits of expressions, e.g. { ... }, python uses indentation. Indentation can be done by either tabs or spaces. Recommended is four spaces.

script.py
if a == b:    # This is within the if statement    pass# This is outside the if statement

Variables and Types

Every object in Python has a type, such as int (integer), float (decimal number), or str (string/text). As Python is dynamically typed, it is not necessary to explicitly specify the type of a named variable and a variable will change its type depending on the object associated with it. The built-in types are listed here.

a = 1  # a is an integera = "Text"  # a is now a string

The most common types are bool for boolean values, int, and float for numbers, str for text, list for lists, and dict for dictionaries (key/value pairs).

my_bool = Truemy_int = 1my_float = 1.5my_str = "Some text"my_list = [1, True, "Text"]my_dict = ["key_1": "value_1", 2: True, False: "Text"]

Conditionals

If/Else

if a > b:    print("a is greater than b")elif a < b:    print("a is less than b")else:    print("a is equal to b")

Match/Case

match number:    case 10:        print("Ten")    case 100:        print("One Hundred")    case 1000:        print("One Thousand")    case _:        print("Dafault valie if no case matches")

Conditional Expression

"a" if a > b else "b"

Loops

Loops are blocks of code that are executed multiple times, either for each item in an iterable (for loop) or while an expression holds true (while loop)

For Loop

for i in [1, 2, 3]:    print(i)# 1# 2# 3

While Loop

i = 1while i <= 3:    print(i)    i += 1  # i = i + 1# 1# 2# 3

Comprehensions

List/dictionary comprehension is a useful and compact way to create lists or dictionaries where we would otherwise have had to use a for loop.

my_old_list = [1, 2, 3, 4, 5]my_new_list = [i ** 2 for i in my_old_list]# my_new_list == [1, 4, 9, 16, 25]

Which is equivalent to using an ordinary for loop.

my_old_list = [1, 2, 3, 4, 5]my_new_list = []for i in my_old_list:    my_new_list.append(i ** 2)# my_new_list == [1, 4, 9, 16, 25]

Functions

Functions are named blocks of code that can be called from elsewhere in the program. They are defined with the def keyword, can take different arguments, and can optionally return one or more values.

def my_function(a, b):    c = a + b    return str(c)my_var = my_function(1, 2)# my_var == "3"

Classes

Classes define custom object and are the basis of object-oriented programming. Although it is unavoidable to use other peoples custom objects through imported packages, it is often not necessary to create our own classes when doing basic data science.

from dataclasses import dataclass@dataclassclass Ball:    # Variables    size: int    color: str    # Method    def grow(self, add_size):        self.size += add_sizeball = Ball(3, "red")ball.grow(2)# Ball(size=5, color="red")
A function that is defined in a class and belongs to an object is called a method.

F-Strings

Python has a special type of string that can be used to combine strings with Python code, called the f-string, which can replace the more common string concatenation with the + operator.

my_fstring = f"1 + 3 = {1 + 3}: this is a string"# Equivalent toomy_string = "1 + 3 = " + str(1 + 3) + ": this is a string"# my_fstring == my_string == "1 + 3 = 4: this is a string"

Imports

To use extended functionality, it is necessary to import Python packages into your program.

# Import the os packageimport os# Import the pandas package into the namespace pdimport pandas as pd# Import only DataFrame from the pandas packagefrom pandas import DataFrame