RSSAmplifier

Blog

dbader.org

dbader.orgRSS feed ↗32 posts

Latest posts

Interfacing Python and C: The CFFI Module

Interfacing Python and C: The CFFI Module How to use Python’s built-in CFFI module for interfacing Python with native libraries as an alternative to the “ctypes” approach. In previous tutorials , we covered the basics of ctypes and some advanced ctypes usage. This tutorial will cover the CFFI module . CFFI is a richer environment than ctypes, allowing several different options for how you want to…

Write More Pythonic Code by Applying the Things You Already Know

Write More Pythonic Code by Applying the Things You Already Know There’s a mistake I frequently make when I learn new things about Python… Here’s how you can avoid this pitfall and learn something about Python’s “enumerate()” function at the same time. When I learn a new trick for my “Python coding toolbox” I often sense some benefit right away. It’s like I know this thing is useful for something—…

Working With File I/O in Python

Working With File I/O in Python Learn the basics of working with files in Python. How to read from files, how to write data to them, what file seeks are, and why files should be closed. In this tutorial you’ll learn how to work with files using Python. Reading and writing to files in any programming language is an important feature. Without it, all variables and information are stored on volatile…

How to Reverse a String in Python

How to Reverse a String in Python An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks. What’s the best way to reverse a string in Python? Granted, string reversal isn’t used all that often in day-to-day programming, but it’s a popular interviewing question: # You have this:…

Mastering Click: Writing Advanced Python Command-Line Apps

Mastering Click: Writing Advanced Python Command-Line Apps How to improve your existing Click Python CLIs with advanced features like sub-commands, user input, parameter types, contexts, and more. Welcome to the second Click tutorial on how to improve your command-line tools and Python scripts. I’ll show you some more advanced features that help you when things are getting a bit more complex and…

Working with Random Numbers in Python

Working with Random Numbers in Python An overview for working with randomness in Python, using only functionality built into the standard library and CPython itself. Generating Random Floats Between 0.0 and 1.0 The random.random() function returns a random float in the interval [0.0, 1.0). This means the returned random number will always be smaller than the right-hand endpoint (1.0). This is also…

How to Send an Email With Python

How to Send an Email With Python Learn how to send emails using Python code via the built-in “smtplib” module from the standard library. In most applications, you need to communicate with your users using electronic methods. Email is used to send password resets, confirmation of orders, and verification of user accounts. Whatever your reason, the process of emailing is always the same no matter…

Python Tricks: The Book Is Now Available on Kindle

Python Tricks: The Book Is Now Available on Kindle Get the Kindle version of “Python Tricks: A Buffet of Awesome Python Features” and enjoy a smooth reading experience across all of your devices. My couch + a good book on my Kindle == bliss . I can’t help it, it’s my happy place. So whenever I can I spend a few happy hours stretched out on the couch, reading and gulping down coffee. Apparently I’m…

Python Multi-line Comments: Your Two Best Options

Python Multi-line Comments: Your Two Best Options Does Python support multi-line comments the way other languages do? What are your options for writing comment blocks in Python if you need them? Most programming languages have syntax for block comments that span multiple lines of text, like C or Java: /* This is a block comment. It spans multiple lines. Nice, eh? */ int answer = 42 ; How do you…

Writing Python Command-Line Tools With Click

Writing Python Command-Line Tools With Click An in-depth tutorial on writing Python command-line (CLI) apps using the Click library for argument parsing and more. Python is often referred to as a glue code language because it’s extremely flexible and integrates well with existing programs. This means that a large portion of Python code is written as scripts and command-line interfaces (CLI).…

Python’s enumerate() Function Demystified

Python’s enumerate() Function Demystified How and why you should use the built-in enumerate function in Python to write cleaner and more Pythonic loops. Python’s enumerate function is a mythical beast—it’s hard to summarize its purpose and usefulness in a single sentence. And yet, it’s a super useful feature that many beginners and even intermediate Pythonistas are blissfully unaware of.…

Using Python for Mobile Development: Kivy vs BeeWare

Using Python for Mobile Development: Kivy vs BeeWare Mobile application development on Android and iOS wasn’t Python’s strong suit in the past—but things might be changing… What about using Python for mobile app development? Historically, Python didn’t have a strong story when it came to writing mobile GUI applications. In fact, Android and iOS development was pretty much out of the question with…

Memoization in Python: How to Cache Function Results

Memoization in Python: How to Cache Function Results Speed up your Python programs with a powerful, yet convenient, caching technique called “memoization.” In this article, I’m going to introduce you to a convenient way to speed up your Python code called memoization (also sometimes spelled memoisation ): Memoization is a specific type of caching that is used as a software optimization technique.…

Finding Python Projects to Grow Your Programming Skills

Finding Python Projects to Grow Your Programming Skills Where to find inspiration for Python projects that will help you develop real-world coding skills and lay the foundation of your programming portfolio. I got this question from a newsletter reader who’s an entry-level Pythonista: What’s the best way of moving from a basic understanding of Python to working on real projects? And what Python…

Interfacing Python and C: Advanced “ctypes” Features

Interfacing Python and C: Advanced “ctypes” Features Learn advanced patterns for interfacing Python with native libraries, like dealing with C structs from Python and pass-by-value versus pass-by-reference semantics. The built-in ctypes module is a powerful feature in Python, allowing you to use existing libraries in other languages by writting simple wrappers in Python itself. In the first part…

Python Tricks: The Book Launch FAQ

Python Tricks: The Book Launch FAQ I’m getting a ton of emails, Tweets, and YouTube comments from people with questions about my new book. Here’s a quick Q&A to keep you informed and my carpal tunnel happy. Alright, I’m getting a ton (and I mean a ton ) of emails, Tweets, and YouTube comments from people with questions about my new book. (In case you haven’t heard about it, it’s called Python…

Announcing Python Tricks: The Book!

Announcing Python Tricks: The Book! It’s finally here! I’m super excited to announce the release of my book “Python Tricks: A Buffet of Awesome Python Features” It’s been a crazy and awesome couple of weeks to get this 300 page book ready for release. I’ve spent almost a year writing and editing this book and I still can’t believe I finally got it published on Amazon . It all started with an email…

Python Parallel Computing (in 60 Seconds or less)

Python Parallel Computing (in 60 Seconds or less) If your Python programs are slower than you’d like you can often speed them up by parallelizing them. In this short primer you’ll learn the basics of parallel processing in Python 2 and 3. Basically, parallel computing allows you to carry out many calculations at the same time, thus reducing the amount of time it takes to run your program to…

Writing a Domain Specific Language (DSL) in Python

Writing a Domain Specific Language (DSL) in Python Learn how to create your own Domain Specific Language with Python from scratch with this step-by-step tutorial. A Domain Specific Language , or DSL for short, is a language that’s specialized to a particular application domain. In other words, it’s a programming language that’s used for a more specific application or use case than a…

Getting a Job as a Self-Taught Python Developer

Getting a Job as a Self-Taught Python Developer Do you need a university degree to get a coder job? Is a generic Computer Science degree best or are there more specific programs? I got this email with Python career questions from newsletter reader Brad: First, with regards to your Python Tricks book, I thought it was well-written and well-priced. I got good use out of, I’d say, 4 or 5 sections.…

How to Structure Your Python Programs

How to Structure Your Python Programs Learn a simple trick for keeping your Python code organized and maintainable—even as a project grows over time. How should you structure your Python programs? Is there a recommended way to bring “structure to the chaos?” If you’re writing anything that goes beyond a few lines of Python code, how should you lay out your functions and other building blocks?…

I’m bad at math and I’ll never be a real programmer

I’m bad at math and I’ll never be a real programmer Do you have to be good at math to be a good programmer? Should you pursue a computer science career if you’re bad at math? There’s a phase in the life of every coder I call “The Valley of Despair.” It looks something like this: It’s 3am and my eyes hurt. I fought my way through a stack of documentation and books—but when I think about writing a…

Contributing to Python Open-Source Projects

Contributing to Python Open-Source Projects How can you become a contributor on popular, “high-profile” Python open-source projects like Django, Requests, and so on? Contributing to open-source projects is a great way to build your programming skills, take part in the community, and to make a real impact with your code… It can also help you get a job as a professional Python developer, but…

Iterator Chains as Pythonic Data Processing Pipelines

Iterator Chains as Pythonic Data Processing Pipelines Here’s another great feature of iterators in Python: By chaining together multiple iterators you can write highly efficient data processing “pipelines.” If you take advantage of Python’s generator functions and generator expressions, you’ll be building concise and powerful iterator chains in no time. In this tutorial you’ll find out what this…

Extending Python With C Libraries and the “ctypes” Module

Extending Python With C Libraries and the “ctypes” Module An end-to-end tutorial of how to extend your Python programs with libraries written in C, using the built-in “ctypes” module. The built-in ctypes module is a powerful feature in Python, allowing you to use existing libraries in other languages by writting simple wrappers in Python itself. Unfortunately it can be a bit tricky to use. In this…

Generator Expressions in Python: An Introduction

Generator Expressions in Python: An Introduction Generator expressions are a high-performance, memory–efficient generalization of list comprehensions and generators. In this tutorial you’ll learn how to use them from the ground up. In one of my previous tutorials you saw how Python’s generator functions and the yield keyword provide syntactic sugar for writing class-based iterators more easily.…

What Are Python Generators?

What Are Python Generators? Generators are a tricky subject in Python. With this tutorial you’ll make the leap from class-based iterators to using generator functions and the “yield” statement in no time. If you’ve ever implemented a class-based iterator from scratch in Python, you know that this endeavour requires writing quite a bit of boilerplate code. And yet, iterators are so useful in…

Unpacking Nested Data Structures in Python

Unpacking Nested Data Structures in Python A tutorial on Python’s advanced data unpacking features: How to unpack data with the “=” operator and for-loops. Have you ever seen Python’s enumerate function being used like this? for ( i , value ) in enumerate ( values ): ... In Python, you can unpack nested data structures in sophisticated ways, but the syntax might seem complicated: Why does the for…

Python String Conversion 101: Why Every Class Needs a “repr”

Python String Conversion 101: Why Every Class Needs a “repr” How and why to implement Python “to string” conversion in your own classes using Python’s “repr” and “str” mechanisms and associated coding conventions. When you define a custom class in Python and then try to print one of its instances to the console (or inspect it in an interpreter session), you get a relatively unsatisfying result.…

Python Iterators: A Step-By-Step Introduction

Python Iterators: A Step-By-Step Introduction Understanding iterators is a milestone for any serious Pythonista. With this step-by-step tutorial you’ll understanding class-based iterators in Python, completely from scratch. I love how beautiful and clear Python’s syntax is compared to many other programming languages. Let’s take the humble for-in loop, for example. It speaks for Python’s beauty…

How to Install and Uninstall Python Packages Using Pip

How to Install and Uninstall Python Packages Using Pip A step-by-step introduction to basic Python package management skills with the “pip” command. Learn how to install and remove third-party modules from PyPI. Python is approaching its third decade of good old age, and over the years many people have contributed to the creation of Python packages that perform specific functions and operations.…

How to Check if a File Exists in Python

How to Check if a File Exists in Python A tutorial on how to find out whether a file (or directory) exists using Python built-ins and functions from the standard library. The ability to check whether a file exists on disk or not is important for many types of Python programs: Maybe you want to make sure a data file is available before you try to load it, or maybe you want to prevent overwriting an…