Prolific Programming Possibilities with Python
This week at Level Up In Tech, I’ve been diving into the awesome world of #Python. I’ve been interested in Python for years, but am now finally having the opportunity to dive in with both feet and I couldn’t be more excited about it! This was originally intended to be a LinkedIn post, however, I’m sorry…I got carried away because I was having too much fun and rapidly realized I was way over the character count limit for a LinkedIn post. So…to Medium we go!
I realized today that I cannot remember the last time I was bored — I literally have no memories of being bored ever. I have an insatiable, absolutely unquenchable curiosity about a broad scope of different topics in life and in the relatively small vertical of tech, there are no shortages of fascinating and interesting topics to choose from.
I am dumbfounded that the word bored and boredom even exist. In a universe full of wonder, someone invented the word bored. Being bored is an anathema to the curious mind. I promise — there’s a point I’m getting to here.
The topic of late that I’m focused on lately is Python and what we can do with it. There’s not much in the way of limitations with respect to how you can utilize Python to solve problems, and that, my friends…is what makes Python fascinating. It should come as no surprise that Python is synonymous with prolific, even prodigious possibilities!
This week, I covered, literals, variables, comments, number systems, numeric operators, operators, bindings, input and output operations, strings, operations, calculations, lists, tuples, dictionaries, and finally, strings. Needless to say, it was a busy week, but an exciting week of learning nevertheless.
For my graphic this week, I included the Zen of Python in the image because it underscores and places a fine point on how a vibrant community can influence the further development of the language. It also serves as a framework and philosophy of sorts to how to write beautiful, efficient code. The Zen of Python was written and posted to the Python mailing list in 1999 but software engineer, Tim Peters. Another fun fact is that it’s included as an Easter egg in the Python interpreter, and can be displayed by entering, “import this” in the REPL, which stands for:
Read your Python commands.
Evaluate your code to work out what the input means.
Print the results to see the computer’s response.
Loop back to step 1. to keep communicating.
Let’s give this a try by entering the following commands in the MacOS terminal:
python3
import this
python3
Python 3.9.13 (v3.9.13:6de2ca5339, May 17 2022, 11:37:23)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import this>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>
What many don’t realize is just how long Python has been around. The language was brought to life in the late 1980’s by Guido Van Rosssum and he’s been affectionately bestowed with the title of Benevolent Dictator For Life (BDFL) by the Python community. Van Rossum continues to play a central role in which direction the language will take to this day. As a fan of Monte Python, I appreciate the fact that the language was named after the famed comedy series.
I always found home brew types of computer clubs interesting, because they were founded on the simple principle of “let’s get together and see what we can do”. The very affordable Raspberry Pi single-board computer project has even adopted Python as it’s primary user-programming language.
What I find most interesting about Python is the broad level of interoperability with other programming languages where a “glue” of sorts is required to accomplish tasks or move data between different languages. Python’s broad extensibility to work with dozens of different functions and just about anything you can imagine, including, but in no way limited to the Apache webserver and web frameworks such as Django, Flask, and web2py, to name a few. Libraries like NumPy, SciPy, and Matplotlib open Python up for usage in the scientific community.
Python is also used extensively with artificial intelligence projects with support from libraries like TensorFlow, PyTorch, and scikit-learn. With such a modular architecture, simple code syntax, rich text processing tools, and an incredibly active and vibrant community, it comes as no surprise to see Python becoming the rockstar of programming languages.
In DevOps, Python’s ease of working with JSON and YAML simplifies the provisioning of #AWS infrastructure and allows for orders of magnitude increases in speed. At the end of the day, you still have to know what you’re doing, otherwise, you could be creating catastrophes at line-rate.
With my projects, I’m always on the hunt for ways to creatively and effectively solve problems. As of late, I’ve had a great need for parsing through large quantities of data to get through the clutter to find the proverbial needle in the haystack — the salient data that illuminates why a problem is occurring and what we can do to solve for it. With that said, anytime is the perfect time to dive into Python, but RIGHT NOW is an even better time. I’m looking forward to writing code to parse through data, which will ultimately help me solve problems faster.
This week, I got right into writing and testing some code for the following project:
Create a list of services using Python. IE: S3, Lambda, EC2, etc
- The list should be empty initially.
- Populate the list using append or insert.
- Print the list and the length of the list.
- Remove two specific services from the list by name or by index.
- Print the new list and the new length of the list.
I wrote my project in Visual Studio Code, as I wanted to give it a shot as an IDE. However, I’ll be transferring this over to AWS Cloud9, which is Amazon’s Cloud IDE or Integrated Development Environment. From there, I’ll upload it to my GitHub account and come back here to post the update on this article.
Here’s a copy of the code I wrote:
#!/usr/bin/env python3.9.13
# Here I'm creating a variable to contain our list of AWS Services.
aws_services = []
# Next, I'll be populating the list using append method to add AWS Services to the list.
aws_services.append('S3')
aws_services.append('EC2')
aws_services.append('Lambda')
aws_services.append('DynamoDB')
aws_services.append('VPC')
aws_services.append('IAM')
aws_services.append('Elastic Beanstalk')
aws_services.append('Elasticache')
aws_services.append('Cognito')
aws_services.append('Cloudwatch')
aws_services.append('CloudFormation')
# For the next step, I'll print the list and the length of the list.
list_length = len(aws_services)
print("This is my list of AWS Services->", aws_services, "and the length of the AWS Services list contains ", list_length, "items.")
# Next, I'll remove two specific services from the list, first by name, and then by index.
aws_services.remove('Cloudwatch')
aws_services.remove('CloudFormation')
# Now I'll show how to remove two services from the list by index. I've commented out this portion because it would be redundant
# from my last step. So use either or to accomplish the removal of two list items.
# del aws_services[9]
# del aws_services[10]
# Lastly, I'll print the new list and the new length of the revised list by creating two new variables, aws_services2, and list_length2.
aws_services2 = list(aws_services)
list_length2 = len(aws_services2)
print("This is my revised list of AWS Services after removing two items->", aws_services2, "and the length of the new AWS Services list contains ", list_length2, "items.")
And a screenshot of my Visual Studio Code IDE:
Commenting code is important because it lets other software developers and engineers understand what the code is doing. I’ve commented my code above to clearly enumerate the steps I took so I won’t repeat it here in text.
I went ahead and tested my code by running it in the MacOS terminal. The formatting isn’t particularly pretty, but we can see that the code works and it satisfies the requirements for this week’s project.
Here’s a copy of the output from the terminal as well as a screenshot:
python3 aws_services.py
This is my list of AWS Services-> ['S3', 'EC2', 'Lambda', 'DynamoDB', 'VPC', 'IAM', 'Elastic Beanstalk', 'Elasticache', 'Cognito', 'Cloudwatch', 'CloudFormation'] and the length of the AWS Services list contains 11 items.
This is my revised list of AWS Services after removing two items-> ['S3', 'EC2', 'Lambda', 'DynamoDB', 'VPC', 'IAM', 'Elastic Beanstalk', 'Elasticache', 'Cognito'] and the length of the new AWS Serviceslist contains 9 items.
Thanks for tuning in and stay tuned for the next article. I’ll be updating this one with the addition of adding my code to AWS Cloud9 and pushing it to my GitHub account.