- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Learn To Program: How To Install Python and Write Your First Progam
Learn To Code Tutorials
This is the first of a series of tutorials designed to help you learn the basics of Python.
Learn To Code This 2014
If you want to learn how to program and create programs confidently, then this is the hub for you!
In this hub you will learn:
- what programming is
- why learn Python
- how to install Python
- how to write your first program
- how to use Python as a calculator
- Fundamentals of Python: strings, writing your first program
One of your new year's resolutions may be to learn to code but you don't know how or think it's too hard. That isn't true and this hub will show you how to program.
An Introduction To Programming
What Is Programming?
When browsing the internet, navigating through your folders, made PowerPoint Presentations or typed some text into Word, you have utilised computer programs. These programs run on thousands of lines of code that run and produce the applications you see on your screen. They do not run on magic but run in a logical manner. The intelligent folks at Microsoft wrote those programs for you, they are known as software engineers. It may seem daunting at first, when you think of the complexness of an application such as Microsoft Word but as you write programs, learn more and build confidence you will discover that Microsoft Word runs in a logical fashion, just like all programs. Here are some other facts you should know about programming:
- Programming is writing code so that a computer can understand it and output the result.
- You write code and the computer outputs what you asked it to do.
- You can create word-based programs which are relatively simple to write or graphical programs that make use of
- Through programming, you can create applications that other users can use on their PC's, tablets and smartphones.
When you write a program, you are essentially writing code. That code can vary depending on what language you are writing it in. All coding concepts and fundamentals are the same (such as strings, loops, blocks, methods, don't worry you'll learn about it later) but the way you write it is different. Some programming languages are easy to learn than others, whilst some are 'beginner' languages which only serve as an introduction to coding. Here are some languages:
- Java - I'm sure we have all heard of this one, and it is more than likely to be running on your computer right now. Java is of a moderate difficulty, and you will require a lot of tools (compilers, NetBeans/Eclipse, runtime enviroments etc). I would not recommend this for complete beginners, but you can still have a go at it if you like.
- C++ - Again, this is a popular language but is fairly difficult for beginners to learn.
- Python - The language that we will be learning and is slightly easier to learn than Java or C++. It is an official language used by many programmers all over the world and is very easy to set up. I will now explain the advantages of learning Python.
Why Learn Python?
- It's an easy language to learn, it isn't utter gibberish like some other programs and is easy to understand.
- Google use it, yep, that's right. They are always looking for programmers in Python so it must be an official language. A lot of Google's products and algorithms were written in Python and if you want a job at Google, you will have an advantage by knowing Python.
- It is quick and easy to set up. Just one download from Python.org's website and you are good to go. With Java you have to download NetBeans or Eclipse, the JRE, the Java Development Kit and more.
- It is very easy to run your written code. You simply save your code in Python's editor in any folder you want on your PC and run it.
- There are so many tutorials out there to help you, from books to websites to courses on Coursera, you can always better yourself if you want.
- It is always updating and always stays current with today's technology.
- In a couple of months, you will be able to write games, complex programs and applications with Python.
How To Install Python (Windows)
1. Open another browser window and type www.python.org into the address bar or search for it on Google. Once you have reached Python's homepage, look to the left and you will see a vertically stacked selection bar. Once you have seen this, click on the Download link (see the image below).
2. You will be brought to Python's download page. Then, select Python 3.3.3, the most current and up-to-date version of Python there is.
3. This will bring you to the Python 3 download page and scroll down. If you have Windows, click on the download link which is highlighted in the 3rd picture in the thumbnail below. If you have a Mac, tell me in the comments below and I'll help you out. This download link will download Python to your computer. Make sure it is saved in a place where you will find it, preferably on your Desktop. Open up the Python folder and you will see a variety of folders within it but the actual editor we will be using will be here in the main folder. It is called: IDLE (Python GUI)
4. Open this up and it is time to begin!
Step By Step ProcessClick thumbnail to view full-size
Arithmetic In Python Shell
Now that you have Python set up and opened, its time to begin. When you have opened IDLE Python GUI, this is what you'll see, the Python Shell.
The Python Shell is an interactive module where you can input some code and Python will give you the output. You can do arithmetic and basic commands with this, but we won't be using it when coding programs. To begin with though, let's do some arithmethic to get a feel of Python's Shell. Type the following into the Shell and press enter:
5 + 3
And you should get this:
The Python Shell
As you can see, its possible for you to do arithmetic in Python Shell with multiplication, division, subtraction and addition. Not only that but you can also work with big numbers too, a feature that is not available on your calculator.
The >>> is known as a prompt. This means that it is prompting you to write something or waiting for you to give it a command.
With the Shell, you can also print text too. Try typing the following line of code exactly after the prompt and hit enter.
The Text Editor
Well done! You have just gotten your computer to say Hello World! to you. Hello World is usually the first command that beginner programmers ask the computer to do, it has become a tradition. However, it is not a proper program yet, we cannot save it or build on it, it is only a one-line prompt. Instead, we will have to build a program that we can save, reuse and build upon.
To do this, you will need a text editor. In this editor, you can write code, build programs and save these which can be accessed again. Python has a text editor built in. To access it, whilst in Python Shell, go to File and then New Window. This will open up a blank screen waiting for you to fill it up with code. The Shell will give you your output based on the code you've written. It's time to get building!
Writing Your First Program
In your New Window, type the following onto the first line.
Once you have the following typed in exactly, go to File and then Save As. Save your program wherever you want, it does not have to be in your Python Folder. I suggest you create a folder where you can put all your programs. Then, press Run and then Run Module or press F5 as a shortcut. Your program will print Hello World in the Python Shell.
What Your Program Should Look Like
- The word print means that you want the computer screen to output the text in the brackets to the user, in short, you want text printed on the screen. So, if you want to print text on the screen, use print. You will notice that the word print is highlighted in purple (if you are using Python 3). This is known as syntax highlighting and is one of Python's main features. All functions (or actions that you want your computer to perform) are highlighted in purple. Any words highlighted in purple means that they are keywords, Python recognises them. If you typed printy instead of print, you will notice that it is no longer highlighted in purple. That way you know if you went wrong.
- After the word print, you have a bracket, then double quotation marks, then your text, your double quotation marks once more and your bracket.
- You will notice that the text that we wrote is highlighted in green. Python is telling us that it is a piece of text and is a string.
What is a string? Well, a string is basically a piece of text within a program that will be used in the output of the program. Strings are enclosed in either single quotes or double quotes, and are highlighted in green. You can print any text you like by putting the function print in front of the string. You will be using strings all the time when writing programs so get yourself familiar to them! Other examples of strings include:
print("My name is ...") print("Its nice to meet you") print('Hi!')
What do you think of this tutorial?
In The Next Tutorial...
You will be learning about variables, embedding variables in your code. how to create an interactive program where you will communicate with your computer and input.
© 2013 Susan W