Introduction to Python Programming

This post is designed to help people get up and running quickly with some well written Python programming lessons. I won’t assume you are dumb, but I will offer you some tips I wish I had when I first started Python programming.

First, you’ll need to install Python. All things Python version 2.x are historic (dead), so be sure to install Python 3.x. You can do that by visiting this link here, and installing the latest version appropriate to your machine. PC, Mac and Linux are supported: Download Python

Next you’ll need a place to write code (Microsoft Word is not a suitable location). If you’re a Windows user, I would take 2 minutes and install Notepad++. If I was on a Mac or Linux machine, I’d just use a built-in tool, such as vi, vim, or emacs (at least to get started). Python IDEs do exist to ‘help’ you code. Some popular ones include PyCharm by JetBrains (the community edition is free), Visual Studio by Microsoft, and some less intrusive ones, like atom. At the end of the day, you’ll just need to experiment and see what works best for you.

You’ll want to create a new file, name it something fun and unique, like supercoder.py. The best starter “rules” I could offer are to end the name in py and not to use a super predictable and boring name like, print.py. Although that might be tempting when you’re trying to teach yourself how the print function works, you’ll inadvertently cause name collisions. So, always be unique when naming python scripts.

Lab 1.1 – Print with Python

#!/usr/bin/python3
# the line above here is called a 'shebang'
# it is best practice, you should always write it in

# Create a string variable and print it to the screen
msgvar = "Hello! Hola! Bonjour! Guten Tag! Salve! Zdravstvuyte!"

# use the print function to print to the screen 
print(msgvar)

Save then run your script. On a Windows machine, you should just be able to double-click your script. On a Mac or Linux box, this should be as simple as typing something like python3 supercoder.py at the CLI.

If you’re running on a Windows machine, you may notice your script opens and closes right away. That happens for one of two reasons. The first, you have an error in your script. The second, and more likely, is that it is running correctly, and Windows is closing the command window after the script finishes. There’s a simple trick to prevent that. Edit your script again, and add the line input("Press ENTER to exit") to the bottom of the script. This function is typically used to collect string input from a user, and assign it to a variable. We’re not assigning it to a variable, so instead, the program just waits for us to hit enter. The updated script is below.

Lab 1.2 – Prevent Windows from closing the window

#!/usr/bin/python3
# the line above here is called a 'shebang'
# it is best practice, you should always write it in

# Create a string variable and print it to the screen
msgvar = "Hello! Hola! Bonjour! Guten Tag! Salve! Zdravstvuyte!"

# use the print function to print to the screen 
print(msgvar)

# cause the program to pause until the user presses ENTER
input("Press ENTER to exit")

Save and exit. Run the script. The procedure will be the same for the remaining script. Copy and paste into new files ending in .py. Then run them to test! Reach out or make a post if you have any questions.

Lab 1.3 – String input

#!/usr/bin/python3
# A simple script showing off input()

# request string input from the user
err_app = input("What application would you like help with? ")

# display the answer provided by the user
print("Okay, great! Let's find some help on ", err_app)

# cause the program to pause until the user presses ENTER
input("Press ENTER to exit")

Lab 1.4 – Type conversion

#!/usr/bin/python3
# Change string data to floats

# request string input from the user
first_num = input("What is the first number? ")
second_num = input("What is the second number? ")

# convert the numbers with type casting
first_num = float(first_num)
second_num = float(second_num)

# now perform arithmetic operations
thesum = first_num + second_num

# display the result
print("The result of ", first_num, "and", second_num, "is", thesum)

# cause the program to pause until the user presses ENTER
input("Press ENTER to exit")

Lists are a basic building block of learning to program in most languages, although, they might not always be called lists. In JSON, the same concept is known as an array. In any event, the next lab is a basic introduction to working with lists.

Lab 1.5 – Intro to lists

#!/usr/bin/python3
# intro to storing data in lists

# build a small list of strings
# lists are created with square brackets
forest = ["oak", "maple", "hickory"]

# display the list
print(forest)

# display the first tree in the list
# lists always start at position 0
print(forest[0])

# display the second tree in the list
print(forest[1])

# add a tree to the end of the list
forest.append("cherry")

# add ANOTHER tree to the end of the list
forest.append("birch")

# display the LAST tree on the list
print(forest[-1])

# print the entire list
print(forest)

# remove the last tree on the list
forest.pop()

# print the entire list
print(forest)

# cause the program to pause until the user presses ENTER
input("Press ENTER to exit")

There you have it. If you found these python programming lessons helpful, be sure to leave a comment below. If you’re looking for even more python experience, I am an author of several Python courses, as well as an instructor. Would love to lead your next training session! I offer 5-day events on-site and on-line. Reach out if you’d like me to quote your next Python training session!!

Previous
Previous

Python Training – Python Dictionaries

Next
Next

AstriCon September 27-29, 2016