• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • Python is one of the most loved programming languages.

    And why not, you can build from simple maintenance scripts to complex machine learning application. There are many cool things you can do with Python, which you’ll love to learn.

    Introduction

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

    Some of the common use cases are:

    • Creating bots
    • Scraping websites
    • Machine learning, data visualization, and analysis
    • Web Development with frameworks like Django and Flask
    • Game development with Pygame
    • Mobile apps with frameworks like Kivy

    In this article, I’ll try to cover multiple domains with examples and show you some of the fun stuff you can do with Python. In case you don’t know python, I’ll recommend learning it!

    Let’s get started!

    For Web Development

    Python has very good support for web development with its frameworks like Django, Flask, and others. It can be used to build server-side web applications and can be integrated with any frontend. Generally, developers use JavaScript in frontend and python for supporting server-side operations. Python is not used directly in browsers.

    Django is one of the most popular web frameworks in python. These frameworks provide a package where you have a defined structure, supports database interactions with ease; all this is set up with a minimal setup command. If you want something minimal to start with – I’ll recommend Flask!

    Apart from these, Python has a large number of libraries for web development. Some popular ones are –

    Some resources to get started with web development in Python –

    Example – Access to the computer file system from mobile

    You can access your file system by running a file server on your machine. Go to the desired directory that 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 can be accessed on the same network. To access your files on mobile, simply connect to the same network(wifi or use the phone’s hotspot on a laptop). Now in your phone browser open –

    <your-computer-ip>:port

    Check your IP by running – ifconfig. Check your local IP (should start with 192.168….)

    Suppose your IP is – 192.168.43.155 and you use the default port. Then, you should open –

    192.168.43.155:8000 on mobile. You’ll see current directory 🙂

    Automation and Scripting

    If you are an engineer, you probably will be lazy and want to automate almost everything you can, right?

    No worries, python got you covered. There are a ton of things that you can automate with as little as 4-5 lines of code.  From setting cron jobs and reminders to downloading your favorite youtube videos, you can do it all with a couple of lines in python.

    Some awesome scripts and packages you can start using –

    Example – Convert CSV to JSON

    You can convert the CSV file to JSON with just 1 command in python!

    Try it now –

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

    Replace with your filename.csv, and you will get a JSON output!

    Easy, right?

    Building Games

    Python supports developing games. Its Pygame library is highly useful. It supports art, music, sound, video, and multimedia projects to be built with it. You can even make cross-platform games using Kivy, which runs on Windows, Mac, Linux, Android, and iOS.

    Resources to learn

    Example – Hangman in Terminal

    Here is a simple python program which lets you play hangman game in the terminal. Code can be shortened a lot, and I’ll leave that as an exercise to you!

    # 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 = ["geekflare", "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 would look something like –

    Web Scraping

    You see a lot of data every day across multiple sites. Think how cool it would be if you can access that data easily; that is what web scraping is, and python makes it even easier with its amazing support and libraries. Data on the web is unstructured, and python provides an easy way to parse and consume this data and even do further analysis and operations.

    Some popular scraping libraries are:

    Let me show you an example on how you can scrape currency values from a website – x-rates.com

    Example – Get currency value compared to USD

    Let’s use scraping in python to fetch currency values –

    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 equals in other currencies.

    Data Science and Machine Learning

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

    Python is well suited for data manipulation, analysis, and implementing complex algorithms. Data parsing and visualization are usually simple functions or a few lines of code with python libraries like NumPy, scipy, scikit-learn, etc.

    Python can be used in data-intensive and machine learning application using a lot of popular libraries like –

    There are a lot of deep learning tools that support python. Some popular libraries and frameworks are –

    One of the other reasons python is used is even complex machine learning models can be achieved with 20-40 lines of code. Check this tutorial on how easily visualizations can be done in python.

    Conclusion

    The tutorial discussed various domains in which python can be used. Here, I present a few of the cool and simple examples for the purpose of the demonstration, but there are a lot more awesome applications and tools you can build with Python. I hope you learned something new!

    Keep exploring. Keep learning!