Explaining Those Confusing Python Concepts
Announcement
Just added the 11 new videos about working with databases to Master Python Fundamentals—The Ultimate Python Course for Beginners.
You will learn how to interact with a database using Python, and you will create an inventory app that interacts with a database.
When you sign up for this course, you will get two books.
Now, you will not just learn Python but will also have enough challenges to tackle as you learn. This Python course is growing with more videos to be added.
Check out this video on viewing data in a database. Click here to watch.
Introduction
When you are learning Python, it is very likely that you will come across some terms or concepts that will be hard to wrap your head around. I have been there. Words like parameters, arguments, iterables, iterators, and even sort vs. sorted might seem interchangeable or confusing at first, even though they are different. Sometimes the best way to understand confusing concepts is to try to see them through analogies and some simple examples. In this article I will break down these concepts through practical examples and relatable analogies to make them easier to grasp.
Understanding the distinctions between these terms is crucial for anyone trying to write proper Python code. Let's jump in.
Parameters vs. Arguments
These two terms often confuse beginners, and even experienced programmers sometimes mix them up. Let’s use an illustration to make the distinction clear. In show business, there’s a concept called seat fillers. Seat fillers are ordinary people hired to temporarily occupy seats assigned to celebrities. When the celebrities arrive, the seat fillers vacate, allowing the celebrities to take their rightful place.
In programming, parameters are like seat fillers. They serve as placeholders in the function definition, waiting to be replaced by real values. These real values, called arguments, are passed into the function when it is called. Here is an example:
In this example, parameters a, b, and c are the placeholders in the function definition. Arguments: "USD", "EUR", and "2024-12-12" are the actual values passed into the function, replacing the placeholders.
Data Analysis with Python: Practice Practice Practice.
The main purpose of this book is to ensure that you develop data analysis skills with Python by tackling challenges. By the end, you should be confident enough to take on any data analysis project with Python. Start your 50-day journey with "50 Days of Data Analysis with Python: The Ultimate Challenge Book for Beginners."
Recommended by LinkedIn
Iterables vs. Iterators
Iterables and iterators can be confusing terms. Simply put, an iterable is an object that can be iterated over, meaning you can access its elements one by one. It has an __iter__() method that returns an iterator. Examples of iterables are lists, tuples, strings, dictionaries, and sets. An iterator is an object that iterates over an iterable and keeps track of the current state of iteration. An iterator has a __next__() method that returns the next element in the sequence. When there are no more elements in a sequence, it raises the StopIteration exception. Let's use an illustration to explain the two terms:
Imagine you have a box full of small toy cars. The box has a tiny hole on its side, just large enough to pull out one car at a time. The cars are neatly arranged in rows inside the box. This box containing the toy cars represents an iterable. It holds all the cars (data) and provides a way to access them. However, the box itself doesn’t actively push the cars out; it simply contains them, ready for retrieval one by one.
Now, let's say you want to retrieve all the cars in the box by retrieving a car at a time, following the sequence of rows in which the cars are placed.
Since the hole is small, your hand is too big to grab the cars from the box and tell which car in the row to grab next. So, you call your daughter, who has a smaller hand. Your daughter can:
Because she can do these three things, her hand represents an iterator. Without your daughter, you wouldn't be able to retrieve the cars from the box one by one. Let's put this illustration in a code:
In this code, the toy_box is an iterable. Your daughter's hand is an iterator. It performs an iteration. Her hand is like an object with the __next__() method. It knows how to pull items one at a time while keeping track of its position. Without this method, you wouldn’t be able to systematically retrieve the cars from the box.
Sort Method Vs Sorted Function
The sort() method and the sorted() function technically do the same thing but behave differently. Here is an example to illustrate the different behavior. Let's say you have a folder of documents and you want to sort them by the date they were produced. If you use the sort() method, it will organize the documents directly in the same folder. No new folder is created, and the original order of the documents in that folder is changed.
If you use the sorted() function, it takes the documents from the original folder, arranges them by date, and places them into a new folder, leaving the original folder unaltered. You now have two folders:
The sort() method is a list method that sorts a list in place. It does not create a new list. It returns None. Here is an example:
You can see that trying to get an output from the sort() method returns None. This means it modifies the existing list. If we check my_list after using the sort() method, you can see that it has been arranged in ascending order.
The sorted() function, on the other hand, is a high-order function that leaves the original iterable unchanged and creates a new sorted object. See below:
You can see that the sorted() function is returning a sorted list, but when we print the original list (my_list), the order has not changed. This is because the sorted function has not modified the original list; it has created a new sorted list from the original list. Use the sort() method if you want to sort a list in place. If you want to create a sorted list from the original list, you must use the sorted() function.
Final thoughts
We are going to leave it here, but I’m sure by now you get the gist of it. Even though terms like parameters vs. arguments, iterables vs. iterators, and the sort() method vs. the sorted() function are often used interchangeably by beginners, they each have specific meanings and behaviors in Python.
Understanding the differences will help you write proper Python code. It will also help you troubleshoot when things go wrong. For instance, knowing when to use the sort() method versus the sorted() function can help avoid unintended side effects, such as altering the original list when you don’t mean to. Similarly, understanding how iterators work can allow you to optimize memory usage when working with large datasets. Thanks for reading, and keep learning Python.
Application Developer I @ Georgia Tech | Java, Spring, Agile, JavaScript, SQL, Python, Node.js, VueJS
1wExcellent article as always Benjamin. I especially love the examples you use to distinguish each term.
OK Boštjan Dolinšek
ERP | IT Business Application Analyst | IT Risks Audit | Workflow Optimization and Process Improvement Consultant | Software QA Integration/Implementation/Customization/Testing | Operations | Project management
3wthank you
Data Analyst
3wThis was a great article Benjamin, with very clever examples of the concepts. Your example of iterables and iterators, using your daughter's hand as the iterator, was perfect! This illustration actually helps you to visualize the sequence of events that take place from start to finish, during iteration of an element. Thanks for the post!
Army
3wVery informative