Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Writing Idiomatic Python (Summary)

In this course, you covered how to write more Pythonic code.

Whether you’re coming from another programming language or Python is your first, you should now have a foundation for understanding and applying idiomatic practices within Python.

In this course, you covered:

  • How to access and interpret The Zen of Python
  • How to set up a script
  • How to test truth values
  • How to swap variables in-place
  • How to create Pythonic for loops

For more articles and courses on this topic, check out How to Write Pythonic Loops, Writing Beautiful Pythonic Code With PEP 8, and other resources in the Write More Pythonic Code Learning Path.

Download

Sample Code (.zip)

2.7 KB

00:00 You’ve come to the end of this short course that is an introduction to writing idiomatic Python. You learned a couple of things in here. First, you learned about The Zen of Python and how you can access this little poem by just importing the this module in your Python interpreter.

00:16 Then you learned about how to set up your scripts using the shebang and also how you’re supposed to put statements in Python on separate lines. Then you learned about truth value testing and how it’s often a good idea to test for truthiness or falsiness of a value rather than for the exact Boolean values True and False.

00:35 Then you learned about using built-in functions and methods in Python, that this is often a good way of doing exactly what you want to do in a very concise way.

00:44 You looked at an example of doing that for string concatenation of string items that are inside of a list. Then you learned about how to swap variables in-place and how you can do that in Python using tuple unpacking in just a one-liner as opposed to needing to use a temp variable as you do in a lot of other languages.

01:05 Then you learned about how you can use the .get() method of dictionaries to provide default values, which gives you a quick way of just going through a dictionary and not having to worry about whether the key is present or not.

01:17 You learned about the Don’t Repeat Yourself principle and how you can apply it in Python with an example of creating a list and then checking whether an item is inside of the list rather than doing individual checks for each of the items.

01:31 And finally, you learned about writing idiomatic for loops and how you can do a good for loop that checks for each item in a list without needing to work with length and indices as you need in many other languages.

01:45 I hope this was insightful for you. There’s a lot more to learn about how to write Pythonic code, and there will be a couple of links in the description that you can check out if you want to learn more. But this gives you a good start, and I would just suggest that you go ahead and read Python and write Python and just keep going. All right, hope you enjoyed that!

02:02 My name is Martin, and I hope to see you around on Real Python.

alexisjawtu on Nov. 14, 2021

I disagree with the uniform recommendation of using always the highest level.

Idioms imply a potentially huge danger, to leave us as mere client developers (like Bruce Eckel said) for ever.

A computer scientist should be able to research for a certain algorithm to solve a problem. Would Project Euler by any fun if we solved the problems by invoking the precise module just written by that lucky guy who made it to a core developer?

anaghost on Nov. 14, 2021

nice lessons, thank you! And I really appreciate you using the dark background, so much easier on the eyes!

Martin Breuss RP Team on Nov. 15, 2021

Glad you liked it @anaghost! :)

@alexisjawtu definitely a good point, and it makes a difference to know the underlying principles, especially if you approach programming from a CS perspective. So a blanket recommendation to always use the highest-level is reaching too far. It can be a good rule-of-thumb, however, when you want to more quickly become productive working with Python.

Some Thoughts on Learning

There are many use cases and levels of experience that you can have when getting into programming, and one aspect of higher-level languages and abstraction in general is that it allows you to solve practical problems without needing to know (or implement) the logic that makes this possible.

It’s a trade-off and I can understand the reasoning either way. But since I’m an educator, and convinced that practical programming is a super useful skill that can help many people to be more productive in their jobs and lifes—and therefore many people would benefit from knowing at least a little bit about it—I think it’s helpful if learners know that they can be productive with a programming language such as Python without needing to be a computer scientist and understanding all the underlying algorithms that they are using.

Understanding the underlying fundamentals could be a next-step for learners who’ve managed to solve practical problems with programming and want to dig deeper (following the learning principles described by David Perkins), but also, it doesn’t need to be.

For many people and situations it can be fine to use programming as a tool that you don’t fully understand (whatever “fully understanding” even means—since you can always go deeper!).

I personally think there’s great value in getting more people involved in the discipline, even if they have different backgrounds and learning approaches that don’t entirely align with the bottom-up and algorithm-focused approaches that are still often taught at CS departments.

Become a Member to join the conversation.