|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Python tutorial 2 |
| 4 | +--- |
| 5 | + |
| 6 | +In this tutorial we are going to look at variables, user input and decision making. |
| 7 | + |
| 8 | +##Creating a variable |
| 9 | + |
| 10 | +Python allows us to store data in something called variables so that we are able to use this data at a later point. To place an item in a variable we give it a name then set its value. |
| 11 | + |
| 12 | +Now in the REPL type: |
| 13 | + |
| 14 | + year = 2015 |
| 15 | + |
| 16 | +In this example you have now stored the value `2015` into the variable `year`. See what happens next when you type `year' into the REPL. Does it show it back to you? |
| 17 | + |
| 18 | +How about saving your age into a variable or your lucky number? Have a play around with storing numbers into variables. Let us know if you think of something more exciting to store in variables than the year. |
| 19 | + |
| 20 | +##Storing numbers in variables |
| 21 | + |
| 22 | +Now that you are familair with the use of variables, we are able to combine variables with the maths operations we learnt in the previous tutorial. |
| 23 | + |
| 24 | +Now in the REPL type the following: |
| 25 | + |
| 26 | + revenue = 1000 |
| 27 | + costs = 200 |
| 28 | + profit = revenue - costs |
| 29 | + |
| 30 | +Now type `profit` to see the results of this calculation. |
| 31 | + |
| 32 | +How much money would a sponsor at codebar spend on pizza if they had 60 students turn up? |
| 33 | + |
| 34 | +Along with pizza, students and cost, what oher variables can you think of that could go into this calculation? |
| 35 | + |
| 36 | +##Storing text in variables |
| 37 | + |
| 38 | +As well as numbers varaiables are able to store text, known in Python as string. |
| 39 | + |
| 40 | +Now in the REPl type: |
| 41 | + |
| 42 | + name = 'codebar' |
| 43 | + url = "codebar.io" |
| 44 | + |
| 45 | +Now type `name' and `url` to see these strings shown back to you. As you can see Python allows both single and double quotes to denote a string variable. Double quotes are required if there is going to be an apostrophe in the string. |
| 46 | + |
| 47 | +For exmaple: |
| 48 | + |
| 49 | + message = "I'm a string" |
| 50 | + |
| 51 | +Try storing a string within a varable without quotes, see what happens? Numbers do not require quotation marks, whereas they are mandatory for storing strings. |
| 52 | + |
| 53 | +Now store some strings in variables that contain apostrophes and some that do not. |
| 54 | + |
| 55 | +What happens when you store a number in a variable wrapped in quotes? |
| 56 | + |
| 57 | +##Storing user input in variables |
| 58 | + |
| 59 | +Now we are going to look at capturing user input using the python input command. |
| 60 | + |
| 61 | +Let's create a variable in which to store the user input. Now type this into your REPL: |
| 62 | + |
| 63 | + question = input("What is your lucky number?") |
| 64 | + |
| 65 | +Type back your answer after it asks you. |
| 66 | + |
| 67 | +Now remember how strings need either single or double quotes? |
| 68 | + |
| 69 | +Now in the REPL type: |
| 70 | + |
| 71 | + food = input("What is your favouirte food?") |
| 72 | + |
| 73 | +When you give the REPL your repsonse make sure you wrap it in quotes as this is storing your response as a string. |
| 74 | + |
| 75 | +Now try: |
| 76 | + |
| 77 | + name = input("What is your name?") |
| 78 | + greeting = "Hello " + name |
| 79 | + |
| 80 | +Then type `message` into your REPL to receive your message. |
| 81 | + |
| 82 | + |
| 83 | +### Further Reading |
| 84 | + |
| 85 | + |
| 86 | +ADD NOTE ABOUT NAMING VARIABLE! |
| 87 | + |
0 commit comments