Python Tips and Tricks — Write Better Python Code

Learn how to write better Python code by understanding how to go beyond the basics. This tutorial covers five different Tips and Tricks namly Filter, Map, String Concatenation, List Comprehension and List iteration. In case you’re new to Python Programming and need a formal introduction to the language, we have made a full Python course on Youtube: https://youtu.be/L3v5tu_ang4.

The first two Tips and Tricks is also explained further in the following video on YouTube: https://youtu.be/UCRw77vRHnw

For more see Vinsleov Academy official Subreddit: https://www.reddit.com/r/vinsloevAcademy/

View our newest medium story:

https://vinsloev.medium.com/getting-into-cybersecurity-in-2023-bb16378c6f36

Map

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns an iterator. In the example below our function numFunc(x) divide each element in our data list by two and return the result. This is way more efficient than making a loop to iterate over each element.

def numFunc(x):
return x/2

data = [1, 10, 20, 25]

list(map(numFunc, data))
Output => [0.5, 5.0, 10.0, 12.5]

Filter

The filter() function extracts elements from an iterable (list, tuple etc.) for which a function returns True. Again this can be used instead of looping over each element applying the condition.

def numLessThan(x):
return x < 10

data = [1, 10, 20, 25]

list(filter(numLessThan, data))
Output => [1]

For more stories by Vinsloev Academy, sign up as a member and support our work: https://vinsloev.medium.com/membership

Now that we have seen the first two examples using standard functions, it’s time to dive into the last three which can really help you improve your Python Programming skills. All three is further explained in: https://youtu.be/DC0UfcHDEbs

Loop Iteration

When having a iterable element e.g a List you can assign naming variables to each element for later reference use. This makes your code more readable and ensures that you dont have to reference to a given position using it’s index value. In the example below the naming variables name, lastname and country is applied to the user1 list. So when in need of e.g name and lastname we can just tell python to provide us those instead of saying user1[0] and user1[1].

user1 = ['Megan', 'Hansen', 'Sweden']
name, lastname, country = user1

people = [user1, ['Thomas', 'Hansen', 'Germany']]

for (name, lastname, country) in people:
print(name, lastname)
Output => Megan Hansen, Thomas Hansen

String Concatenation

When doing string concatenation you might be used to loop over each element in e.g a list and then concatenate is to a given variable and its previous value using +=, like illustrated below:

user = ['Megan', 'Hansen', 'Sweden']

result = ''
for item in user:
result += item

print(result)

However you can obtain the same result using the join() function and thereby remove the need for a loop making your code both cleaner and more efficient.

result1 = ''.join(user)

print(result1)

String Comprehension

When in need of a subset from a given list for appending on a new/another list, again a for loop along with a if statement, might be the go-to choice like written below. However the same result can be achieved using less code and written as a one liner.

def numLessThan(x):
return x < 10

oList = [7, 10, 20, 5, 4, 80]
nList = []

for item in oList:
if numLessThan(item):
nList.append(item)
print(nList)

The following line of code achieve the same result with less amount of code, even tough the for loop and the if condition is preserved.

nList = [(item) for item in oList if numLessThan(item)]
print(nList)

For more Python Tips and Tricks like this visit the Vinsloev Academy page here on Medium or on YouTube, as this story is part of a larger series of build it yourself Python tools for Ethical hacking: https://www.youtube.com/channel/UC-OKxBgjKLBGHbueyIOWptw?sub_confirmation=1

Book — Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers: https://amzn.to/3RWAjyY

--

--

At Vinsloev Academy we strive to change the world by making software development skills available to all who wish to learn and evolve.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinsloev

At Vinsloev Academy we strive to change the world by making software development skills available to all who wish to learn and evolve.