Beginner Series: Python Tutorial
Once Python and the development tools are installed, you can begin working with the language and learning the basics. To help you get started, we’re going to walk you through several novice-level projects; they’re pretty easy to follow even if you have no previous programming experience.
Keep in mind, you must have Python already installed to participate.
If you don’t have the necessary environment setup, be sure to look at our previous segment on Setting Up a Python Development Environment.
1) Hello World
Every novice developer starts with the infamous “hello world,” exercise that is meant to introduce you to a new programming language. The goal here is to output a small message because unsurprisingly, that is one of the most common things you will be doing with any language.
Compared to other languages, this task is incredibly simple to achieve in Python.
All you need to do is open the interpreter and type the following:
print("Hello World") print("My name is") #add your name after the word "is" obviously
If all goes well, you should see something like this:
> python3 #to call upon Python on MAC OS X use this command, for Windows use "python" Python 3.5.1 (default, Jan 14 2016, 06:54:11) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World") >>> print("My name is Bob") Hello World My name is Bob
Clearly, the command print is used to display content on the screen. Remember this command because you’ll be using it often.
The text you see after the # symbol is called a comment. Comments do not appear at runtime and are instead meant for the developers who will be working with the code. The comment we left above provides instructions for adding your name to the message. More often than not, comments will provide labels or quick descriptions for a snippet of code, so you can easily identify what a particular section is for.
2) Performing Calculations
Next, let’s take a simple calculation and feed it through the interpreter to see what happens. Enter the following:
7 + 2
After typing in the equation above and hitting enter – to submit – you should see something like the following:
>>> 7 + 2 9
Notice how the interpreter automatically answers the equation and spits out the result?
3) Creating Your First String
A string is a sequence of characters that can be processed by a computer. The string is usually stored for manipulation later.
Strings must always begin and end with the same character, this is a requirement. In addition you can use either single or double quotations to signify a string, there is no difference between the two. The quotation marks only serve to inform Python that what’s inside of them is a string.
Let’s save your name as a string to call upon later. To do that, type the following into the interpreter:
>>> "Bob" 'Bob'
Congrats! You just created your first string, and this is signified by the information sent back to you. We can see that the name was saved as a string.
Now, we want to test out this string and see what kinds of things we can do with it. First, let’s use multiple strings in tandem. Enter the following into the interpreter:
>>> "Hello there " + "my name is " + "Bob" 'Hello there my name is Bob'
Notice how Python adds the strings together before outputting the content?
Another neat trick you can do is multiply strings or manipulate them through equations.
>>> "Bob" * 4 'BobBobBobBob'
This may seem silly right now, as you would probably never need to multiply your name like this in the real world. However, this type of manipulation can really come in handy when you’re working on large projects in Python that have a lot of strings.
To see your name in upper case – instead of using caps – try working with the following command:
>>> "Bob".upper() 'BOB'
Pretty cool, right?
4) Return the Length of a Phrase or Word
Normally, if you want to know the number of letters in a word or phrase you would just count them yourself, but that’s no fun! There’s actually a dedicated command to do just this!
To determine the number of letters in a word or string, type the following into the interpreter:
>>> len("BobIsTheGreatestEver") 20
You can also calculate the length (size) of a list using the same command.
>> players = ['bryan', 'john', 'chris'] >>> len(players) 3
5) Storing Variables
Each entry in the list of “players” we created above is called a variable. Variables are nothing more than names or titles for a particular set of data so that you can store them and call upon them later. For example, the variable in the tutorial above was “players” because that’s what we used to store the names of the players.
Let’s create a new variable of our own:
>>> movie = "Terminator"
Our variable is “movie” and in that variable we stored the data “Terminator,” as you can see.
One thing you’ll notice about variables is that the interpreter doesn’t return anything once the information is stored. You may be wondering how we know the variable was actually stored?
You can test this by simply entering “movie” in the interpreter and hitting enter. It should return the data stored in that particular variable, like so:
>>> movie 'Terminator'
Good job! You created your first variable! Feels great, doesn’t it?
But, let’s say we get sick of seeing “Terminator” as the data stored in that variable. We can change this easily.
>>> movie = "Cinderella" >>> movie 'Cinderella'
Sweet! No more crazy robots or androids! Just a compassionate, naive girl named Cinderella who will finish all our chores for us!
You can store just about anything inside a variable, including numbers, equations, and more.
6) Comparisons
One remarkably useful – yet underrated – thing you can do with a programming language is compare sets of data. Let’s try that now, using numbers.
>>> 7 > 2 True >>> 9 < 1 False >>> 6 > 2 * 4 False >>> 3 == 3 True >>> 5 != 2 True
Notice how we used two equal signs (==) to check if sets of data are equal? You must always use two equal signs if that’s what you are trying to do. This is because a single equal sign, or “=” is used to assign a value to a variable.
In addition, if you want to check whether or not two values are unequal you can use an exclamation mark followed by an equal sign like so: “!=”
Time to Move on to More Advanced Content!
You should be pleased to know that you’ve completed the basic tutorials! If you feel comfortable you can move on to a full-featured tutorial that will walk you through the steps, from beginner to advanced.
Good luck on the rest of your journey fellow programmers! Code on!
Finally, we’re going to list some Resources to Learn More About Python.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.