In this article we want to talk about 5 Best Python Libraries For Fun, Python is high level and interpreted programming language used for different type of tasks such as web development, data analysis, artificial intelligence and scientific computing. it is known for its simplicity, readability, and versatility. Python was first released in 1991 and has since become one of the most popular programming languages in the world.
5 Best Python Libraries For Fun
- Pygame – Game development library
- Matplotlib – Data visualization library
- Turtle – Easy-to-use graphics library
- PyAutoGUI – Automate GUI tasks library
- Pillow – a library for processing images and performing tasks such as cropping, resizing and adding filters.
What is Pygame ?
Pygame is free and open source Python library used for making video games. it provides modules for you to use for creating 2D graphics, playing sounds and music, handling user input, and other features that are commonly used in game development. Pygame is designed to be easy to use and it allows you quickly create games without having to deal with the lowlevel details of graphics and sound. to install Pygame, you can use the following command in your terminal or command prompt:
1 |
pip install pygame |
Also if you’re using Anaconda, you can use the following command in your Anaconda prompt:
1 |
conda install pygame |
Note: make sure you have the latest version of pip or conda installed.
This is basic example of Pygame to display a window with a red background:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import pygame # initialize Pygam pygame.init() # set the window siz window_size = (400, 300) # create the window screen = pygame.display.set_mode(window_size) # fill the window with red red = pygame.Color(255, 0, 0) screen.fill(red) # update the window pygame.display.update() # wait for the user to close the window running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # quit Pygame pygame.quit() |
This code will display a window with a red background and wait for the user to close it.
Run the complete code and this will be the result
What is Matplotlib ?
Matplotlib is plotting library for the Python programming language. it provides functions for creating static, animated and interactive visualizations in 2D and 3D. Matplotlib is widely used for data visualization, exploration and analysis, and supports different graph types, including line plots, scatter plots, bar plots, error bars, histograms and many more. it is also compatible with other popular Python libraries such as NumPy, pandas and Seaborn, making it a powerful tool for data analysis and presentation. for installing Matplotlib you can use the following command in your terminal or command prompt:
1 |
pip install matplotlib |
Also if you’re using Anaconda, you can use the following command in your Anaconda prompt:
1 |
conda install matplotlib |
Note: make sure you have the latest version of pip or conda installed.
This simple example of Matplotlib to plot simple line graph:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import matplotlib.pyplot as plt # create some data to plot x = [1, 2, 3, 4, 5] y = [2, 4, 1, 5, 3] # plot the data plt.plot(x, y) # add a title and labels to the axes plt.title("Line Plot Example") plt.xlabel("X-axis") plt.ylabel("Y-axis") # show the plot plt.show() |
This code will display a window with a line graph of the x and y data, with labels and a title.
Run the complete code and this will be the result
What is Turtle ?
Turtle is Python library for creating graphics. It was originally part of the Python standard library but has since been moved to a separate library. Turtle provides simple way to draw shapes and graphics and it makes it great tool for learning the basics of computer graphics as well as for creating simple animations and games. with Turtle you can draw shapes, lines and patterns by controlling cursor called turtle on the screen. turtle can be moved and rotated, and you can change its pen properties, such as color and width to create more complex drawings. Turtle is already included in the standard library of Python, so you don’t need to install it separately.
This is simple example of Turtle to draw a square:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import turtle # create a turtle object t = turtle.Turtle() # set the turtle's pen size and color t.pensize(5) t.pencolor("blue") # loop 4 times to draw the sides of the square for i in range(4): t.forward(100) t.right(90) # hide the turtle and display the drawing turtle.done() |
This code will display a window with a blue square drawn by the turtle. The turtle starts at the center of the window and moves forward 100 units to draw the first side of the square. It then turns right 90 degrees to face the next side and repeats the process until all 4 sides have been drawn.
Run the complete code and this will be the result
What is PyAutoGUI ?
PyAutoGUI is Python library for automating GUI interactions. it allows you to automate mouse and keyboard actions, such as clicking buttons, typing text and moving the mouse. with PyAutoGUI you can write scripts to automate repetitive tasks or perform tests on graphical user interfaces. PyAutoGUI works on Windows, macOS and Linux and supports different types of GUI applications, including web browsers, text editors and game clients. it can be used for different purposes such as data scraping, testing and automation of desktop applications. to install PyAutoGUI, you can use the following command in your terminal or command prompt:
1 |
pip install pyautogui |
Also if you’re using Anaconda, you can use the following command in your Anaconda prompt:
1 |
conda install pyautogui |
This si basic example of PyAutoGUI to move the mouse to a specific location:
1 2 3 4 |
import pyautogui # move the mouse to (100, 100) pyautogui.moveTo(100, 100) |
This code will move the mouse cursor to the location (100, 100) on the screen.
Note: Please use PyAutoGUI with caution, as it can interfere with your other actions and even cause harm to your system if not used properly. it is recommended to run PyAutoGUI scripts in virtual environment or on a secondary device.
What is Pillow ?
Pillow is library for the Python programming language that provides support for handling and manipulating images. it is fork of the Python Imaging Library (PIL) and adds many new features and improvements over its predecessor. with Pillow you can load, manipulate and save various image formats including JPEG, PNG and GIF. You can also perform tasks such as cropping, resizing, rotating and applying filters to images. Pillow is widely used in Python community for tasks such as image processing, computer vision, and web development. To install Pillow, you can use the following command in your terminal or command prompt:
1 |
pip install pillow |
Also if you’re using Anaconda, you can use the following command in your Anaconda prompt:
1 |
conda install pillow |
This is basic example of Pillow to load and display an image:
1 2 3 4 5 6 7 |
from PIL import Image # open the image im = Image.open("image.jpg") # display the image im.show() |
This code opens an image file named image.jpg and displays it using the default image viewer. you can replace image.jpg with the path to your own image.
Note: Pillow also provides many other functions for image processing such as resizing, cropping and applying filters. To learn more about the library, you can check the official documentation at https://pillow.readthedocs.io/en/stable/.