Skip to main content

35 docs tagged with "beginners"

View all tags

*args and **kwargs: Accepting a Variable Number of Arguments

We've learned how to define functions with a fixed number of positional and keyword arguments. But what if you want to create a function that can accept any number of arguments? For example, a function that can sum two numbers, or five, or a hundred. This is where args and *kwargs come in, providing the ultimate flexibility for your function definitions.

A Simple OOP Project: Modeling a Real-World Entity

Theory is important, but the best way to solidify your understanding of Object-Oriented Programming is to build something. In this article, we will put together all the concepts we've learned in this series—classes, objects, attributes, methods, and encapsulation—to model a real-world entity from scratch.

Basic Web Concepts: HTTP, URLs, and the Request/Response Cycle

Welcome to a new chapter in your Python journey! We're moving from writing command-line scripts and applications to the exciting world of web development. Before we can build web applications with Python frameworks like Flask or Django, we must first understand the fundamental concepts that power the entire internet.

Core OOP Concepts: Classes and Objects

Welcome to a new chapter in your Python journey! So far, we've been using a style of programming called procedural programming. We write procedures (functions) that perform operations on data. This is great, but as programs become larger and more complex, it can be hard to manage the relationship between the data and the functions that operate on it.

Creating Your Own Modules: Structuring Your Projects for Reusability

We've learned what modules are and how to import them. Now it's time to take the next logical step: creating your own. This is a fundamental skill for any Python developer. By creating your own modules, you can break down complex problems into logical, reusable pieces, making your code cleaner, easier to debug, and more professional.

Default Arguments and Keyword Arguments

We've learned how to define functions, pass information to them, and get values back using the [return statement]. Now, let's make our functions more flexible and readable. This article explores two powerful features: default arguments and keyword arguments, which allow you to make function parameters optional and your function calls more explicit.

Defining Classes: Attributes and Methods

In the last article, we learned that a class is a blueprint and an object is the instance we create from it. We used the init() method to set up the initial data, or attributes, of an object.

Defining Functions: `def` Keyword, Parameters, and Arguments

In our previous article, we introduced [Introduction to Functions: The DRY principle and code reusability]. We learned that functions are essential for writing clean, reusable, and maintainable code. This article dives deeper into the mechanics of creating functions, focusing on the def keyword and the crucial distinction between parameters and arguments.

Docstrings and Type Hinting: Documenting Your Functions

Writing code that works is only half the battle. Writing code that you and others can understand months from now is just as important. This is where documentation comes in. In this article, we'll explore two powerful, related concepts for documenting your Python functions: docstrings and type hints. Mastering them will elevate the quality and professionalism of your code.

Function Return Values: The `return` Statement

In our last article, we learned how to define functions and pass information to them using [parameters and arguments]. So far, our functions have only performed actions, like printing to the console. But what if we need a function to give us a result back? This is where the return statement comes in, and it's a game-changer for writing powerful, modular code.

Handling Exceptions: The `else` and `finally` Blocks

In the last article, we introduced the try...except block as the fundamental way to handle runtime errors in Python. It allows our programs to react to problems gracefully instead of crashing. However, the story doesn't end there. The full try statement includes two more optional but powerful clauses: else and finally.

Instance, Class, and Static Methods

We've learned how to define methods—functions inside a class—that give our objects behavior. So far, all the methods we've created have been instance methods, which operate on a specific instance of a class.

Introduction to Exception Handling: `try` and `except` Blocks

Up to this point, our scripts have followed a "happy path." We assume the user will enter the correct type of data, the files we want to read will always exist, and we'll never divide by zero. But in the real world, things go wrong. Network connections fail, files are moved, and users enter invalid input.

Introduction to Functions: The DRY Principle and Code Reusability

Welcome to a new chapter in your Python journey! We've spent a great deal of time understanding the fundamental building blocks of Python—variables, data types, and control flow. Now, it's time to learn how to organize our code into reusable, efficient, and readable blocks. This article introduces one of the most important concepts in programming: functions.

Introduction to Packages: Organizing Modules into Directories

We've successfully organized our code into reusable modules. But what happens when your project grows even larger and you have dozens of modules? Throwing them all into the same directory becomes just as messy as having one giant file. The next level of organization in Python is the package. A package is simply a way to structure your project's modules into a directory hierarchy.

Introduction to Pip: The Python Package Manager

So far, we've worked with our own modules and those from Python's extensive Standard Library. But the true power of the Python ecosystem comes from the millions of third-party packages created by the community. To access this vast universe of code, you need a package manager. For Python, that tool is pip.

Introduction to Web Frameworks: WSGI and ASGI

In the last article, we learned about the request/response cycle that powers the web. A client sends an HTTP request, and a server sends back an HTTP response. But how does a web server (like Apache or Nginx) communicate with a Python script?

Raising Exceptions: The `raise` Keyword

We have learned how to handle exceptions that Python raises automatically, like ValueError or FileNotFoundError. But what if you encounter an error condition in your own code that isn't covered by a built-in exception?

Reading Text Files: `read()`, `readline()`, and `readlines()`

In the last article, we learned how to open files using different modes. Now, let's focus on the primary reason you'd open a file in read mode ('r'): to get data out of it. Python's file objects provide several methods for reading content, each suited for different situations.

Request and Response Objects in Flask

We now know how to route a URL to a specific view function. But web development is a two-way street. Our application needs to be able to receive data from the user and send back more than just a simple string of HTML.

Routing in Flask: Variable Rules and URL Building

We've successfully created a simple Flask application that can serve a response from a single URL. But a real website has many pages: an about page, a contact page, user profiles, and so on. The system that maps URLs to the Python functions that handle them is called routing.

Scope: Local, Enclosing, Global, and Built-in (LEGB Rule)

We've been working with variables and functions, but we haven't yet discussed a crucial concept: scope. Scope determines the visibility and accessibility of a variable. In other words, it answers the question, "Where in my program can I access this variable?" Understanding scope is essential for writing bug-free code and avoiding common errors where variables seem to "disappear" or have unexpected values.

Templates in Flask (Part 1): Template Inheritance

We've learned how to use Flask's render_template() function to serve HTML files and pass variables to them. This is great, but as you build a website with multiple pages, you'll notice that a lot of your HTML is repetitive. The navigation bar, the footer, the `` section with all your CSS links—these are the same on every page.

Templates in Flask (Part 2): Control Structures and Filters

We've learned how to render templates and pass variables from our Flask application to our HTML. Now, let's unlock the true power of the Jinja2 templating engine by exploring two of its most important features: control structures and filters.

The `import` Statement: Bringing Code into Your Namespace

In our last article, we learned how to organize code into separate files called [modules]. Now, we need to master the tool that makes modules useful: the import statement. This statement is the gateway to accessing code from other files and from Python's vast standard library, allowing you to build powerful applications without reinventing the wheel.

Understanding Modules: Organizing Code into Separate Files

So far, we've been writing all our code in a single Python file. This is fine for small scripts, but as your projects grow, it becomes messy, hard to navigate, and difficult to maintain. The solution is to break your code into multiple files, and in Python, these files are called modules. This article is the first step toward organizing your code like a professional developer.

What is Flask? A Micro Web Framework

Now that we understand the basic concepts of how the web works, it's time to choose a tool to help us build web applications in Python. There are many choices, but one of the most popular, especially for beginners and for building smaller applications and APIs, is Flask.

Working with File Paths: The `os.path` Module

When we work with files, we often hardcode the file path as a simple string, like 'myfolder/myfile.txt'. This works, but it's not robust. What happens if your script is run on a different operating system? Windows uses a backslash (\) as a path separator, while macOS and Linux use a forward slash (/). Hardcoding paths with one type of slash can make your script fail on other systems.

Working with Files: The `open()` Function and File Modes

So far, our Python programs have been self-contained. They run, perform calculations, and when they finish, any data they generated is gone. To create persistent programs that can save data and read it back later, we need to interact with files.

Writing Text Files: `write()` and `writelines()`

We've mastered the art of reading from files. Now it's time to look at the other side of the coin: writing data to files. This is how you can save program state, log events, generate reports, or create any text-based output you need.

Your First Flask App (Part 2): Templates and Jinja2

In the last article, we created our first Flask application, but our view function returned a simple string of HTML. This is not practical for building real websites. A real website has complex HTML, and we need a way to keep our Python logic separate from the presentation markup.

Your First Flask App: 'Hello, Web!' (Part 1)

With our environment set up, it's time to write our very first web application with Flask. The traditional starting point for any new programming endeavor is the "Hello, World!" application. We'll create a simple web server that, when accessed, returns that classic phrase to the user's browser.