Let’s Learn Python #3

All you need to know to get started with Python

Nyx Iskandar
4 min readNov 28, 2021

Do you want to start learning Python but can’t find free learning and practice resources? Look no further than this four-part series about the fundamentals of Python (complete with coding notebooks to get you started right away)!

In this article (part 3), we will be answering the following questions:

  • What are functions?
  • What are tuples and lists?
  • What are dictionaries?

Let’s start!

What are functions?

The DRY Principle

In programming, there is one important principle to remember: Don’t Repeat Yourself. This principle, affectionally nicknamed DRY, is quite self-explanatory; as its name suggests, the principle recommends that the code you write is kept as organised and as simple as possible.

More Code != Better Code

So, how do we abide by the DRY Principle? The answer lies in two words: decomposition and abstraction (yay more jargon!). Don’t worry about the complicated names. Essentially, decomposition is breaking down code into distinct reusable blocks, and abstraction is hiding tedious details to keep code readable. By embracing this concepts, you’ll be able to gradually code more DRY-ly!

That’s all well and good, but how do functions figure in all of this?

I’m glad you asked!

Functions

Functions are, at its core, reusable blocks of code.

A simple function to add two integers together

To define a function, you must specify the following:

  1. The name of the function (e.g. add) preceded with the def keyword
  2. The parameter(s) the function can take (e.g. x and y)
  3. The body of the function (what the function does) which is indented (e.g. return x + y)

You can then call (or use) the function by writing its name with the arguments (or parameters) to be passed into it in parentheses.

More often than not, a function returns a value or a couple of values after processing its parameters. To do so, we use the return keyword, which outputs (not prints) a value from the function.

return vs print()

It is also important to take note of the scope of variables. For the add() function above, x and y only exist within the function body. In other words, trying to access both variables outside of the function will yield an error as they do not exist beyond the scope of the function.

DRY x Functions

Now, let’s go back to decomposition and abstraction. How does a function achieve them and thus the DRY Principle? Well, the very definition of a function (a reusable block of code) suggests that a function achieves decomposition. A function also achieves abstraction because when other programmers use your code for their own projects, they won’t need to know how exactly you defined certain functionalities in your code, simplifying their work and allowing them to focus on developing novel functionalities specific to their project. As a result, minimal code repetition is done and hence the DRY Principle is fulfilled.

What are tuples and lists?

Mutability

Before we explore tuples and lists, we must first understand the concept of mutability. When something is mutable, it means that that something is changeable. The opposite of mutable is immutable.

Strings, for example, are immutable; when you try to change a certain character or substring in a string, you will get an error. Don’t believe me? Try it for yourself!

Tuples

A tuple is an immutable ordered sequence of elements represented with parentheses. Sometimes, you can leave out the parentheses, but I recommend you write them keep things readable.

Working with tuples

If you’re wondering, tuples do have helpful uses. They can be used to swap variables simply, as well as return multiple values from a function.

Swapping x and y
Returning multiple values from a function

Lists

A list is a mutable ordered sequence of elements represented with square brackets.

Working with lists

In Python, we are equipped with built-in functions for lists. Here are a few of them you can experiment with (Google for more!):

  • .append()
  • .remove()
  • .pop()
  • del()
  • sorted()
  • .sort()
  • .reverse()
  • .join()

Loops

Want to loop over the values in tuples and lists? Well you’re in luck! Python allows us to do that (quite easily too)!

Implementing a for loop on a list

What are dictionaries?

A dictionary is a data structure consisting of key-value pairs represented with curly braces.

Defining a dictionary

The keys in a dictionary must be unique and immutable, but the values can be duplicates and mutable/immutable.

There are also built-in operations you can do to manipulate dictionaries. Let’s try some of them out!

Adding and deleting key-value pairs

You can also loop through dictionaries!

Implementing a for loop on a dictionary

Lesson & Practice Notebooks

That’s all for this article! If you want to consolidate your learning by actually coding, follow these steps:

  1. Go to this GitHub repository
  2. Click the green Code button (refer to the image below)
  3. Click Download ZIP (refer to the image below)
  4. Open the downloaded .zip file
  5. Find Lesson 3.ipynb and Practice 3.ipnyb
  6. Upload the files in 5. on Google Colab
  7. Start coding 🎉
For steps 2. and 3.

Next up…

Hopefully, this article has helped you gain a better understanding of programming and the very basics of Python.

In the next article (part 4), we will be answering the following questions:

  • What is Object Oriented Programming (OOP)?
  • What are classes?
  • What is inheritance?

Make sure to follow me to not miss out on the next part of the Let’s Learn Python series!

Happy coding!

--

--