<​Python or Nah?​>

<Python or Nah?>

What does a person with less than six months of coding experience do at a hackathon? Well, I picked up a new programming language: Python 🐍🐍🐍

I heard Python was pretty easy to pick up and I knew I wanted to learn it eventually, so I figured a hackathon is a good time as any to pick up a new programming language. It's not. If boot camps are sprints, hackathons are drag races and you need to hit the ground running. 

Luckily for me, Python almost reads like English and picking it up becomes a lot easier if you already know JavaScript or any other programming language for that matter. Now, the fastest way to learn a new programming language and get familiar with its syntax is by jumping right in and creating small bite-sized projects. And that's exactly what I did on day one of the hackathon. If you want to just jump right into Python, I recommend watching this ~7-hour video by freeCodeCamp. It’ll walk you through how to build five different classical games using Python. It’s a great way to get familiar with Python’s syntax and you’ll also pick up a few Python libraries. 

Once I became familiar with Python, I decided I wanted to do something a little more ‘interactive’ by bringing hardware and software together. I decided to take advantage of a Raspberry Pi that was laying around at home. It's actually surprising how many people have this card-sized computer laying around at home. I took advantage of the Pi’s GPIO pins to build an umpire to go along with my virtual table tennis that flashes a red light when Player One scores and flashes a green light when Player Two scores. 

GPIO stands for General Purpose Input Output. GPIO pins are what makes the Pi so so fun and powerful and gives you the ability to interact with the real world as well as solve problems that extend beyond the confines of your computer screen. You can use these pins to attach all kinds of components and sensors to your Raspberry Pi: motion sensors, humidity and air pressure sensors, ultrasonic sensors, navigation modules, lasers, LED lights motors, cameras( which are attached through its designated slot and not through the GPIO pins), touchscreens, joysticks and the list goes on. Think of all the possibilities and just let your imagination run wild. 

All these components are fairly simple to incorporate into your project and if that is your intention when using the Pi, you’ll need to import the RPI.GPIO Python library which will give you access to the pins in your program. Once the library is imported, you’ll need a way to refer to each individual pin. This is where you’ll need to set the GPIO mode. There are two modes that you can choose from; GPIO.BCM and GPIO.BOARD. Without getting too technical, the biggest difference between the two modes is how they refer to / number the physical pins. 

GPIO.BOARD refer to the physical location of the pins and follow an easy and predictable pattern. BCM refers to ‘Broadcom SOC Channel” and the numbers to which physical pin is referred to is harder to follow. Also, the numbers can change from Pi model to model. In either case, it's best to refer to a diagram when hooking up components because there are different kinds of GPIO pins which serve different purposes and by extension, forcing you to use them you’ll use differently. Essentially, you’ll have 5V (volt) pins, 3.3V (aka 3V3) pins and ground pins (0V), which can’t be programmed at all. If you want to learn more about the pins and the different functionalities that are beyond the scope of this article, I recommend reading the Raspberry Pi GPIO documentation

Once you’ve decided what GPIO mode you’d like to use in your script, you need to pick the pin you’re hooking up your component to and set it as either GPIO.IN or GPIO.OUT. Remember that these pins are General Purpose Input/Output pins, meaning that your Raspberry Pi can send an electrical signal out into the world or receive an electrical input through the components that are attached. Once you’ve decided that a particular pin will send out signals into the world(GPIO.OUT), you now need to decide between a HIGH input (on) and LOW(off). The HIGH and LOW designations of the GPIO.OUT pin in addition to the ‘time’ Python library will help you control when to send a signal out into the world. 

The script below demonstrates the Python syntax as well as how to import libraries, set the GPIO mode, set a pin as an Input or Output and lastly how to designate HIGH or LOW to a GPIO.OUT pin. Note: Comments are followed by ‘#’. Comments are used by developers to leave comments in their code. If something is a comment, the program will just disregard and skip everything that follows it.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)

def redFlicker():
    GPIO.output(18, GPIO.HIGH)
    # wait .1 seconds before moving on to next line 
    time.sleep(0.1) 
    GPIO.output(18, GPIO.LOW)

def greenFlicker():
    GPIO.output(23, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(23, GPIO.LOW)

# an infinite loop that continuously calls functions

while True:   
    redFlicker()
    greenFlicker()
    time.sleep(.75)   # wait .75 seconds before repeating 
    

GPIO.cleanup()

When working with GPIO pins, don’t forget to ‘clean up’ at the end of your program. This is will help minimize any bugs as it resets the pins that you’ve used back to GPIO.IN. This is important because if somewhere in your program you set a pin to GPIO.HIGH and never set it back to GPIO.LOW, there’s a chance that your program doesn't run the way you expect it to especially if you’re exporting your script into another program, which can be a debugging nightmare. There’s also a chance that you might connect two GPIO.OUT pins to one component instead of connecting it to one GPIO.OUT and one Ground. The former can lead to your Pi short-circuiting and that's no fun. You can read more about why you should clean up here. And here’s a blog article as to why you should clean up in other aspects of your life.

After using Python with the Raspberry Pi I decided to explore automation with Python. And this is where things can get very cool! And kinda hackery? 🤷🏽‍♂️

Beautiful Soup is a Python library that parses through HTML and ‘scrapes’ data from it. In other words, with the help of another library called Requests, I can go to websites, download information, use Beautiful Soup to parse the website’s HTML and extract key information. So as part of my hackathon demonstration, I did just that but also took it one step further by also using python’s ‘webbrowser’ module.

import bs4 as bs
import webbrowser
import requests

sauce1 = requests.get('http://nike.com')
sweetSauce = bs.BeautifulSoup(sauce1.text, 'lxml')

for url in sweetSauce.find_all('a'):
    link = str(url.get('href'))
    print(link)
    #webbrowser.open(link) #uncomment this line if you hate browsers

What's going on in the above code snippet (only nine lines of code, mind you)? I used Requests to get raw data from Nike.com, parsed all that data to find all links on the website using Beautiful Soup and finally used the web browser to open all of their links ALL AT ONCE!!! Try it for yourself! So.Much.Fun.

When will you ever need to do this? Probably never. But its a very good example of what you can do with Python. It also demonstrates the versatility of Python. We only scratched the surface of what we can do with Python. Once you’ve played around with Python and had the chance to get familiar with it, you’ll open up some really cool opportunities for yourself like Data Science, Robotics, Machine Learning and AI. And that’s truly mind-blowing!

To view or add a comment, sign in

More articles by Munaiz Ahmed

  • <software engineering as a discipline>

    The essence of software development as a discipline and my reason to pursue software development as a career can be…

    2 Comments

Others also viewed

Explore content categories