GETTING STARTED INSTALLING PYTHON First you need to make sure that your computer has the software you need to be able to code with Python Download the newest free version from the Python website There are some apps for tablets that run Python but you will need to use a Mac or PC to do most of these activities Ask permission from the person who owns your computer before you download the software STEPI FIND THE PYTHON WEBSITE www python og Open your web browser and visit www python org Downloads STEP 2 START DOWNLOADING Click the Downloads button near the top of the web page Select a version from the list at the bottom of the page Different versions of Python may be available for your computer This book uses Python 3 5 Make sure you choose a version starting with Python 3 not Python 2 For Windows XP or older versions download Python 3 4 Wait for the download to complete Download Python 3 52 Download Python 3 STEP 3 INSTALL THE SOFTWARE STEP 4 RUN PYTHON Some browsers will then ask you to run the installation program Choose Run If this doesn't happen don't panic The installer file should have been downloaded to your computer Look in your Downloads folder for it Double click it to start installing Python You should get a box giving you instructions on what to do next Follow them to complete the installation On a PC Click Start Programs Python IDLE Or just click IDLE Python if it appears in the recently added section On a Mac Click Finder Applications Click Applications Desktop Double click Python 3 Python 3 Click the IDLE icon Make a shortcut by dragging the IDLE file IDLE onto your dock at the bottom of the desktop Click the icon IDLE
HELLO WORLD SAYING HI When computer programmers start learning a new language the first thing they usually do is learn to make a program that displays a message such as Hello world Let's look at how to do that in Python STEP I RUN IDLE STEP 2 START A NEW FILE Click the File menu then choose New File IDLE File Edit IDLE stands for Integrated Development and Learning Environment This is the program you will use to create your code Click the shortcut to IDLE on your computer See step 4 from page 4 New File Open STEP 3 GET CODING STEP 4 SAVE YOUR CODE Click Save Carefully type in the code shown below The brackets tell Python where the item to display starts and ends Click the IDLE File Edit File menu then Open choose Save Save Type hello as the filename STEP 5 RUN YOUR CODE Click the Run mat Run Optio menu then choose Python Run Module Run Module The print command tells Python to display something The quotation marks tell Python that the item to display is a text string some letters CUSTOMIZE Add more lines to your program Can you make it tell a simple story You must type your code in exactly If you leave out a quotation mark or bracket it will not work IDLE will make parts of your code change color to let you know you have added brackets and quotation marks correctly Python is case sensitive which means you need to use lowercase letters for your commands You'll need to save and run your program steps 4 and 5 each time you add code new
LOOPS AND VARIABLES If you've done some coding before you will know that one of the most important programming concepts is the loop If you want your code to do something over and over again it's much quicker to use a loop instead of copying out the code several times Let's look at how this works in Python STEP 1 START A NEW FILE STEP 2 GET CODING IDLE Run IDLE See step 4 from page 4 if you need help Click the File menu then choose New File File Edit New File Carefully type in this code Press enter after each line Untitled Open STEP 3 SAVE YOUR CODE STEP 4 RUN YOUR CODE Python3 5 Shell Click File Save IDLE File Edit Type loops as the filename Open Save Click Save Click the mat Run Optio Run menu Python then choose Run Module Run Module STEP 5 USE A LOOP The for command starts the loop a is a variable see page 7 The number of times to repeat the loop Steps 1 to 4 are a simple way to make our program say hello three times But what if you wanted to make it say hello ten or a hundred times There is a quicker way Delete your code and change it to the code shown to the right Check your code carefully for mistakes Save and Run your code What happens The commands you want to repeat must be indented Python should automatically indent lines for you If it doesn't just use the space bar or the tab key to indent the code exactly as shown A colon must be at the end of the line
STEP 6 KEEPING COUNT A variable is used in Python to count how many times a loop has been repeated In our program the variable is called a but it can be given any letter or word as a name By printing that variable we can change our program to make it count from 0 to 4 Variables are special parts of a program that store data or information Variables are different from ordinary numbers because they can be changed as a program runs Variables have a name that is used to point to a part of the computer's memory A value is then stored in that part of the memory Delete and change your code so it says loops py for a in range 5 print a Python 3 5 Shell Look at how the print command has been written If there were quotation marks around the letter a Python would think it was a text string and would print the letter a ten times But because there are no quotation marks Python knows a is a variable Save and Run your code to see what happens STEP 7 COUNTING BACKWARD Delete and change your code again and insert the extra numbers shown inside the brackets below CUSTOMIZE Try to make your program count down from 100 all the way to 0 Pick a times table such as the five times table Adapt your code so it counts from 5 10 15 20 up to 60 The number to start at Stop repeating when this number en this number is reached Change the number by this amount each time KEY CONCEPTS LOOPS When we want to repeat some code we use a loop Save and Run your code VARIABLES When we want a number to store a value that will change we use a variable In our loop a is a variable