Python Programming Basics

Author

Abraham Olvera Barrios & Alasdair Warwick

Published

March 1, 2025

TLDR
  • Python can be used as a calculator for basic arithmetic operations.
  • Variables are assigned using the = operator.
  • DataFrames are used to store tabular data using the pandas library.
  • Understanding different object types in Python is crucial for data manipulation.
  • Logical and comparison operators help in making decisions.
  • Loops and functions are essential for repetitive tasks and code organization.

Welcome to Python programming! This guide will introduce you to the basics of Python, starting with simple calculations, then moving on to assigning variables, working with DataFrames, and exploring different object types.

Introduction

Using Python as a Calculator

Python can be used to perform basic arithmetic operations just like a calculator.

Assigning Variables

In Python, you can assign values to variables using the = operator.

DataFrames

DataFrames are a fundamental data structure in Python, used to store tabular data. Each column in a DataFrame is a series of values of the same type. This structure allows for efficient data manipulation and analysis. DataFrames are created using the pandas library.

Objects

Basic Types

Scalars

Lists

Combine objects of the same type in a list

Access items using square brackets

Dictionaries

Combine objects of different types in a dictionary

Dictionaries can contain dictionaries (nested dictionaries)

Subset dictionaries using square brackets

Access dictionary items with square brackets

DataFrames

A special type of dictionary, where each item is a series of the same length

Subset with square brackets

Access columns with square brackets or dot notation. Both methods return a series

Functions

Functions are blocks of code

Functions may be called to execute the code they contain

Arguments extend function utility

Conditions

Comparison operators

Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 4 True
< Less than 3 < 5 True
> Greater than 3 > 5 False
<= Less than or equal to 3 <= 3 True
>= Greater than or equal to 3 >= 5 False

Logical operators

Operator Description Example Result
& Element-wise AND [True, False] & [True, True] [True, False]
| Element-wise OR [True, False] | [False, True] [True, True]
not NOT not True False
in Checks if elements in one list are present in another list 2 in [1, 2, 3] True

Examples

if, elif, else

Data wrangling

With the foundational knowledge we’ve covered, we can now begin to manipulate our DataFrames effectively.

Filtering rows

Use a boolean mask

Creating columns

Loops

Loops are an essential concept in programming, allowing you to automate repetitive tasks efficiently. They enable you to execute a block of code multiple times, which can save time and reduce errors in your code.

For loops

Do something for each item in an iterable (list/dictionary)

Can also loop through by index using range()

A common pattern is to create an empty list to hold the results from your loop

While loops

Continue looping while a certain condition is met (beware infinite loops!)

Exercises

Now it’s time to practice what you’ve learned. Try the following exercises. Hints are available if you get stuck, and your answers will be graded.

Exercise 1: Basic Arithmetic

Calculate the sum of 15 and 25.

Hint 1

Remember to use the + operator for addition.

15 + 25
Fully worked solution:

Simply add the two numbers:

15 + 25

Exercise 2: Assigning Variables

Assign the value 100 to a variable named a and the value 200 to a variable named b. Then calculate their sum.

Hint 1

Use the = operator to assign values to variables.

a = 100
b = 200
Fully worked solution:

Assign the values and calculate the sum:

a = 100
b = 200
a + b

Exercise 3: Logical Operators

Check if the number 5 is greater than 3 and less than 10.

Hint 1

Use the and operator to combine two logical conditions.

5 > 3 and 5 < 10
Fully worked solution:

Combine the two conditions using the and operator:

5 > 3 and 5 < 10

Exercise 4: For Loop

Write a for loop to create a list containing the numbers 1 to 5.

Hint 1

Use the append() method to add elements to a list.

# empty list
result = []

# append `1` to the empty list using `append()`
result.append(1)
Fully worked solution:

Use append() to grow the list:

result = []
for i in range(1, 6):
  result.append(i)
result

Exercise 5: While Loop

Write a while loop to create a list containing the numbers 1 to 5.

Hint 1

Increment the value of x by 1 in each iteration.

x = 1

# the loop will complete when the value of `x` reaches 6
while x <= 5:
  x += 1
Fully worked solution:

Write the while loop to increment x and grow the list:

result = []
x = 1
while x <= 5:
  result.append(x)
  x += 1
result

Exercise 6: DataFrames - filter rows

Filter this DataFrame for people aged 30 or older.

Hint 1

Use square brackets to filter rows.

# Filter for rows where name is "Alice"
df[df["Name"] == "Alice"]
Fully worked solution:

Filter the DataFrame using square brackets:

df[df["Age"] >= 30]