Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

01b - Python Basics

Here are a few basic principles in Python (really any programming language):

Assignment operator vs. equality operator

The assignment operator is =. The equality operator is ==. So setting a value of 5 to a variable x is done with x = 5. Checking if x is equal to 5 is done with x == 5. Below is example code:

The value of x is 5 and the result of x==5 is True

Data Types

There are several data types in Python. The most common are int, float, str, bool, and NoneType. int is an integer, float is a decimal number potentially, str is a string like ‘a’ or ‘hello’, bool is True or False, and NoneType is an empty value. The type function can be used to determine the type of a variable. Below is example code:

a is 7, b is 7.97, c is Hello World, d is True, e is None

Other common data types are given below

Tuple

What’s a tuple? How is it different from a list? A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed (immutable) unlike lists and tuples use parenthesis (althouth not required), whereas lists use square brackets.

The value of tup is (4, 5, 6) and tup[0] is 4
The value of anothertup is (7, 8) and the type of anothertup is <class 'tuple'>
(3, 4)

Note that the variable ‘tup’ can be reassigned to a different tuple but that once a tuple has been set, it cannot be changed. This is different from a list which can be changed (as it’s immutable). For example:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 tup[0] = 4 #try and reassign the first element of the tuple to 4

TypeError: 'tuple' object does not support item assignment

Tuples can be useful as in this example where we want to pass multiple values to a function.

The solution is 57.14 for L and 42.86 for V
<class 'tuple'> <class 'tuple'>

Lists and Arrays

Lists and arrays are similar in that they are both a series of values. However, lists can contain different types of values. Arrays are typically all the same type of value. Arrays are also more efficient to use for calculations.

The numbers are [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and the type is <class 'list'>
['hello', 3, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3.14]
5
3.14
3.14
The array is [ 1  2  3  4  5  6  7  8  9 10] and the type is <class 'numpy.ndarray'>

Some python operations can be done on a list but other operations cannot and are better done with an array. For example:

[1, 2, 3, 4, 5, 6]
[5 7 9]

Dictionaries

Dictionary is a collection of key:value pairs. It is similar to a list, but you can access the values by the key instead of the index. Here is an example:

1
1

Dictionaries can be very helpful when you have data and you want to access it by a key. For example, if you have a list of students and you want to access their grades, you can use a dictionary with the student name as the key and the grade as the value. A dictionary can also be thought of as a database. They can easily be converted to a pandas dataframe.

Loops

For and while loops are powerful to complete repetitive tasks. Here are some examples:

[0, 1, 4, 9, 16]
[0, 1, 4, 9, 16, 0, 0, 0, 0, 0]

Inline loop to generate a list

[0, 2, 8, 18, 32, 0, 0, 0, 0, 0]

Conditionals

Conditionals are used to make decisions in code. Most often that is done with an if statement. The if statement checks a condition and then runs the code if the condition is true (or doesn’t run the code if the condition is false). Here is an example:

x is 4

Objects

What’s an object? An object is similar to a variable in that it holds a value or series of values called properties. It can also have functions associated with it. For example, a list is an object and it has functions associated with it like append and sort. You will use objects all the time when you’re using Python. Objects are defined by a class operator.

<class 'list'>
6

I can also make my own objects by defining a class. For example, I can make a class called Person and give it properties like name and age. I can also give it functions like birthday and greet. Here is an example:

Hello, my name is Joe Bai and I'm 25.

I recommend using the param package to define objects. It’s has many benefits.

The density of air at 300 K and 101325 Pa is 1176.88 kg/m^3
Molecular weight of the gas, kg/mol
Help on Number in module param:

    Molecular weight of the gas, kg/mol

Bits

Bits are collections of 1s and 0s (or on versus off in a transistor). They are the basis of all computer operations.

001 is 2^2*1 + 2^1*0 + 2^0*1 = 5
5
'0b101'

Below is a code snippet to calculate the number of bits required to represent a number.

8