Python program to count files inside a folder

February 23, 2022 A Goodman

This concise article walks you through a complete example of counting the number of files inside a folder. Let’s say we have a project like the following with an index.py file and a photos folder that…

Python: Count words in a string using Regular Expression

February 23, 2022 Guest Contributor

The Python program below counts the number of words in a given string by using a regular expression. Whitespace and any special symbols except the underscore will be ignored. Output: That’s it. Further reading: Python: Calculate…

5 essential List Methods in Python 3

March 26, 2022 A Goodman

List is an extremely important datatype in Python and you will work with it a lot when building programs. It is not necessary to memorize all List methods in Python, but mastering the most popular methods…

How to deep copy a List in Python

March 19, 2022 A Goodman

To copy a list in Python (not just copy its reference), you can use the deepcopy method of the copy module. Example The code: Output: Further reading: Python: Checking System Default Encoding List, Dict, and Set…

Python: Generate a random integer between Min and Max

February 23, 2022 A Goodman

To generate a random integer between two given integers, you can use the randint method of the random module. The Min and Max are both included (that means one of them has a chance to be…

5+ Python For Loop examples

February 23, 2022 A Goodman

A few examples of the For loop in Python. Print all the divisible-by-x numbers from 0 to n a) Print all the divisible-by-3 numbers from 0 to 100: Output: b) Print all the divisible-by-7 numbers from…

Python: Calculate Fibonacci number with 4 lines of code

February 23, 2022 A Goodman

The Fibonacci sequence looks like this: Calculating Fibonacci numbers in Python Reminder: Python accepts function recursion, which means a defined function can call itself. With only 4 lines of Python code, we can write a program…

Python: Sum integers from 1 to N the easiest way

February 23, 2022 Guest Contributor

This’s a short and straight-to-the-point guide to calculating the sum of integers from 1 to a given positive integer (let’s call it “n”) in Python. The code: Output: Further reading: How to Install Python Libraries in…

Check Python versions on Mac

February 23, 2022 A Goodman

To check Python 2 version in macOS, run: Note: Python 2 is pre-installed on Macs. Output (your version may be higher or lower a little bit than mine): To see the exact version of Python 3…

How to run Python 3 with Nodemon on Mac

February 23, 2022 A Goodman

Using nodemon when working with Python will improve your productivity because it can automatically restart your program when file changes in the directory are detected. Before using nodemon, you must have Node.js installed. If you don’t,…

Python if/else/elif examples

February 23, 2022 A Goodman

A few examples of how to use if/else/elif statements in Python. Quick notes: Unlike other programming languages, Python doesn’t use brackets to separate block of code execution statements. Instead, it uses whitespace. The keyword ‘elif’ is…

Python Hello World with VS Code

February 9, 2022 A Goodman

In this short article, you will learn how to write your first lines of Python code and run them in 2 different ways: Using the built-in terminal of the VS Code. Using the Python extension Prerequisites:…

1 2 3 4