> 6 Python Programming Projects for Beginners
Once you have Python installed, you can move on to working with the language and learning the basics. To get you started, we're going to discuss several projects you can attempt, even if you have no prior programming experience.
Keep in mind, you must have Python already installed to participate.
1) Hello World
Ah, the all familiar "hello world," exercise that you do every time you start learning a new programming language. The goal here is to output a small message to introduce yourself to the language.
In Python, this is incredibly simple. All you need to do is open the interpreter and type the following:
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")
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:
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:
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
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:
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:
You can also calculate the length (size) of a list using the same command.
>>> players = ['bryan', 'john', 'chris']
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:
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"
Sweet! No more crazy robots or androids! Just Cinderella!
You can store just about anything inside a variable, including numbers, equations, and more.
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
>>> 9 < 1
>>> 6 > 2 * 4
>>> 3 == 3
>>> 5 != 2
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: "!="
The World is Your Oyster
If you don't like shellfish - or you're allergic - we apologize. Still, 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.
The world is your oyster! Get out there and... well, get out there and shuck it!
Recommended Python Training – Treehouse
For Python training, our top recommendation is Treehouse.
Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.
Treehouse has beginner to advanced Python training that programmers of all levels benefit from.