Python Basics

Table of contents

1. Variable replacement

v = "sushi" 
u = 10
print(v) 
# sushi 
print("I like " + v) 
# I like sushi
print ('I like ', v) 
# I like sushi
print(f'I like {v}') 
# I like sushi
print("I like %s" % (v)) 
# I like sushi
print("I ordered %s and I paid $%f" % (v, u)) 
# I ordered sushi and I paid $10.000000
print("I ordered %s and I paid $%i" % (v, u)) 
# I ordered sushi and I paid $10

We use input() to prompt users for input from the command line:

name = input('What is your name?\n')
print(f'Hello {name}.')

2. Lists

Lists in Python can hold multiple item types. Here’s how you can create a list:

items = ["sushi", 10, "sashimi", True]

You can check whether an item is in a list by using the in operator:

print("sashimi" in items) 
# True

You can use the index() method to find the index of an item in the list:

items.index("sushi") 
# 0
items.index(10) 
# 1

Using a negative index will access items from the end:

items[-1] 
# True

You can also extract a part of a list, using slices:

items[0:2] 
# ["sushi", 10]
items[2:] 
# ["sashimi", True]

You can get the number of items contained in a list using the len() function:

len(items) 
# 4

You can add items to a list by using append(), extend(), or =+:

items.append("nigiri")
items.extend(["nigiri"])
items += ["nigiri"]

They will all have the same output below:

items = ["sushi", 10, "sashimi", True, "nigiri"]

You can remove an item from a list by using the remove() method with the name of the item you want to remove:

items.remove("nigiri")

To add an item in the middle of a list, at a specific index, use the insert() method:

items.insert(1, "nigiri") # adds "nigiri" at index 1

The list will now become:

items = ['sushi', 'nigiri', 10, 'sashimi', True]

2.1. List copying

You can copy a list in Python, by using the copy() method, or the list() method, or the slice operator [:]. All will create a new list that is a copy of the original list, so you can modify the new list without affecting the original list.

Using the copy() method:

original_list = [1, 2, 3]
new_list = original_list.copy()  

Using the list() method:

original_list = [1, 2, 3]
new_list = list(original_list)

Using the slice operator [:] method:

original_list = [1, 2, 3]
new_list = original_list[:]

2.2. List sorting

You can sort a list by using the sort() method:

items.sort(key=str.lower)

You use key=str.lower to normalize the sort because uppercase is ordered before lowercase.

Note: Sorting modifies the original list.

To avoid that, you can make a copy of the list first, like this:

items_copy = items[:]

You can also use the sorted() method to return a sorted copy of the original list:

items_copy = sorted(items, key=str.lower))

2.3. List iteration

In Python, you can iterate over a list using a for loop. In this example, we first create a list called my_list with five integers. Then, we use a for loop to iterate over each item in the list, and print it to the console.

my_list = [1, 2, 3, 4, 5]

for item in my_list:
    print(item)

# 1
# 2
# 3
# 4
# 5

You can also use the range() function to specify the number of times you iterate:

for item in range(4):
    print(item)

# 1
# 2
# 3
# 4
# 5

You can also use a while loop to iterate over a list.

In this example, we use a while loop to iterate over each item in the list, and print it to the console. We use a counter variable i to keep track of the index we’re currently at in the list. We use the len() function to get the length of the list, and make sure we stop iterating when we reach the end of the list.

my_list = [1, 2, 3, 4, 5]

i = 0
while i < len(my_list):
    print(my_list[i])
    i += 1

# 1
# 2
# 3
# 4
# 5

To iterate over a two-dimensional list in Python, you can use nested for loops. In this example, we first create a two-dimensional list called my_list with three rows and three columns. Then, we use a nested for loop to iterate over each item in the list, and print it to the console. The outer for loop iterates over each row in the list, while the inner for loop iterates over each item in the current row.

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in my_list:
    for item in row:
        print(item)

Alternatively, you can use indexing to access the elements in a two-dimensional list. In this example, we use nested for loops to iterate over the list using indices. The outer loop iterates over each row in the list, while the inner loop iterates over each item in the current row using indices. We use the len() function to get the length of each row, and the range function to generate the indices for the loops.

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for i in range(len(my_list)):
    for j in range(len(my_list[i])):
        print(my_list[i][j])

You can also do this using a while loop. In this example, we use nested while loops to iterate over the list using indices. The outer while loop iterates over each row in the list, while the inner while loop iterates over each item in the current row using indices. We use the len() function to get the length of each row, and the counter variables i and j to keep track of the current indices.

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

i = 0
while i < len(my_list):
    j = 0
    while j < len(my_list[i]):
        print(my_list[i][j])
        j += 1
    i += 1

To access and print out the index, you should wrap the sequence into the enumerate() function:

items = [1, 2, 3, 4]
for index, item in enumerate(items):
    print(index, item)

4. Dictionaries

Coming soon.


5. Tuples

Coming soon.


6. Functions

Coming soon.


7. Classes

Coming soon.


8. Introspection

Introspection in Python means that you can analyze: Functions, variables and objects. This is mostly helpful for troubleshooting without using a debugger tool. In this section we will outline 3 different introspection tools commonly used in Python.

8.1. The dir() method

The dir() global function allows you to return all properties and methods of an object including the built in ones as a list. This is very useful especially when you are not sure what attributes or methods can be called on a specific object.

>>> from courses.models import Course
>>> courses = Course.objects.all()
>>> print(dir(courses[0]))

# ['check', 'clean', 'clean_fields', 'created', ... ]

We basically get a list of both private and public functions and attributes. Private functions and attributes are the ones that begin with an underscore.

8.2. The inspect module

Another good introspection method to know is inspect. It's a built-in python module that can also be used to get comprehensive information about a Django object. The getmembers() method return a list of tuple of all the attributes and functions of an object.

>>> from inspect import getmembers
>>> getmembers(user)

But a better way is to filter and return just the functions using the inspect.isfunction() method:

>>> from inspect import getmembers, isfunction
>>> from django.contrib.auth.models import User
>>> functions = [x[0] for x in getmembers(User) if isfunction(x[1])]
>>> print(functions)

8.3. The __dict__ attribute

And finally, you can use the __dict__ attribute of python objects which is used to store an object’s (writable) attributes. This allows you to see the readable attribute name as well as the values it contains. You use it by calling .__dict__ on the object like so: user.__dict__.