Seven Tips To Clean Code With Python

Seven Tips To Clean Code With Python

Here are the seven tips and code bites that I use every day in my work as a data scientist.

In this story, I will share what I use in my day-to-day work and what has helped me improve my code. Check the list below to see if there’s anything new for you!

  • String formatting with F-strings
  • Platform independent directory delimiters
  • Variable unpacking and the _ operator
  • .get instead of [key] for dictionary iterations
  • Loop two iterators with the zip function
  • The power of list comprehensions
  • Multiple assignment with * and **

String formatting with f-strings

Hallelujah! That is what I thought when I learned about the Python 3.6+ update that includes a new way of formatting strings: the Python formatted string literal. String formatting in Python has come a long way.

purpose = "string formatting"
percent_sign = "% operator"
dot_format_method = ".format() method"
string_literal = "f-string"
quantity, revenue = 100, 25
d = {"costs": 300}
list_operations = ['slicing and other things', 'indexing', 'key references']
limitations = ["be empty", "contain comments", "contain backslashes"]

print("Before w used the %s for %s." % (percent_sign, purpose))
>>> "Before w used the % operator for string formatting."

print("Since Python 2.7 we are using the {method} for {0}, enabling us {1:^8s}.".format(purpose, "to do fun things", method=dot_format_method))
>>> "Since Python 2.7 we are using the .format() method for string formatting, enabling us to do fun things."

print(f"But now with Python 3.6 come the {string_literal}!")
>>> "But now with Python 3.6 come the f-string!"

print(f"With f-strings we can do arithmetic expressions: price €{quantity / revenue} per unit.")
>>> "With f-strings we can do arithmetic expressions: price €4.0 per unit."

print(f"We can do {list_operations[0][0:7]}, {list_operations[1]} and {list_operations[2]}: cost €{d['costs'] / quantity} per unit.")
>>> "We can do slicing, indexing and key references: cost €3.0 per unit."

print(f"f-strings have three limitations: {*limitations,}.")
>>> "f-strings have three limitations: ('be empty', 'contain comments', 'contain backslashes')."

programming data-science python clean-code

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Data Science With Python Training | Python Data Science Course | Intellipaat

🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...

Data Science with Python Certification Training in Chennai

Enroll in our Data Science with Python training in Chennai. Best Data Science with Python Training courses in Chennai for 100% Job Placements Support.

Python for Data Science | Data Science With Python | Python Data Science Tutorial

🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...

Applied Data Science with Python Certification Training Course -IgmGuru

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

Data Cleaning in R for Data Science

A data scientist/analyst in the making needs to format and clean data before being able to perform any kind of exploratory data analysis.