Python tutorials

Using portable SIMD in stable Rust

In a previous post we saw that you can speed up code significantly on a single core using SIMD: Single Instruction Multiple Data. These specialized CPU instructions allow you to, for example, add 4 values at once with a single instruction, instead of the usual one value at a time. The performance improvement you get compounds with multi-core parallelism: you can benefit from both SIMD and threading at the same time. Unfortunately, SIMD instructions are specific both to CPU architecture […]

Read more

Python Dictionary Comprehensions: How and When to Use Them

Dictionary comprehensions are a concise and quick way to create, transform, and filter dictionaries in Python. They can significantly enhance your code’s conciseness and readability compared to using regular for loops to process your dictionaries. Understanding dictionary comprehensions is crucial for you as a Python developer because they’re a Pythonic tool for dictionary manipulation and can be a valuable addition to your programming toolkit. In this tutorial, you’ll learn how to: Create dictionaries using dictionary comprehensions Transform existing dictionaries with […]

Read more

Formatting Floats Inside Python F-Strings

You’ll often need to format and round a Python float to display the results of your calculations neatly within strings. In earlier versions of Python, this was a messy thing to do because you needed to round your numbers first and then use either string concatenation or the old string formatting technique to do this for you. Since Python 3.6, the literal string interpolation, more commonly known as a formatted string literal or f-string, allows you to customize the content […]

Read more

Python News Roundup: November 2024

The latest Python developments all point to the same thing—Python is currently thriving. The recent GitHub Octoverse 2024 report has revealed that Python is now the most used language on GitHub. Also, last month saw the release of Python 3.13, which is already laying the groundwork for some exciting future improvements. While Python core developers have been busy exploring the language’s features as they tinker with upcoming enhancements, it’s good to know that working on Python’s source code isn’t the […]

Read more

How to Reset a pandas DataFrame Index

In this tutorial, you’ll learn how to reset a pandas DataFrame index, the reasons why you might want to do this, and the problems that could occur if you don’t. Before you start your learning journey, you should familiarize yourself with how to create a pandas DataFrame. Knowing the difference between a DataFrame and a pandas Series will also prove useful to you. In addition, you may want to use the data analysis tool Jupyter Notebook as you work through […]

Read more

Quiz: Variables in Python: Usage and Best Practices

Interactive Quiz ⋅ 16 QuestionsBy Leodanis Pozo Ramos Share In this quiz, you’ll test your understanding of Variables in Python: Usage and Best Practices. By working through this quiz, you’ll revisit how to create and assign values to variables, change a variable’s data type dynamically, use variables to create expressions, counters, accumulators, and Boolean flags, follow best practices for naming variables, and create, access, and use variables in their scopes. The quiz contains 16 questions and there is no time […]

Read more

Variables in Python: Usage and Best Practices

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: Variables in Python In Python, variables are symbolic names that refer to objects or values stored in your computer’s memory. They allow you to assign descriptive names to data, making it easier to manipulate and reuse values throughout your code. Understanding variables is key for Python developers because variables are essential building blocks for […]

Read more

The Python Square Root Function

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: The Square Root Function in Python The Python square root function, sqrt(), is part of the math module and is used to calculate the square root of a given number. To use it, you import the math module and call math.sqrt() with a non-negative number as an argument. For example, math.sqrt(9) returns 3.0. This […]

Read more

Quiz: Using .__repr__() vs .__str__() in Python

Interactive Quiz ⋅ 6 QuestionsBy Martin Breuss Share In this quiz, you’ll test your understanding of Python’s .__repr__() and .__str__() special methods. These methods allow you to control how a program displays an object, making your classes more readable and easier to debug and maintain. The quiz contains 6 questions and there is no time limit. You’ll get 1 point for each correct answer. At the end of the quiz, you’ll receive a total score. The maximum score is 100%. […]

Read more

Python Closures: Common Use Cases and Examples

In Python, a closure is typically a function defined inside another function. This inner function grabs the objects defined in its enclosing scope and associates them with the inner function object itself. The resulting combination is called a closure. Closures are a common feature in functional programming languages. In Python, closures can be pretty useful because they allow you to create function-based decorators, which are powerful tools. In this tutorial, you’ll: Learn what closures are and how they work in […]

Read more
1 2 3 4 181