Introduction: Getting Started With Python and Programming - the Short Tutorial
So I see you're interested in learning about programming ... or snakes.
If you are here for snakes, I'm sorry this isn't the place for you. Well, maybe...
If you are here to learn about programming or Python then you have arrived.This tutorial was made as a very basic introduction to programming in general and Python.
Maybe you will be asking: What is Python? Well, Python is just one of the many programming languages that exist for people to write instructions for a computer. In the next section you will get a better idea of what I'm talking about.
Note: In no way do I claim to know that much about Python.This just an attempt to guide a beginner through getting started with their first programming language.
What you need:
1. Windows XP or better
2. Internet connection (I assume you have one since you are here )
3. Keyboard and mouse
4. Eagerness to learn
Note: You can install and run Python on Linux and Mac, but this tutorial is for users with Windows.
Step 1: Background Information
The purpose of a programming language is to make simple and understandable commands for the computer. These commands, or instructions, will then be sent to another program that can translate what you say into “machine language” 1s and 0s that the computer can understand.
There are many languages with different purposes, but for learning about programming in general we will use Python. Other languages can may of the same things as Python, so don’t think Python is the only or best way to do things.
You can think of a computer as a very obedient five year old. He doesn’t know much so you need to give him direct and specific orders. If you told this five year old to brush his teeth he might use a hair brush and only do it for a few seconds. So you can see how you would have to be specific in the instructions you give, such as “brush your teeth with toothpaste on a tooth brush for 2 minutes.” I’m sure you can get the idea.
Step 2: Downloading Python
Note: See the included pictures for the step by step download and installation.
• First you will want to go to http://www.python.org/.
On the Python website you will be able to download the tools you need to get started.
• On the left side of the screen you will see a download tab, click it and continue to the download page. On the download page you will see multiple versions of Python (don’t worry we just need one). Find the line that reads “Python 2.7.3 Windows Installer (Windows binary -- does not include source)” and click the link to start the download. Save the setup file it somewhere you can find it.
• Once you have completed the download, find the file you downloaded and double click it to start the installation.
• You might be prompted with a security warning, click "run" to allow the install to continue.
• You will then be asked where you want Python to be installed to. By default, Python will create its own directory in your C: drive (your main hard drive). This is what we want so click "next."
• The next step in the installation will give you the option to customize the install of Python. For our purposes in this tutorial we will use the defaults without any modification of the options, so just click "next."
• Finally you simply wait for the setup to complete. Once Python is installed click "finish" to end the installation.
Step 3: Writing Code and Printing With the Interpreter
Note: In this tutorial, the code you will write will be enclosed in lines for clarity. These line will help separate the sections of code I want you to write from rest of the tutorial.
you would write what is in between these two lines.
WARNING: Don’t copy and paste code from this tutorial. You will learn better if you type it out, and if you do copy the code it might not work when you try to run it.
• Assuming your installation went according to plan, and you used the default install path, you should be able to access python through your start menu. Go ahead go to this location in your Start Menu-> Programs -> Python 2.7 ->IDLE ( Python GUI).
Before we explain what this program does, type the following into the Python Shell window and press enter.
print “Hello World, I am the Python Interpreter”
You should get an output that reads "Hello World, I am the Python Interpreter". There you go you just wrote and ran your first piece of code! You did this inside the Python Interpreter. The Python interpreter runs your code one line at a time unless if finds and error then it gives you more information about that error.
• You may receive a dialogue box saying “There’s an error in your program: Invalid Syntax.”
This means that there is an error in the way you have entered in the line of code. If you click ok, it will take you back to the line of code with the syntax error. If so make sure the code is exactly like what I wrote. Don't copy and paste!
I'm going to have you make some incorrect code so you know what to expect when something goes wrong type in the code that follows.
prints “Hello World
After you run this you should receive an error message saying: "SyntaxError: invalid syntax".
• If you add another quotation mark ( " ) to the end of the line and remove the 's' from prints, the code should work correctly.
• We will come back to the interpreter later. In the next step we will write some code that you can save.
Step 4: Writing Code You Can Save for Later
• From Idle (the Python Shell window), go to the file menu and select the option New Window.
• A blank screen should appear. This is where you will be able to write multiple lines of code and save them for later.
Now type in the following :
print “”” Look, now I have preceded what will be printed by three quotation marks
I can now add new lines inside the quotes and they will….
Show up when the program is run “””
In order to run the piece of code you just wrote, You need to go to Run -> Run Module.You will be prompted to save your code. Simply click "ok" and then save your program somewhere you can find it and click "ok." Now you should see a new Idle window that will bring up the interpreter and run your code. Success!
The three quote marks allow you to add new lines to your print statements.
Navigate away from your new window and go back to the interpreter and try out the next few lines of code.
This code will print out two lines without errors.
print """here is line one
here is line two"""
If you try this code it will not work and will give you errors.
print "here is line one
here is line two"
Now that you have handle on how to print, try and print something of your own.
Step 5: Variables
Say you want to print something twice without writing out twice each time you want to print it. Enter Variables. A variable can be used to store various types of information. For example you could do something like this:
Quote=”This is a string of characters”
print “quote = “ , Quote
print “A = “, A
print “A + B = “, A+B
Try typing the above out. Your output should look like this:
quote = This is a string of characters
A = 5
A + B = 6.23
You can think of a variable as a name for the information you want to use later.This information can be a string of text,a number, or an answer to a math problem.Variables can also be many other things, but we won't concern ourselves with that right now.
Step 6: Comments
Now edit you code to match this:
#On the following line, the text or "string" is assigned to the variable Quote
Quote="This is a string of characters"
#Here the variable A is assigned the number value of 5
#Bellow the variable B is assigned the number value of 1.23
#Now on the following lines, we are printing out what is contained
#in the variable quote with a the string "quote =" before it.
print "quote =", Quote
#Again in the following lines, we are printing out what is contained
#in the variable A with the string "A =" before it.
print "A =", A
#Finally we are doing something similar here, but with a twist.
#We are printing out the string "A + B =" but then we are print out the
#sum of the variables A and B.
print "A + B =", A+B #in our case the result will be 6.23
You should notice that when you try and run this piece of code, it is the same output as before. This is because that the symbol # represents the start of a comment. As you typed out the additions to the code you might have noticed that I tried to explain each line with comments. Go back to your code and read over the comments again to make sure you have a grasp on what you wrote.
A comment is just a way for programmers to make notes to themselves or others about how the code works. Commenting your code is very helpful for when your program gets more complicated to read.
Note: You can make your variable names anything you want, but you want them to be descriptive so you code is easy for you and other people to understand and follow later on.
Step 7: Conclusion
So I see you are really getting into this programming thing. Now you know a handful of the major concepts of programming.
Well if you want to learn more some of the best information is just a Google search away. If you like to learn more with a in-depth tutorial, you should fire up your favorite search engine and type in Learn Python the Hard Way or go to the link below. Then click on the free HTML version of the book. He will fill in where we left off and help you fill out your knowledge about python and programming.