Python is a great language for learning the basics of programming. There are so many websites out there which will help you. Why not have a quick look? See what you can find out!
The next few pages will teach you how to do some coding. Each page will have a small task todo in order to send you to the next page. It will explain how to write the code and all you have to do is ensure you enter the correct code and run. Simple!
You can store text in variables as well as numbers. In order to tell the difference between text and a variable you use quotation marks. a = "hello" b = "world" c = a + b print c What will the above code print out? Give it a go! RunOutput: (clear)
What is “hello” minus “world”? No idea, same here! Silly question really! Some operators do not make sense when it comes to text. There are only two you can really use. Try the code below – a = "hello" b = "bert" print a + b print a * 3 print (a + b) * […]
There are a number of tests which you do. These are known as conditions. The code below demostrates the main ones. a = 34 b = 87 if a < b: print a, "less than", b if a > b: print a, "greater than", b if a == b: print a, "equal", b if a […]
If statement conditions are based on true or false values. You can either use true or false variables directly or do a test. a = True if a == True: print "it is true" if a: print "it is also true" In python true is written with a capital letter (as is false). You can […]
Look at the IF statement below. What condition would ensure that “how do you like them apples” is printed? a = 4 b = 10 if ????: print "I like cheese" else: print "how do you like them apples" RunOutput: (clear)
Let us consider a shopping list. All of the prices of the items are stored in an array as shown below prices = [3.99, 12.99, 4.49, 18.99, 0.99, 0.89, 2.35] To add up the total for this we need to loop over each price. With each price we need to add it to a total […]
Have you noticed that a while loop has a condition like a if statement? No? Oh dear…. A loop will run while the condition is true. The second the loop becomes false it will stop. x = 10 while x > 0: print x x = x - 1 This loop will start at 10 […]
Time to try something useful! total = 0 c = 1 while c
You can use text in loops as well as numbers. Look at the code below – a = "*" while len(a)
A common task is to get a small section of characters from a string. This is sometimes referred to as a sub-string or substr for short! In python sub-strings are easy peasy! You just use the same syntax as the last exercise to specify the start letter and then say the end letter. In the […]
It is official! The following people are cool and this code knows it! cool_people = "bert sally susan fred fiona" pos = 0 start_pos = 0 while pos < len(cool_people): if cool_people[pos] == " ": print cool_people[start_pos:pos], "is cool" start_pos = pos+1 pos = pos + 1 Can you work out what is going on? […]
cool_people = "bert sally susan fred fiona" pos = 0 start_pos = 0 while pos < len(cool_people): if cool_people[pos] == " ": print cool_people[start_pos:pos], "is cool" start_pos = pos+1 pos = pos + 1 Not sure what the code does? Well here is an explanation! line 1 - This creates the list of names, each […]
When you use the sub-string feature the result can be stored in a variable. The code example below demonstrates this. text = "hello world" hi = text[0:5] world = text[6:11] text = "goodbye world" print hi, "silly", world print text Once the sub-string is stored in a variable you can change the original without effecting […]
The following loop will reverse a string. It is very easy to do! text = "reverse me" x = len(text) -1 temp = "" while x >=0: temp = temp + text[x] x = x - 1 print temp We start at the end of the string (remember strings start at 0 which is why […]
When you want to change a variable you can use code like this – x = 4 x = 9 x = x + 1 print x To change an array you need to access an individual element within the array first before you make a change. numbers = [5,2,1,7,1] print numbers numbers[3] = 20 […]
Getting values in and out of an array is all about the index! You can use any expression to represent the index. Python will evaluate it and substitute the answer as the index. Look at the code below – numbers = [3,6,1,9,2] print numbers numbers[1+1] = 20 print numbers x = 3 numbers[x] = 30 […]
The best thing about arrays is that you can loop over them in the same way you can loop over individual letters in a string. In fact the code for both loops is pretty much identical! The code below is a updated version of the friends code we saw a few exercises ago. friends = […]
There are numerous methods which can be applied on lists. You can find out more by following this link. fruit = ["pear", "banana", "apple", "mango"] fruit.reverse() print fruit fruit.sort() print fruit Append “lemon” to the list, sort it and then finally print to move onto the next task. RunOutput: (clear)
A list can contain other lists. By using this nifty feature you can store more complicated data. Consider the following example – test_scores = [] test_scores.append(["bert", [76, 21, 43]]) test_scores.append(["sue", [11,9,92,12]]) for student in test_scores: print "Student - ", student[0] total_score = 0 for score in student[1]: total_score = total_score + score average = total_score […]
Now print out the text “it seems print is a keyword!” RunOutput: (clear)
The video below will show you how to install Geany and Python. The links to the key files and extra help can be found under the video. Download link to python – http://www.python.org/getit/ Download Geany – http://www.geany.org/Download/Releases Setting up Python – 1. Download the version for your computer. It is advised to download version 2.7 which is […]
Variables allow you to save a value for later use. This later use may be to display it or to perform further calculations. Whatever the reason variables form the backbone of most programs. Some sample code to try – The code sample should print 11. Open up Geany or try it in the interactive editor […]
Programming involves some maths. Every program, from games to apps, will use maths in some form. The following symbols allow you to do the basic maths functions – * Multiply / Divide + Plus – Minus In order to make your python program output the answer to a calculation you need to use the print […]
Syntax result = raw_input ( 'question?' ) About raw_input raw_input allows you to get text from the user for later use in your program. In order to use raw_input you need to combine it with variables. The sample code is taken from the video. Try and add a few more questions and a […]
You can use brackets in your maths to make sure everything is done in the correct order. For example the code below produces two different values – print (4 *5) + 6 print 4 * (5 + 6) print 4 * 5 + 6 Brackets tell python the order you wish to do things in. […]
Syntax if a > 59 : print 'True block' else : print 'False block' About IF If statements allow you to change the flow of your program. It allows you to test a condition, test what the user has entered or evaluate the current state of your variables. Fundamentally an if statement will […]
Syntax if a > 59 : print 'True block' elif a > 80 : print 'runs only if the second condition evaluates to true' else : print 'False block' About elif elif allows you to chain together multiple if statements. It will only ever run one of the statements which is different […]
In order to place a value into a variable you use the “=” sign. This is called assignment. When python sees an assignment it will copy the result of the right hand side and store it in a variable on the left. This is why you must always have a variable name on the left. […]
Did you know that 80% of a computers time is spent in only 20% of the code? The reason for this is most things that a computer does it will repeat over and over again with only small changes. Think about a computer game. It will draw the screen many times every second, normally only […]
Syntax if a < 0 and a > 10 : print 'True block' else : print 'False block' About binary logic and IF Binary logic uses AND, OR and NOT to link together conditions in if statements and loops. Understanding binary logic is crucial if you want to use if statements effectivly. Below […]
Variables are used to store the results of calculations in order for them to be used later. # times table x = 2 y = 6 result = x * y x = 3 print x * y print result Run the code. Did you expect the two values which were printed to be the […]
Strings can be manipulated in many ways. The first one we will look at is how to access certain characters. x = "Ah I see this is a string!" print x[5] Try the above code. See how only one character is printed? and a “s” no less. If you count the letters you will notice […]
Syntax print random . randint ( low , high ) About random numbers Note – You must import random before using random numbers. Random numbers are produced in python by using the random module. The video below demonstrates how to use one function from the random module, randint(a,b). Random numbers on a computer are actually […]
There are a number of string methods which you can use for all of your string tomfoolery! We are not yet at the stage where we can explain what a method is, but for now just think of it as something a string can do. You can find more about string functions at the official […]
Syntax x = 1 while x<=10 : print x x = x + 1 About While While loops allow you to repeat code. In order to repeat code you need to know when the code should start, when it should end and how many steps it must take from start to end. This […]
Every language contains some form of data structure which allows you to store more than one value inside a variable. These tend to be called an array or a list. In python we call them lists. What is the difference between an array and a list? What is a data structure? Lost? Ok let us […]
A lot of languages have more than one way to loop and python is no exception. For in is the other main loop in python. Below is a example of how for in works – names= ["bert", "fred", "sally"] for x in names: print x It is a simpler way of iterating over a list […]
Syntax arr = [ 32,54,11 ] About arrays Arrays allow you to store multiple values into a single variable reference. This makes the manipulation of the data held within the array much simpler by the use of a loop. The sample code is taken from the video. Key term Data structure – a specific way […]
Python uses lists instead of arrays. Lists are dynamic and have specific methods which can make dealing with collections much simpler. With arrays once you have created one you are unable to add more items. They are fixed length and as such are known as fixed length. Lists on the other hand are dynamic which […]
Syntax arr [ 2 ] = 43 List Vs Array Python uses lists not arrays. However a lot of the GCSE and GCE exams will make reference to arrays. It is important that you understand the difference. The key differences are – Arrays have a fixed size while lists are variable Arrays do not have […]