Python’s exec(): Execute Dynamically Generated Code

Python’s built-in exec() function allows you to execute arbitrary Python code from a string or compiled code input. The exec() function can be handy when you need to run dynamically generated Python code, but it can be pretty dangerous if you use it carelessly. In this tutorial, you’ll learn not only how to use exec(), but just as importantly, when it’s okay to use this function in your code. In this tutorial, you’ll learn how to: Work with Python’s built-in […]

Read more

How to Check if a Python String Contains a Substring

If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains another string in Python. Identifying such substrings comes in handy when you’re working with text content from a file or after you’ve received user input. You may want to perform different actions in your program depending on whether a substring is present or not. In this tutorial, you’ll focus on the most […]

Read more

Invasive procedures: Python affordances for performance measurement

When your Python code is too slow, you need to identify the bottleneck that’s causing it: you need to understand what your code is doing. Luckily, beyond pre-existing profiling tools, there are also a variety of ways you can poke and prod Python programs to get a better understanding of what they’re doing internally. This allows you to do one-time introspection, add profiling facilities to your program that you can turn on and off, build custom tools, and in general […]

Read more

How to Find an Absolute Value in Python

Absolute values are commonly used in mathematics, physics, and engineering. Although the school definition of an absolute value might seem straightforward, you can actually look at the concept from many different angles. If you intend to work with absolute values in Python, then you’ve come to the right place. In this tutorial, you’ll learn how to: Implement the absolute value function from scratch Use the built-in abs() function in Python Calculate the absolute values of numbers Call abs() on NumPy […]

Read more

Caching in Python With lru_cache

There are many ways to achieve fast and responsive applications. Caching is one approach that, when used correctly, makes things much faster while decreasing the load on computing resources. Python’s functools module comes with the @lru_cache decorator, which gives you the ability to cache the result of your functions using the Least Recently Used (LRU) strategy. This is a simple yet powerful technique that you can use to leverage the power of caching in your code. In this video course, […]

Read more

GitHub Copilot: Fly With Python at the Speed of Thought

GitHub Copilot is a thrilling new technology that promises to deliver to your code editor a virtual assistant powered by artificial intelligence, and it stirred up considerable controversy when it was released to the general public. Python is among the languages that are particularly well-supported by this tool. After reading this tutorial, you’ll know whether GitHub Copilot is a risk, a gimmick, or a true game changer in software engineering. To continue with this tutorial, you need to have a […]

Read more

Finding performance problems: profiling or logging?

When your software is too slow in production, how can you figure out the source of the problem? One common starting point to improving production observability is logging, and ideally trace-based logging (tracing for short). For example, the OpenTelemetry standard and the libraries and backend services that work with it can help you collect metrics, logs, and traces. Tracing—both within and across processes—is the most general of these, immensely useful for identifying and debugging problems, including performance problems. But there’s […]

Read more

Sorting a Python Dictionary: Values, Keys, and More

# samples.py dictionary_of_dictionaries = { 1: {“first_name”: “Dorthea”, “last_name”: “Emmanuele”, “age”: 29}, 2: {“first_name”: “Evelina”, “last_name”: “Ferras”, “age”: 91}, 3: {“first_name”: “Frederica”, “last_name”: “Livesay”, “age”: 99}, 4: {“first_name”: “Murray”, “last_name”: “Linning”, “age”: 36}, 5: {“first_name”: “Annette”, “last_name”: “Garioch”, “age”: 93},

Read more

Exploring Special Function Parameters

Have you ever come across the forward slash (/) and asterisk (*) symbols in the documentation for your favorite libraries? These represent special parameters, which tell you what kinds of arguments you can use to call a given function. In Python, these parameters can help you ensure that your functions are used correctly. Maybe you’re a regular at Real Python’s weekly Office Hours meetup, or perhaps you’re curious about what happens there. You’ll get a glimpse in this Code Conversation, […]

Read more

Python News: What’s New From July 2022

In July 2022, Python reached for the stars, playing a key role in processing data from the James Webb Space Telescope. After two years of virtual conferences, EuroPython 2022 took place in Dublin, Ireland. Anaconda celebrated its tenth birthday, and Flask achieved a major milestone on GitHub. Two new pre-release versions of Python 3.11 were released, with 3.11.0b5 representing the final beta version. Meanwhile, the Python Package Index (PyPI) introduced a two-factor authentication requirement for maintainers of critical projects. Finally, […]

Read more
1 145 146 147 148 149 908