Python tutorials

Effective Python Testing With Pytest

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Testing Your Code With pytest Testing your code brings a wide variety of benefits. It increases your confidence that the code behaves as you expect and ensures that changes to your code won’t cause regressions. Writing and maintaining tests is hard work, so you should leverage all the tools at your disposal to make […]

Read more

Why new Macs break your Docker build, and how to fix it

One of the promises of Docker is reproducibility: you can build an image on a different machine, and assuming you’ve done the appropriate setup, get the same result. So it can be a little confusing when you try to build your Python-based Dockerfile on a new Mac, and everything starts failing. What used to work before—on an older Mac, or on a Linux machine—fails in completely unexpected ways. The problem is that the promise of reproducibility relies on certain invariants […]

Read more

Python mmap: Doing File I/O With Memory Mapping

The Zen of Python has a lot of wisdom to offer. One especially useful idea is that “There should be one—and preferably only one—obvious way to do it.” Yet there are multiple ways to do most things in Python, and often for good reason. For example, there are multiple ways to read a file in Python, including the rarely used mmap module. Python’s mmap provides memory-mapped file input and output (I/O). It allows you to take advantage of lower-level operating […]

Read more

How Can You Emulate Do-While Loops in Python?

If you came to Python from a language like C, C++, Java, or JavaScript, then you may be missing their do-while loop construct. A do-while loop is a common control flow statement that executes its code block at least once, regardless of whether the loop condition is true or false. This behavior relies on the fact that the loop condition is evaluated at the end of each iteration. So, the first iteration always runs. One of the most common use […]

Read more

Build Your Python Project Documentation With MkDocs

In this tutorial, you’ll learn how to quickly build documentation for a Python package using MkDocs and mkdocstrings. These tools allow you to generate nice-looking and modern documentation from Markdown files and your code’s docstrings. Maintaining auto-generated documentation means less effort because you’re linking information between your code and the documentation pages. However, good documentation is more than just the technical description pulled from your code! Your project will appeal more to users if you guide them through examples and […]

Read more

Combining Data in pandas With concat() and merge()

The Series and DataFrame objects in pandas are powerful tools for exploring and analyzing data. Part of their power comes from a multifaceted approach to combining separate datasets. With pandas, you can merge and concatenate your datasets, allowing you to unify and better understand your data as you analyze it. In this video course, you’ll learn how and when to combine your data in pandas with: merge() for combining data on common columns or indices concat() for combining DataFrames across […]

Read more

The subprocess Module: Wrapping Programs With Python

If you’ve ever wanted to simplify your command-line scripting or use Python alongside command-line applications—or any applications for that matter—then the Python subprocess module can help. From running shell commands and command-line applications to launching GUI applications, the Python subprocess module can help. By the end of this tutorial, you’ll be able to: Understand how the Python subprocess module interacts with the operating system Issue shell commands like ls or dir Feed input into a process and use its output. […]

Read more

Pandas vectorization: faster code, slower code, bloated memory

When you’re processing data with Pandas, so-called “vectorized” operations can significantly speed up your code. Or at least, that’s the theory. In practice, in some situations Pandas vectorized operations can actually make your code slower, or at least no faster. And they can also significantly increase memory usage. Let’s dig in and see what vectorization means in Pandas, when and why it helps, and when it’s harmful. Vectorization: what it means, and how it speeds up your code Vectorization can […]

Read more

A First Look at PyScript: Python in the Web Browser

PyScript is a brand-new framework that caused a lot of excitement when Peter Wang, the CEO and co-founder of Anaconda, Inc., revealed it during his keynote speech at PyCon US 2022. Although this project is just an experiment in an early phase of development, people on social media seem to have already fallen in love with it. This tutorial will get you up to speed with PyScript, while the official documentation is still in the making. In this tutorial, you’ll […]

Read more

Write and Test a Python Function: Interview Practice

Whether you’re looking to ace your coding interview or simply to level up your development skills, solving coding challenges can help you grow as a programmer. In this Real Python Code Conversation, Philipp challenges Martin to write a function that doubles each character in a string. Through their conversation, you’ll learn how to tackle a coding challenge and stand out from the pack in your job search. In this video course, you’ll learn how to: Use functions to write reusable, […]

Read more
1 93 94 95 96 97 185