en 5 cool things you can do with Python

5 cool things you can do with Python

Python is one of the most beloved programming languages.

Build everything from simple maintenance scripts to complex machine learning applications. There are so many great things you can do with Python that I would love to learn.

introduction

Python is a very popular language among developers. Writing scripts to automate and build is easy and fun.

Common use cases include:

  • Creating a bot
  • website scraping
  • Machine learning, data visualization, and analysis
  • Web development using frameworks such as Django and Flask
  • Game development using Pygame
  • Mobile apps using frameworks like Kivy

This article covers multiple domains with examples to show you some of the fun things you can do with Python. If you don’t know Python, we recommend learning it.

Let’s get started!

5 cool things you can do with Python
5 cool things you can do with Python

For web development

Python supports web development very well using frameworks like Django, Flask, etc. It can be used to build server-side web applications and can be integrated with any front end. Developers typically use JavaScript on the front end and Python to support server-side operations. Python is not used directly in the browser.

Django is one of the most popular web frameworks for Python. These frameworks provide packages with defined structures and easily support database interaction. All of this is set up with minimal setup commands. If you want to start with something minimal, we recommend Flask.

Apart from these, Python has many libraries for web development. Popular ones include –

Some resources to get started with web development with Python –

Example – Accessing your computer’s file system from your mobile

You can access your file system by running a file server on your machine. Navigate to the directory you want to access and run the following command:

 # python version >=  3.X
python3 -m http.server

# If Python version >= 2.X and < 3.X
python -m SimpleHTTPServer
#default port: 8000

This starts a file server that is accessible on the same network. To access files on your mobile, just connect to the same network (using Wi-Fi or your phone’s hotspot on your laptop). Open in your phone’s browser –

<your-computer-ip>:port

– Run ifconfig to check the IP. Check your local IP (should start with 192.168…).

Suppose your IP is – 192.168.43.155 and you are using the default port. Next, you need to open –

192.168.43.155:8000 on mobile. The current directory is displayed:

5 cool things you can do with Python
5 cool things you can do with Python

Automation and scripting

If you’re an engineer, you’re probably lazy and want to automate everything you can, right?

No need to worry. Python has you covered. There are many things you can automate with just 4-5 lines of code. From setting up cron jobs and reminders to downloading your favorite YouTube videos, you can do it all with a few lines of Python.

Great ready-to-use scripts and packages –

Example – Convert CSV to JSON

Convert CSV files to JSON with just one command in Python!

Let’s try it –

 python -c "import csv,json;print json.dumps(list(csv.reader(open('your_csv_file.csv'))))"

Replace the filename with .csv and you will get JSON output.

Easy, right?

5 cool things you can do with Python
5 cool things you can do with Python

building a game

Python supports game development. That Pygame library is very useful. It supports art, music, sound, video, and multimedia projects built using it. You can also create cross-platform games using Kivy , which runs on Windows, Mac, Linux, Android, and iOS.

Resources to learn

Example – Hangman in the Terminal

This is a simple Python program that allows you to play the Hangman game in your terminal. The code can be significantly shortened, so I’ll leave that as an exercise.

 # hangman.py
#importing the time module
import time
import random

turns = 10

print "Hello, Let's play hangman! You will have " + str(turns) + " turns!"

print ""

# delay
time.sleep(0.5)

# set of words to guess from
wordList = ["", "awesome", "python", "magic"]
word = random.choice(wordList)

guesses = ''

# loop till no turns are remaining
while turns > 0:         
    wrong = 0             

    for char in word:      
        if char in guesses:    
            print char,    
        else:
            print "_",     
            wrong += 1    

    print("\n")

    if wrong == 0:        
        print "You won :)"  

        break              

    print

    guess = ''
    if len(guess) < 1:
        guess = raw_input("Guess a character or enter the correct word: ")[0]

    guesses += guess                    

    if guess not in word:  
        turns -= 1        
 
        print "Wrong"    
 
        print "You have", + turns, ' turns left!' 
 
        if turns == 0:           
    
            print "You Lose :("

The output will look like this –

5 cool things you can do with Python
5 cool things you can do with Python

web scraping

Every day, large amounts of data are displayed across multiple sites. Think how great it would be if you could easily access that data. That’s web scraping. Python’s great support and libraries make it even easier. Data on the web is unstructured, and Python provides an easy way to parse and utilize this data for further analysis and manipulation.

Some common scraping libraries include:

Let me give you an example of how to collect currency values ​​from a website – x-rates.com.

Example – Get currency value compared to USD

Let’s get the currency value using scraping in Python –

 import requests 
from bs4 import BeautifulSoup 
  
URL = "https://www.x-rates.com/table/?from=USD&amount=1"
r = requests.get(URL) 

soup = BeautifulSoup(r.content, 'html.parser') 
ratelist = soup.findAll("table", {"class": "ratesTable"})[0].findAll("tbody")

for tableVal in ratelist:
	trList = tableVal.findAll('tr')
	for trVal in trList[:6]:
		print(trVal.text)

This returns how much 1 USD is equal to in other currencies.

5 cool things you can do with Python
5 cool things you can do with Python

Data science and machine learning

DS and ML are the most trending topics these days. These technologies are the future of computer science.

Python is well suited for data manipulation, analysis, and implementing complex algorithms. Data analysis and visualization is typically done with simple functions or a few lines of code using Python libraries such as NumPy, scipy, and scikit-learn.

Python can be used in data-intensive machine learning applications using many popular libraries, including:

There are many deep learning tools that support Python. Popular libraries and frameworks include:

Another reason Python is used is that even complex machine learning models can be achieved in 20 to 40 lines of code. Check out this tutorial to see how easy it is to perform visualizations in Python.

conclusion

In this tutorial, we learned about the different domains in which you can use Python. I’ll show you some cool and simple examples here for demonstration purposes, but there are many other great applications and tools you can build with Python. I hope you learned something new!

Keep exploring. Keep learning!