├── CONTRIBUTING.md ├── LICENCE.md ├── README.md ├── code └── intro.py ├── images └── python.jpg ├── learn.md ├── meta.yml └── overview.md /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | All contributions are assumed to be licensed under the same license as the source, i.e. [CC BY-SA](http://creativecommons.org/licenses/by-sa/4.0/). This license must remain in all derivatives of this work. 4 | 5 | ## Issues 6 | 7 | If you find a mistake, bug or other problem, please [open an issue](https://github.com/raspberrypilearning/python-intro/issues) in this repository. 8 | 9 | ## Pull Requests 10 | 11 | If you fix a mistake, bug or problem or have something to contribute, please create a pull request for each modification. Please consider grouping modifications sensibly, i.e. don't bundle typo fixes in the same pull request as code changes, instead file them separately. 12 | 13 | Please note that sometimes things are done for pedagogical reasons so changes which make sense from a software engineering perspective (reducing duplication or making use of more advanced programming language features) may not be suitable to maintain the intended educational value. 14 | 15 | ## Derivatives 16 | 17 | The licence must remain in all derivatives of this work. 18 | 19 | ## Licence 20 | 21 | Unless otherwise specified, everything in this repository is covered by the following licence: 22 | 23 | ![Creative Commons Attribution 4.0 International Licence](http://i.creativecommons.org/l/by-sa/4.0/88x31.png) 24 | 25 | ***Python Intro*** by [Ben Nuttall](http://bennuttall.com) is licensed under a [Creative Commons Attribution 4.0 International Licence](http://creativecommons.org/licenses/by-sa/4.0/). 26 | 27 | Based on a work at https://github.com/bennuttall/python-intro 28 | -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | # Licence 2 | 3 | Unless otherwise specified, everything in this repository is covered by the following licence: 4 | 5 | ![Creative Commons Attribution 4.0 International Licence](http://i.creativecommons.org/l/by-sa/4.0/88x31.png) 6 | 7 | ***Python Intro*** by [Ben Nuttall](http://bennuttall.com) is licensed under a [Creative Commons Attribution 4.0 International Licence](http://creativecommons.org/licenses/by-sa/4.0/). 8 | 9 | Based on a work at https://github.com/bennuttall/python-intro 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **This is an archived resource.** The repo will remain available but the resource will no longer be maintained or updated. Some or all parts of the resource may no longer work. To see our latest resources, please visit [raspberrypi.org](http://www.raspberrypi.org). 2 | 3 | # Python Intro 4 | 5 | A Python learning tool with beginner exercises in using variables, data structures and basic control flow. Instructions and guidance are given in comments in the file. 6 | 7 | ![Python](images/python.jpg) 8 | 9 | Compatible with both Python 3 and Python 2. 10 | 11 | Also designed to be [PEP-8](http://legacy.python.org/dev/peps/pep-0008/) compliant to encourage good coding style. 12 | 13 | ## Contents 14 | 15 | - Printing 16 | - Variables 17 | - Basic maths operators (add, subtract, multiply) 18 | - Basic variable types (strings, integers) 19 | - Concatenating strings 20 | - Casting an integer to a string 21 | - Booleans (True / False) 22 | - Inequalities (Greater Than / Less Than) 23 | - If/Else statements 24 | - Lists 25 | - List methods (append, extend) 26 | - Adding lists together with + 27 | - Sets 28 | - For Loops 29 | - Indexing strings 30 | - Splitting strings 31 | - Tuples 32 | - Dictionaries 33 | 34 | ## How to use 35 | 36 | Run the command `wget http://goo.gl/0ZDOdX -O intro.py --no-check-certificate` and open with `idle3 intro.py &`. 37 | 38 | Run the file with `F5` to see the output then go back to the code and read the instructions, edit away, save and run again. 39 | 40 | ## Python 2 or Python 3? 41 | 42 | The code works in both versions of Python, but Python 3 is recommended. Read about this in our [documentation](http://www.raspberrypi.org/documentation/usage/python/more.md). 43 | 44 | ## Licence 45 | 46 | Unless otherwise specified, everything in this repository is covered by the following licence: 47 | 48 | ![Creative Commons Attribution 4.0 International Licence](http://i.creativecommons.org/l/by-sa/4.0/88x31.png) 49 | 50 | ***Python Intro*** by [Ben Nuttall](http://bennuttall.com) is licensed under a [Creative Commons Attribution 4.0 International Licence](http://creativecommons.org/licenses/by-sa/4.0/). 51 | 52 | Based on a work at https://github.com/bennuttall/python-intro 53 | -------------------------------------------------------------------------------- /code/intro.py: -------------------------------------------------------------------------------- 1 | # Welcome! This is a Python program file 2 | 3 | # The lines that start with a hash (#) are comments 4 | # They are for you to read and are ignored by Python 5 | 6 | # When you see 'GO!', save and run the file to see the output 7 | # When you see a line starting with # follow the instructions 8 | # Some lines are python code with a # in front 9 | # This means they're commented out - remove the # to uncomment 10 | # Do one challenge at a time, save and run after each one! 11 | 12 | # 1. This is the print statement 13 | 14 | print("Hello world") 15 | 16 | # GO! 17 | 18 | # 2. This is a variable 19 | 20 | message = "Level Two" 21 | 22 | # Add a line below to print this variable 23 | 24 | # GO! 25 | 26 | # 3. The variable above is called a string 27 | # You can use single or double quotes (but must close them) 28 | # You can ask Python what type a variable is. Try uncommenting the next line: 29 | # print(type(message)) 30 | # GO! 31 | 32 | # 4. Another type of variable is an integer (a whole number) 33 | a = 123 34 | b = 654 35 | c = a + b 36 | 37 | # Try printing the value of c below to see the answer 38 | # GO! 39 | 40 | # 5. You can use other operators like subtract (-) and multiply (*) 41 | # Try some below by replacing the word with the correct operator 42 | 43 | # a times b 44 | # b minus a 45 | # 12 times 4 46 | # 103 add 999 47 | 48 | # GO! 49 | 50 | # 6. Variables keep their value until you change it 51 | 52 | a = 100 53 | # print(a) # think - should this be 123 or 100? 54 | 55 | c = 50 56 | # print(c) # think - should this be 50 or 777? 57 | 58 | d = 10 + a - c 59 | # print(d) # think - what should this be now? 60 | 61 | # GO! 62 | 63 | # 7. You can also use '+' to add together two strings 64 | 65 | greeting = 'Hi ' 66 | name = '' # enter your name in this string 67 | 68 | message = greeting + name 69 | # print(message) 70 | 71 | # GO! 72 | 73 | # 8. Try adding a number and a string together and you get an error: 74 | 75 | # age = # enter your age here (as a number) 76 | 77 | # print(name + ' is ' + age + ' years old') 78 | 79 | # GO! 80 | 81 | # See the error? You can't mix types like that. 82 | # But see how it tells you which line was the error? 83 | # Now comment out that line so there is no error 84 | 85 | # 9. We can convert numbers to strings like this: 86 | 87 | # print(name + ' is ' + str(age) + ' years old') 88 | 89 | # GO! 90 | 91 | # No error this time, I hope? 92 | 93 | # Or we could just make sure we enter it as a string: 94 | 95 | # age = # enter your age here, as a string 96 | 97 | # print(name + ' is ' + age + ' years old') 98 | 99 | # GO! 100 | 101 | # No error this time, I hope? 102 | 103 | # 10. Another variable type is called a boolean 104 | # This means either True or False 105 | 106 | raspberry_pi_is_fun = True 107 | raspberry_pi_is_expensive = False 108 | 109 | # We can also compare two variables using == 110 | 111 | bobs_age = 15 112 | # your_age = # fill in your age 113 | 114 | # print(your_age == bobs_age) # this prints either True or False 115 | 116 | # GO! 117 | 118 | # 11. We can use less than and greater than too - these are < and > 119 | 120 | # bob_is_older = bobs_age > your_age 121 | 122 | # print(bob_is_older) # do you expect True or False? 123 | 124 | # GO! 125 | 126 | # 12. We can ask questions before printing with an if statement 127 | 128 | money = 500 129 | phone_cost = 240 130 | tablet_cost = 200 131 | 132 | total_cost = phone_cost + tablet_cost 133 | can_afford_both = money > total_cost 134 | 135 | if can_afford_both: 136 | message = "You have enough money for both" 137 | else: 138 | message = "You can't afford both devices" 139 | 140 | # print(message) # what do you expect to see here? 141 | 142 | # GO! 143 | 144 | # Now change the value of tablet_cost to 260 and run it again 145 | # What should the message be this time? 146 | 147 | # GO! 148 | 149 | # Is this right? You might need to change the comparison operator to >= 150 | # This means 'greater than or equal to' 151 | 152 | raspberry_pi = 25 153 | pies = 3 * raspberry_pi 154 | 155 | total_cost = total_cost + pies 156 | 157 | if total_cost <= money: 158 | message = "You have enough money for 3 raspberry pies as well" 159 | else: 160 | message = "You can't afford 3 raspberry pies" 161 | 162 | # print(message) # what do you expect to see here? 163 | 164 | # GO! 165 | 166 | # 13. You can keep many items in a type of variable called a list 167 | 168 | colours = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'] 169 | 170 | # You can check whether a colour is in the list 171 | 172 | # print('Black' in colours) # Prints True or False 173 | 174 | # GO! 175 | 176 | # You can add to the list with append 177 | 178 | colours.append('Black') 179 | colours.append('White') 180 | 181 | # print('Black' in colours) # Should this be different now? 182 | 183 | # GO! 184 | 185 | # You can add a list to a list with extend 186 | 187 | more_colours = ['Gray', 'Navy', 'Pink'] 188 | 189 | colours.extend(more_colours) 190 | 191 | # Try printing the list to see what's in it 192 | 193 | # GO! 194 | 195 | # 14. You can add two lists together in to a new list using + 196 | 197 | primary_colours = ['Red', 'Blue', 'Yellow'] 198 | secondary_colours = ['Purple', 'Orange', 'Green'] 199 | 200 | main_colours = primary_colours + secondary_colours 201 | 202 | # Try printing main_colours 203 | 204 | # 15. You can find how many there are by using len(your_list). Try it below 205 | 206 | # How many colours are there in main_colours? 207 | 208 | # GO! 209 | 210 | all_colours = colours + main_colours 211 | 212 | # How many colours are there in all_colours? 213 | # Do it here. Try to think what you expect before you run it 214 | 215 | # GO! 216 | 217 | # Did you get what you expected? If not, why not? 218 | 219 | # 16. You can make sure you don't have duplicates by adding to a set 220 | 221 | even_numbers = [2, 4, 6, 8, 10, 12] 222 | multiples_of_three = [3, 6, 9, 12] 223 | 224 | numbers = even_numbers + multiples_of_three 225 | # print(numbers, len(numbers)) 226 | numbers_set = set(numbers) 227 | # print(numbers_set, len(numbers_set)) 228 | 229 | # GO! 230 | 231 | colour_set = set(all_colours) 232 | # How many colours do you expect to be in this time? 233 | # Do you expect the same or not? Think about it first 234 | 235 | # 17. You can use a loop to look over all the items in a list 236 | 237 | my_class = ['Sarah', 'Bob', 'Jim', 'Tom', 'Lucy', 'Sophie', 'Liz', 'Ed'] 238 | 239 | # Below is a multi-line comment 240 | # Delete the ''' from before and after to uncomment the block 241 | 242 | ''' 243 | for student in my_class: 244 | print(student) 245 | ''' 246 | 247 | # Add all the names of people in your group to this list 248 | 249 | # Remember the difference between append and extend. You can use either. 250 | 251 | # Now write a loop to print a number (starting from 1) before each name 252 | 253 | # 18. You can split up a string by index 254 | 255 | full_name = 'Dominic Adrian Smith' 256 | 257 | first_letter = full_name[0] 258 | last_letter = full_name[19] 259 | first_three = full_name[:3] # [0:3 also works] 260 | last_three = full_name[-3:] # [17:] and [17:20] also work 261 | middle = full_name[8:14] 262 | 263 | # Try printing these, and try to make a word out of the individual letters 264 | 265 | # 19. You can also split the string on a specific character 266 | 267 | my_sentence = "Hello, my name is Fred" 268 | parts = my_sentence.split(',') 269 | 270 | # print(parts) 271 | # print(type(parts)) # What type is this variable? What can you do with it? 272 | 273 | # GO! 274 | 275 | my_long_sentence = "This is a very very very very very very long sentence" 276 | 277 | # Now split the sentence and use this to print out the number of words 278 | 279 | # GO! (Clues below if you're stuck) 280 | 281 | # Clue: Which character do you split on to separate words? 282 | # Clue: What type is the split variable? 283 | # Clue: What can you do to count these? 284 | 285 | # 20. You can group data together in a tuple 286 | 287 | person = ('Bobby', 26) 288 | 289 | # print(person[0] + ' is ' + str(person[1]) + ' years old') 290 | 291 | # GO! 292 | 293 | # (name, age) 294 | students = [ 295 | ('Dave', 12), 296 | ('Sophia', 13), 297 | ('Sam', 12), 298 | ('Kate', 11), 299 | ('Daniel', 10) 300 | ] 301 | 302 | # Now write a loop to print each of the students' names and age 303 | 304 | # GO! 305 | 306 | # 21. Tuples can be any length. The above examples are 2-tuples. 307 | 308 | # Try making a list of students with (name, age, favourite subject and sport) 309 | 310 | # Now loop over them printing each one out 311 | 312 | # Now pick a number (in the students' age range) 313 | # Make the loop only print the students older than that number 314 | 315 | # GO! 316 | 317 | # 22. Another useful data structure is a dictionary 318 | 319 | # Dictionaries contain key-value pairs like an address book maps name 320 | # to number 321 | 322 | addresses = { 323 | 'Lauren': '0161 5673 890', 324 | 'Amy': '0115 8901 165', 325 | 'Daniel': '0114 2290 542', 326 | 'Emergency': '999' 327 | } 328 | 329 | # You access dictionary elements by looking them up with the key: 330 | 331 | # print(addresses['Amy']) 332 | 333 | # You can check if a key or value exists in a given dictionary: 334 | 335 | # print('David' in addresses) # [False] 336 | # print('Daniel' in addresses) # [True] 337 | # print('999' in addresses) # [False] 338 | # print('999' in addresses.values()) # [True] 339 | # print(999 in addresses.values()) # [False] 340 | 341 | # GO! 342 | 343 | # Note that 999 was entered in to the dictionary as a string, not an integer 344 | 345 | # Think: what would happen if phone numbers were stored as integers? 346 | 347 | # Try changing Amy's phone number to a new number 348 | 349 | # addresses['Amy'] = '0115 236 359' 350 | # print(addresses['Amy']) 351 | 352 | # GO! 353 | 354 | # Delete Daniel from the dictinary 355 | 356 | # print('Daniel' in addresses) # [True] 357 | # del addresses['Daniel'] 358 | # print('Daniel' in addresses) # [False] 359 | 360 | # GO! 361 | 362 | # You can also loop over a dictionary and access its contents: 363 | 364 | ''' 365 | for name in addresses: 366 | print(name, addresses[name]) 367 | ''' 368 | 369 | # GO! 370 | 371 | # 23. A final challenge using the skills you've learned: 372 | # What is the sum of all the digits in all the numbers from 1 to 1000? 373 | 374 | # GO! 375 | 376 | # Clue: range(10) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 377 | # Clue: str(87) => '87' 378 | # Clue: int('9') => 9 379 | -------------------------------------------------------------------------------- /images/python.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raspberrypilearning/python-intro/8874bc509ac7ac4af7b55d5c2801e9396b05b4ee/images/python.jpg -------------------------------------------------------------------------------- /learn.md: -------------------------------------------------------------------------------- 1 | By following this resource you will learn: 2 | 3 | - Printing 4 | - Variables 5 | - Basic maths operators (add, subtract, multiply) 6 | - Basic variable types (strings, integers) 7 | - Concatenating strings 8 | - Casting an integer to a string 9 | - Booleans (True / False) 10 | - Inequalities (Greater Than / Less Than) 11 | - If/Else statements 12 | - Lists 13 | - List methods (append, extend) 14 | - Adding lists together with + 15 | - Sets 16 | - For Loops 17 | - Indexing strings 18 | - Splitting strings 19 | - Tuples 20 | - Dictionaries 21 | 22 | This resource covers elements from the following strands of the [Raspberry Pi Digital Making Curriculum](https://www.raspberrypi.org/curriculum/): 23 | 24 | -------------------------------------------------------------------------------- /meta.yml: -------------------------------------------------------------------------------- 1 | title: Python Intro 2 | category: learn 3 | -------------------------------------------------------------------------------- /overview.md: -------------------------------------------------------------------------------- 1 | A Python learning tool with beginner exercises in using variables, data structures and basic control flow. Instructions and guidance are given in comments in the file. 2 | --------------------------------------------------------------------------------