4 Essential Techniques You Must Learn as a Python Beginner
The Lambda Function
Let's say you are working with some data in a Jupyter notebook, just doing some quick exploration and analysis. You're still in the early stages of data cleaning and processing, far from any production-ready models or visualizations or applications. But you do have a deadline to meet, so you're exploring quickly and efficiently, making use of your stellar Python skills.
In the midst of your adventures, you come across a column in your data that requires transformation. You just need to square the numbers in the column. It's nothing crazy, but it's unfortunately also one of those weird necessities that is simple enough to be quick, but complex enough to not have its own built-in function.
So, you decide to use Pandas's apply
function to transform the data column using your own, custom function [1]. To do this, you need to write a function that squares numbers, and you do so in the only way you know how:
def square(num):
return num * num
This gets the job done, but it's a little annoying and messy, especially for a Jupyter notebook. It doesn't meld well with the one-line structure of most Pandas operations, and accordingly won't look very nice when your colleagues review your notebook.
But do not despair, my friend, for the lambda function is here to save you. Lambda functions – or, more generally, anonymous functions – provide an alternative way to define functions in Python. And the best part is, you can write them in a single line! This is most easily seen via example:
square = lambda num: num * num
The above code is identical to our earlier definition of this function. Here are a few things you should know:
- The
lambda
keyword is analogous to thedef
keyword, letting Python know we want to define a function. - The parameters for lambda functions are to the left of the colon, and the return statement is to the right of the colon (and doesn't actually use the
return
keyword). - We don't have to give the function a name, but if we wish to do so, we can do it using variable assignment.
That last point is key. Using lambda functions lets us define a function and call it or pass it in as an argument to another function without ever having to give it a name. Let's illustrate this by returning to our previous example and making it concrete.
Let's imagine we have the DataFrame my_df
below, containing three people's salaries:
Name Salary
0 John 45000
1 Mary 60000
2 Julie 100000
In this ridiculously idealistic world, employers have just announced that everyone's salaries will be squared. We can update our DataFrame in a single line by using a lambda function:
>>> my_df['Salary'] = my_df['Salary'].apply(lambda num: num * num)
>>> my_df
Name Salary
0 John 2025000000
1 Mary 3600000000
2 Julie 10000000000
And voila – endless riches! Perhaps a bit dramatic, but hopefully you will now remember what lambda functions are and how to use them. If you would like a more detailed discussion on their nuances and subtleties, I recommend checking out my two-part article series that discusses lambda functions in great detail.
In this article, it's time to move to the next topic.
The List Comprehension
If you're in the early stages of learning Python, it's unlikely you've heard of the list comprehension, but I dare say you've probably heard of the for loop. You know, that thing that lets you loop over sequences and do with them as you please:
my_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_new_lst = list() # Make an empty list
for item in my_lst:
if item % 2 == 0: # checking if even
my_new_lst.append(item)
# This will give us a new list with only the even numbers
In the above example, we take a list of numbers and keep just the even ones. The code is simple enough – but because this is Python, and we love simplicity – we can make it even more elegant.
How? Enter the list comprehension. A list comprehension is a piece of Python syntax that effectively lets you run a for loop in a single line and put all the resulting elements into a newly constructed list. It's a bit confusing in abstract terms, but becomes easier to see with an example. Here's how you would accomplish the same thing as above using a list comprehension:
my_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_new_lst = [item for item in my_lst if item % 2 == 0]
This code basically just puts a for loop into one line. You still have the for
keyword telling Python to loop through the original list, and the item
at the beginning just tells Python what we want to include in the final list. In this case, we just want to include the original item, but only if it passes the evenness filter.
What's more, you can also apply an additional transformation after filtering. The code below will also square the result before putting it into our final list:
my_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_new_lst = [(item * item) for item in my_lst if item % 2 == 0]
# Final list will be [4, 16, 36, 64, 200]
You can see how list comprehensions can come in handy. If you do choose to use them, here are a few important points to keep in mind:
- The filter will always be applied before the mapping transformation. In the above code, the filter checks for even numbers, and the mapping transformation squares the original items.
- Like any for loop, a list comprehension can be used to iterate over any iterable sequence, not just lists. Why is it called a list comprehension then? Because it always produces a list as its end result.
- Finally, remember that while list comprehensions can generally be used to make your code more concise, they aren't always necessary. A good rule of thumb is to only use them if they actually make your code more readable. Remember that readability isn't necessarily a function of length – it's possible for code that is simple to understand in a traditional for loop to become messy and confusing in a list comprehension.
For more on list comprehensions, you can check out my two-part series on them as well.
Continuing and breaking
Two useful but occasionally overlooked features of the Python Programming language (and other programming languages, as it happens) are the continue
and break
keywords.
Let's start be defining what each of these do – then we'll look at a couple concrete examples and talk about why you might want to use them.
Continuing and breaking are both operations that can be performed from inside of a loop. They are similar in flavor, but fundamentally different in their effects. Continue can be used to immediately stop the current run of a loop and skip to the next run, whereas break is used to leave the loop completely.
As with most programming-related concepts, this is best seen with an example. Study the code snippet below carefully.
Python"># Continue example
>>> for i in range(10):
... if i == 5:
... continue
... print(i)
...
0
1
2
3
4
6
7
8
9
-------------------------------------------------------------------------------
# Break example
>>> for i in range(10):
... if i == 5:
... break
... print(i)
...
0
1
2
3
4
Do you see the difference? When we use continue
, we simply skip over the 5
because Python immediately moves to the next iteration of the loop and thus never sees the print
statement for 5
. Alternatively, when we use break
, Python exits the loop completely, so nothing is printed out at all after we hit 4
.
Also, it's worth noting that while we used a for loop in the example above, both continue
and break
work just as well with while loops.
Here are a couple situations where you might need to use these charming little keywords:
- If you need to loop indefinitely until some condition is reached, it's common practice to use
break
in combination withwhile True:
. Normally,while True:
would result in an infinite loop, but pairing it withbreak
allows you to get around that. - Say you're looping through a set of data that's coming to you in real time (a stream of sorts, if you want to get technical). If you want to ignore some of the data for whatever reason but keep looping over the rest, then
continue
will be a very helpful keyword indeed.
Now then, let's continue
, shall we?
Getting User Input
One could argue that this is not an essential technique in Python, but in programming in general. But since Python is a subset of programming in general, and it's the subset I assume you're learning on account of having made it this far into my article, I think this topic is fair game.
In case you're unfamiliar with this in general, gathering user input is just a fancy programming term for letting whoever is using your program or application interact with it by (usually) typing in their own characters. In Python, this feature is accessed through the input
function. You can try it out yourself in a live interpreter sessions:
>>> input()
Hi I am a person
'Hi I am a person'
There are three things happening here:
- First, I simply called the
input
function - This led to the cursor moving to the next line. At this point, I was able to type in
Hi I am a person
. We know that Python was expecting user input on this line because it didn't error despite this not being valid Python code. We can also see that the interpreter prompt (>>>
) is not present on this line. - The last line simply prints the whatever the user input was back to the terminal. Generally, this won't happen, as you'll probably want to save your user input into a variable so you can use it later.
I know the above example is a bit vague, so here's an example of how you might write some code in an actual program that makes use of this feature. We can usually pass a string into the input
function as well, which will be used to prompt the user to type something in.
# Writing code that delivers a survey
user_response = input("Did you find this product useful? Please answer yes or no")
if user_response == "yes":
print("Excellent! We are happy we could be of service.")
elif user_response == "no":
print("Ah, we're sorry to hear that. We'll do better.")
And there you have it! Now you know how to interact directly with whoever uses your incredible programs. There's just one more thing I should mention: Everything read in via user input is read in as a string. As a result, remember to convert numbers explicitly using int
or float
, lest you end up with some very annoying bugs!
Final Thoughts and Recap
Here's your Python beginner cheat sheet (Part 2):
- Write better Pandas code (or code in general, but especially Pandas code) by making use of one-line lambda functions.
- Get to know the for loop's cousin, the list comprehension. It's a useful, oft-overlooked feature that can optimize your code quality.
- Maximize your loop usage with the help of
break
andcontinue
. - A plethora of programs will require you to use user input, so it's in your best interest to get comfortable with it.
Python is a language rich and full with features and treasures all collectively aimed at making you a better programmer.
So learn it, and learn it well.
Want to excel at Python? Get exclusive, free access to my simple and easy-to-read guides here. Want to read unlimited stories on Medium? Sign up with my referral link below!
References
[1] https://pandas.pydata.org/docs/reference/api/pandas.Series.apply.html