# Python

## Diamond Pattern in Python Using For Loop

To create a diamond pattern in Python using a for loop, use this simple piece of code: Then run the program. For example, here is a diamond output of height 7: How Does It Work The diamond pattern using for loop in Python is a common beginner question in Python courses. Make sure you understand […]

## Python Math Module: A Complete Guide (Examples)

If you need anything more complex than the basic arithmetic operators in Python, there’s a built-in library called Math you can use. With this module, you can calculate square roots, medians, distances, and such. This guide teaches you how to start using the math module in Python. To start using the module in your project,

## Python Program to Check Leap Year (Theory & Example Use)

A leap year is a year that is divisible by 4. The exception is the years that are divisible by 100 but not by 400. This comprehensive guide teaches you how to check if a year is leap year in Python. You will also learn how to find the next leap year given the current

## Python If-Else on One Line

In Python, you can have if-else statements on one line. To write an if-else statement on one line, follow the conditional expression syntax: For example: This is handy with short if-else statements because it allows you to save lines of code while preserving code quality. But do not overuse it. Turning longer if-else statements into

## Decorators in Python: A Complete Guide (with Examples)

Python decorators provide a readable way to extend the behavior of a function, method, or class. Decorating a function in Python follows this syntax: Here the guard_zero decorator updates the behavior of divide() function to make sure y is not 0 when dividing. How to Use Decorators in Python The best way to demonstrate using

## Str() vs repr() in Python

In Python, the built-in str() and repr() functions both produce a textual representation of an object. Output: The difference between str() and repr() is: The str() function returns a user-friendly description of an object. The repr() method returns a developer-friendly string representation of an object. In this guide, you learn: What are the functions str()

Leaving code comments might sometimes help clarify your code. There are 3 unofficial comment types in Python. A “block comment”: An “inline comment”: A “multi-line” comment that misuses a docstring: Commenting Code in Python To add a comment in Python, use the hashtag operator # before starting to type the comment. Before digging into the

## How to Read a Text File in Python

To read a text file in Python: For example, let’s read a text file called example.txt from the same folder of the code file: Now the lines variable stores all the lines from example.txt as a list of strings. This is the quick answer. To learn more details about reading files in Python, please read

## How to Write to a Text File in Python

In Python, you can write to a text file by following these three steps: You can skip the last step by using the with statement. For example: This piece of code creates a new file called example.txt and writes “Hello World” into it. In this guide, you are going to learn the basics of writing

## Python Pretty Print JSON

To pretty-print JSON in Python: For example: Where: Here is an illustration of what pretty printing does to JSON objects: In this guide, you learn why JSON is usually unreadable and how to use pretty printing to fix the problem. You will also see some common examples when pretty-printing. Before learning how to pretty-print JSON,