Fundamentals 4 min read

30 Common Python Code Examples

This article presents a curated collection of 30 practical Python code snippets, ranging from basic algorithms like bubble sort and factorial calculation to file system operations, string manipulations, and utility functions such as generating calendars and checking leap years.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
30 Common Python Code Examples

This article provides a list of thirty useful Python code examples that can be saved and referenced for everyday programming tasks.

1. Bubble sort implementation.

2. Method to compute x to the power of n.

3. Compute a*a + b*b + c*c + ….

4. Factorial calculation (n!).

5. List all files and directories in the current folder.

6. Convert all strings in a list to lowercase.

7. Output all files and folders' paths under a given directory.

8. Output all file paths under a directory and its subdirectories.

9. Output all files with the .html suffix in a directory tree.

10. Invert a dictionary's key‑value pairs to create a new dictionary.

11. Print the multiplication table (九九乘法表).

12. Replace all occurrences of 3 with "3a" in a list.

13. Print each name from a list.

14. Merge lists and remove duplicates.

15. Two methods to generate a verification code.

16. Compute the square root of a number.

17. Determine whether a string consists only of digits.

18. Determine if a number is odd or even.

19. Determine whether a year is a leap year.

20. Find the maximum value in a collection.

21. Generate the Fibonacci sequence.

22. Convert decimal numbers to binary, octal, and hexadecimal.

23. Compute the greatest common divisor (GCD).

24. Compute the least common multiple (LCM).

25. Simple calculator implementation.

26. Generate a calendar for a given month.

27. File I/O operations.

28. String type checking.

29. Convert string case (upper/lower).

30. Calculate the number of days in each month and retrieve yesterday's date.

Each example is accompanied by an illustrative image showing the code output, making it a handy reference for Python beginners and intermediate developers.

pythonAlgorithmscode examplesProgramming Basicsutilities
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.