Python Quick Review

jumashafara

Juma Shafara

Posted on July 25, 2024

Python Quick Review

Photo by DATAIDEA

Introduction

Python is a great general-purpose programming language on its own, but with the help of a few popular libraries (numpy, scipy, matplotlib) it becomes a powerful environment for scientific computing.

I expect that you have some experience with Python; and if you don't, this section will serve as a quick crash course

In this tutorial, we will cover:

  • Basic Python: Basic data types (Containers, Lists, Dictionaries, Sets, Tuples), Functions, Classes

A Brief Note on Python Versions

As of Janurary 1, 2024, Python has officially dropped support for python2. We'll be using Python 3.10 for this iteration of the course. You can check your Python version at the command line by running python --version.

# checking python version
!python --version
Enter fullscreen mode Exit fullscreen mode
Python 3.10.12
Enter fullscreen mode Exit fullscreen mode

Don't Miss Any Updates!

Before we continue, we have a humble request, to be among the first to hear about future updates of the course materials, simply enter your email below, follow us on (formally Twitter), or subscribe to our YouTube channel.

Basics of Python

Python is a high-level, dynamically typed multiparadigm programming language. Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable. As an example, here is an implementation of the classic quicksort algorithm in Python:

def quicksort(array):
    if len(array) <= 1:
        return array
    pivot = array[len(array) // 2]
    left = [number for number in array if number < pivot]
    middle = [number for number in array if number == pivot]
    right = [number for number in array if number > pivot]
    return quicksort(left) + middle + quicksort(right)

quicksort([3,6,8,10,1,2,1])
Enter fullscreen mode Exit fullscreen mode
[1, 1, 2, 3, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode
sorted('listen', reverse=True)
Enter fullscreen mode Exit fullscreen mode
['t', 's', 'n', 'l', 'i', 'e']
Enter fullscreen mode Exit fullscreen mode

Variables

Variables are stores of value

name = 'Juma'
age = 19
id_number = 190045
Enter fullscreen mode Exit fullscreen mode

Rules to consider

  • Variable names should be meaningful eg number instead of x
  • Variable names should contain only alpha-numberic characters, and maybe under_scores
  • Variable names can only start with letters or an underscore
  • Variable name cannot contain special characters
  • Variables names are case sensitive

Examples of variables

For this course, we'll use snake case for quick variables

name = 'Eva'
list_of_names = ['Eva', 'Shafara', 'Bob'] # snake case
Enter fullscreen mode Exit fullscreen mode

we'll use camel case for function variables

def calculateBMI(weight_kg, height_m): # camel case
    bmi = weight_kg / height_m ** 2
    rounded_bmi = round(bmi, 3)
    return rounded_bmi
Enter fullscreen mode Exit fullscreen mode

finally, we'll use pascal case for class variables

class MathFunction:    # Pascal Case
    def __init__(self, number):
        self.number = number

    def square(self):
        return self.number ** 2

    def cube(self):
        return self.number ** 3
Enter fullscreen mode Exit fullscreen mode

Basic data types

Numbers

Integers and floats work as you would expect from other languages:

number = 3
print('Number: ', number)
print('Type: ', type(number))
Enter fullscreen mode Exit fullscreen mode
Number:  3
Type:  <class 'int'>
Enter fullscreen mode Exit fullscreen mode
# Quick number arithmetics

print(number + 1)   # Addition
print(number - 1)   # Subtraction
print(number * 2)   # Multiplication
print(number ** 2)  # Enumberponentiation
Enter fullscreen mode Exit fullscreen mode
4
2
6
9
Enter fullscreen mode Exit fullscreen mode
# Some compound assingment operators

number += 1 # number = number + 1
print(number)
number *= 2
print(number)
number /= 1 # number = number / 1
print(number)
number -= 2
print(number)
Enter fullscreen mode Exit fullscreen mode
4
8
8.0
6.0
Enter fullscreen mode Exit fullscreen mode
number = 2.5
print(type(number))
print(number, number + 1, number * 2, number ** 2)
Enter fullscreen mode Exit fullscreen mode
<class 'float'>
2.5 3.5 5.0 6.25
Enter fullscreen mode Exit fullscreen mode
# complex numbers
vector = 2 + 6j
type(vector)
Enter fullscreen mode Exit fullscreen mode
complex
Enter fullscreen mode Exit fullscreen mode

Booleans

Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols:

t, f = True, False
type(t)
Enter fullscreen mode Exit fullscreen mode
bool
Enter fullscreen mode Exit fullscreen mode

Now we let's look at the operations:

# Logical Operators

print(t and f) # Logical AND;
print(t or f)  # Logical OR;
print(not t)   # Logical NOT;
print(t != f)  # Logical XOR;
Enter fullscreen mode Exit fullscreen mode
False
True
False
True
Enter fullscreen mode Exit fullscreen mode

Strings

A string is a sequence of characters under some quotes. Eg.

hello = 'hello'   # String literals can use single quotes
world = "world"   # or double quotes; it does not matter
print(hello, len(hello))
Enter fullscreen mode Exit fullscreen mode
hello 5
Enter fullscreen mode Exit fullscreen mode
# We can string in python
full = hello + ' ' + world  # String concatenation
print(full)
Enter fullscreen mode Exit fullscreen mode
hello world
Enter fullscreen mode Exit fullscreen mode
hw12 = '{} {} {}'.format(hello, world, 12)  # string formatting
print(hw12)
Enter fullscreen mode Exit fullscreen mode
hello world 12
Enter fullscreen mode Exit fullscreen mode
statement = 'I love to code in {}'
modified = statement.format('JavaScript')
print(modified)
Enter fullscreen mode Exit fullscreen mode
I love to code in JavaScript
Enter fullscreen mode Exit fullscreen mode
# formatting by indexing
statement = '{0} loves to code in {2} and {1}'
statement.format('Juma', 'Python', 'JavaScript')
Enter fullscreen mode Exit fullscreen mode
'Juma loves to code in JavaScript and Python'
Enter fullscreen mode Exit fullscreen mode
# formatting by name
statement = '{name} loves to code in {language1} and {language2}'
statement.format(language2='Python', name='Juma', language1='JavaScript')
Enter fullscreen mode Exit fullscreen mode
'Juma loves to code in JavaScript and Python'
Enter fullscreen mode Exit fullscreen mode
# String Literal Interpolation
name = 'Juma'
language1 = 'JavaScript'
language2 = 'Python'

statement = f'{name} loves to code in {language1} and {language2}'

print(statement)
Enter fullscreen mode Exit fullscreen mode
Juma loves to code in JavaScript and Python
Enter fullscreen mode Exit fullscreen mode

String objects have a bunch of useful methods; for example:

string_ = "hello"
print(string_.capitalize())  # Capitalize a string
print(string_.upper())       # Convert a string to uppercase; prints "HELLO"
print(string_.rjust(7))      # Right-justify a string, padding with spaces
print(string_.center(7))     # Center a string, padding with spaces
print(string_.replace('l', '(ell)'))  # Replace all instances of one substring with another
print('  world '.strip())  # Strip leading and trailing whitespace
Enter fullscreen mode Exit fullscreen mode
Hello
HELLO
  hello
 hello 
he(ell)(ell)o
world
Enter fullscreen mode Exit fullscreen mode
statement = 'i love to code in Python '

capitalized = statement.capitalize()
upped = statement.upper()
replaced = statement.replace('Python', 'javascript')
statement.strip()
Enter fullscreen mode Exit fullscreen mode
'i love to code in Python'
Enter fullscreen mode Exit fullscreen mode

You can find a list of all string methods in the documentation.

Containers

  • Python containers (collections) are objects that we use to group other objects
  • Python includes several built-in container types: lists, dictionaries, sets, and tuples.

Lists

A list is an ordered collection of python objects or elements. A list can contain objects of different data types

list_of_numbers = [3, 1, 2]   # Create a list
print(list_of_numbers)
print(list_of_numbers[2])
print(list_of_numbers[-1])     # Negative indices count from the end of the list; prints "2"
Enter fullscreen mode Exit fullscreen mode
[3, 1, 2]
2
2
Enter fullscreen mode Exit fullscreen mode
list_of_numbers[2] = 'foo'    # replacing a specific value in a list
print(list_of_numbers)
Enter fullscreen mode Exit fullscreen mode
[3, 1, 'foo']
Enter fullscreen mode Exit fullscreen mode
list_of_numbers.append('bar') # Add a new element to the end of the list
print(list_of_numbers)  
Enter fullscreen mode Exit fullscreen mode
[3, 1, 'foo', 'bar']
Enter fullscreen mode Exit fullscreen mode
last_item = list_of_numbers.pop()     # Remove and return the last element of the list
print(last_item)    # returns the last item 
print(list_of_numbers) # Modifies the original list
Enter fullscreen mode Exit fullscreen mode
bar
[3, 1, 'foo']
Enter fullscreen mode Exit fullscreen mode

Research on:

  • del
  • remove()

As usual, you can find all the gory details about lists in the documentation.

Slicing

In addition to accessing list elements one at a time, Python provides concise syntax to access a range of values in a list; this is known as slicing:

list_of_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list_of_numbers)
Enter fullscreen mode Exit fullscreen mode
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode
print(list_of_numbers)         # Prints "[0, 1, 2, 3, 4]"
print(list_of_numbers[2:4])    # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(list_of_numbers[2:])     # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(list_of_numbers[:2])     # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(list_of_numbers[:])      # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print(list_of_numbers[:-1])    # Slice indices can be negative; prints ["0, 1, 2, 3]"
list_of_numbers[2:4] = [8, 9] # Assign a new sublist to a slice
print(list_of_numbers)         # Prints "[0, 1, 8, 9, 4]"
Enter fullscreen mode Exit fullscreen mode
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3]
[2, 3, 4, 5, 6, 7, 8, 9]
[0, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 8, 9, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode

Loops

A for loop is used to loop through (or iterate) over a sequence of objects (iterable objects). Iterable objects in python include strings, lists, sets etc

You can loop over the elements of a list like this:

list_of_animals = ['cat', 'dog', 'monkey']

for animal in list_of_animals:
    print(animal)
Enter fullscreen mode Exit fullscreen mode
cat
dog
monkey
Enter fullscreen mode Exit fullscreen mode
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
list_of_squared_numbers = []

for number in list_of_numbers:
    list_of_squared_numbers.append(pow(number, 2))

list_of_squared_numbers
Enter fullscreen mode Exit fullscreen mode
[1, 4, 9, 16, 25, 36, 49, 64, 81, 0]
Enter fullscreen mode Exit fullscreen mode

If you want access to the index of each element within the body of a loop, use the built-in enumerate function:

animals = ['cat', 'dog', 'monkey']

for index, animal in enumerate(animals):
    print(f'{index}: {animal}')
Enter fullscreen mode Exit fullscreen mode
0: cat
1: dog
2: monkey
Enter fullscreen mode Exit fullscreen mode

List comprehensions:

numbers = [0, 1, 2, 3, 4]
squares = []

for number in numbers:
    squares.append(pow(number, 2))

print(squares)
Enter fullscreen mode Exit fullscreen mode
[0, 1, 4, 9, 16]
Enter fullscreen mode Exit fullscreen mode

You can make this code simpler using a list comprehension:

list_of_numbers = [0, 1, 2, 3, 4]

squares = [pow(number, 2) for number in list_of_numbers]

print(squares)
Enter fullscreen mode Exit fullscreen mode
[0, 1, 4, 9, 16]
Enter fullscreen mode Exit fullscreen mode

List comprehensions can also contain conditions:

numbers = [0, 1, 2, 3, 4]

even_squares = [pow(number, 2) for number in numbers if number % 2 == 0]

print(even_squares)
Enter fullscreen mode Exit fullscreen mode
[0, 4, 16]
Enter fullscreen mode Exit fullscreen mode

Research:

  • How to combine lists

Dictionaries

  • A dictionary is an unordered and mutable collection of items
  • A dictionary is created using curly brackets
  • Each item in a dictionary contains a key/value pair
# creating a dictionary
person = {
    'first_name': 'Juma',
    'last_name': 'Shafara',
    'age': 51,
    'married': True
}
person
Enter fullscreen mode Exit fullscreen mode
{'first_name': 'Juma', 'last_name': 'Shafara', 'age': 51, 'married': True}
Enter fullscreen mode Exit fullscreen mode
# accessing items in a dictionary
first_name = person['first_name']
last_name = person['last_name']
full_name = first_name + ' ' + last_name

# display
full_name
Enter fullscreen mode Exit fullscreen mode
'Juma Shafara'
Enter fullscreen mode Exit fullscreen mode
# add items to a dictionary
person['hobby'] = 'Coding'
person
Enter fullscreen mode Exit fullscreen mode
{'first_name': 'Juma',
 'last_name': 'Shafara',
 'age': 51,
 'married': True,
 'hobby': 'Coding'}
Enter fullscreen mode Exit fullscreen mode
email = person.get('email', 'email not available')
print(email)
Enter fullscreen mode Exit fullscreen mode
email not available
Enter fullscreen mode Exit fullscreen mode
# modifying a value in a dictionay
person['married'] = False
person
Enter fullscreen mode Exit fullscreen mode
{'first_name': 'Juma',
 'last_name': 'Shafara',
 'age': 51,
 'married': False,
 'hobby': 'Coding'}
Enter fullscreen mode Exit fullscreen mode
# remove an item from a dictionary
person.pop('age')
person
Enter fullscreen mode Exit fullscreen mode
{'first_name': 'Juma',
 'last_name': 'Shafara',
 'married': False,
 'hobby': 'Coding'}
Enter fullscreen mode Exit fullscreen mode

Research:

  • How to remove an item using the del method
  • How to iterate over objects in a dictionary
  • Imitate list comprehension with dictionaries

You can find all you need to know about dictionaries in the documentation.

Sets

  • A set is an unordered, immutable collection of distinct elements.
  • A set is created using curly braces
  • The objects are placed inside the brackets and are separated by commas
  • As a simple example, consider the following:
animals = {'cat', 'dog'}

print('cat' in animals)   # Check if an element is in a set; prints "True"
print('fish' not in animals)  # prints "True"
Enter fullscreen mode Exit fullscreen mode
True
True
Enter fullscreen mode Exit fullscreen mode
animals.add('fish')      # Add an element to a set

print('fish' in animals) # Returns "True"

print(len(animals))       # Number of elements in a set;
Enter fullscreen mode Exit fullscreen mode
True
3
Enter fullscreen mode Exit fullscreen mode
animals.add('cat')       # Adding an element that is already in the set does nothing
print(len(animals)) 

animals.remove('cat')    # Remove an element from a set
print(len(animals))       
Enter fullscreen mode Exit fullscreen mode
3
2
Enter fullscreen mode Exit fullscreen mode

Research:

  • How to remove with discard()
  • How to remove with pop()
  • How to combine sets
  • How to get the difference between 2 sets
  • What happens when we have repeated elements in a set

Loops: Iterating over a set has the same syntax as iterating over a list; however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set:

animals = {'cat', 'dog', 'fish'}

for index, animal in enumerate(animals):
    print(f'{index}: {animal}')
Enter fullscreen mode Exit fullscreen mode
0: fish
1: cat
2: dog
Enter fullscreen mode Exit fullscreen mode

Set comprehensions: Like lists and dictionaries, we can easily construct sets using set comprehensions:

from math import sqrt

print({int(sqrt(x)) for x in range(30)})
Enter fullscreen mode Exit fullscreen mode
{0, 1, 2, 3, 4, 5}
Enter fullscreen mode Exit fullscreen mode

Tuples

  • A tuple is an (immutable) ordered list of values.
  • A tuple is in many ways similar to a list; one of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. Here is a trivial example:
d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
t = (5, 6)       # Create a tuple
print(type(t))
print(d[t])       
print(d[(1, 2)])
Enter fullscreen mode Exit fullscreen mode
<class 'tuple'>
5
1
Enter fullscreen mode Exit fullscreen mode
# t[0] = 1
Enter fullscreen mode Exit fullscreen mode

Research:

  • Creating a tuple
  • Access items in a tuple
  • Negative indexing tuples
  • Using range of indexes
  • Getting the length of items in a tuple
  • Looping through a tuple
  • Checking if an item exists in a tuple
  • How to combine tuples
  • Prove that tuples are immutable

Functions

  • A function is a group of statements that performs a particular task
  • Python functions are defined using the def keyword. For example:
def overWeightOrUnderweightOrNormal(weight_kg:float, height_m:float) -> str:
    '''
    Tells whether someone is overweight or underweight or normal
    '''
    height_m2 = pow(height_m, 2)
    bmi = weight_kg / height_m2
    rounded_bmi = round(bmi, 3)
    if bmi > 24:
        return 'Overweight'
    elif bmi > 18:
        return 'Normal'
    else:
        return 'Underweight'

overWeightOrUnderweightOrNormal(67, 1.7)
Enter fullscreen mode Exit fullscreen mode
'Normal'
Enter fullscreen mode Exit fullscreen mode

We will often use functions with optional keyword arguments, like this:

bmi = calculateBMI(height_m=1.7, weight_kg=67)

print(bmi)
Enter fullscreen mode Exit fullscreen mode
23.183
Enter fullscreen mode Exit fullscreen mode
def greet(name:str='You')->str:
    """
    This function greets people by name
    Example1:
    >>> greet(name='John Doe')
    >>> 'Hello John Doe'
    Example2:
    >>> greet()
    >>> 'Hello You'
    """
    return f'Hello {name}'

# greet('Eva')
?greet
Enter fullscreen mode Exit fullscreen mode
[0;31mSignature:[0m [0mgreet[0m[0;34m([0m[0mname[0m[0;34m:[0m [0mstr[0m [0;34m=[0m [0;34m'You'[0m[0;34m)[0m [0;34m->[0m [0mstr[0m[0;34m[0m[0;34m[0m[0m
[0;31mDocstring:[0m
This function greets people by name
Example1:
>>> greet(name='John Doe')
>>> 'Hello John Doe'
Example2:
>>> greet()
>>> 'Hello You'
[0;31mFile:[0m      /tmp/ipykernel_66386/2049930273.py
[0;31mType:[0m      function
Enter fullscreen mode Exit fullscreen mode

Classes

  • In python, everything is an object
  • We use classes to help us create new object
  • The syntax for defining classes in Python is straightforward:
class Person:
    first_name = 'John'
    last_name = 'Tong'
    age = 20
Enter fullscreen mode Exit fullscreen mode
# Instantiating a class
object1 = Person()

print(object1.first_name)
print(object1.last_name)
print(object1.age)

print(f'object1 type: {type(object1)}')
Enter fullscreen mode Exit fullscreen mode
John
Tong
20
object1 type: <class '__main__.Person'>
Enter fullscreen mode Exit fullscreen mode
# Instantiating a class
object2 = Person()

print(object2.first_name)
print(object2.last_name)
print(object2.age)
Enter fullscreen mode Exit fullscreen mode
John
Tong
20
Enter fullscreen mode Exit fullscreen mode
class Person:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def greet(self, name):
        return f'Hello {name}'
Enter fullscreen mode Exit fullscreen mode
object1 = Person('Juma', 'Shafara', 24)
print(object1.first_name)
print(object1.last_name)
print(object1.age)
print(type(object1))
Enter fullscreen mode Exit fullscreen mode
Juma
Shafara
24
<class '__main__.Person'>
Enter fullscreen mode Exit fullscreen mode
object2 = Person('Eva', 'Ssozi', 24)
print(object2.first_name)
print(object2.last_name)
print(object2.age)
print(object2.greet('Shafara'))
print(type(object2))
Enter fullscreen mode Exit fullscreen mode
Eva
Ssozi
24
Hello Shafara
<class '__main__.Person'>
Enter fullscreen mode Exit fullscreen mode
class Student(Person):
    def __init__(self, first_name, last_name, age, id_number, subjects=[]):
        super().__init__(first_name, last_name, age)
        self.id_number = id_number
        self.subjects = subjects

    def addSubject(self, subject):
        self.subjects.append(subject) 
Enter fullscreen mode Exit fullscreen mode
student1 = Student('Calvin', 'Masaba', 34, '200045', ['math', 'science'])
Enter fullscreen mode Exit fullscreen mode
student1.addSubject('english')
Enter fullscreen mode Exit fullscreen mode
student1.subjects
Enter fullscreen mode Exit fullscreen mode
['math', 'science', 'english']
Enter fullscreen mode Exit fullscreen mode

Research:

  • Inheritance: This allows to create classes that inherit the attributes and methods of another class

What's on your mind? Put it in the comments!

💖 💪 🙅 🚩
jumashafara
Juma Shafara

Posted on July 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related