[vc_row][vc_column][vc_column_text]
Python Introduction for pcDuino Lubuntu
A Python learning tool with beginner exercises in using variables, Basic maths operators, data structures and basic control flow.
Designed to be compatible with both Python2 and Python3.
Also designed to be PEP-8 compliant to encourage good coding style.
Contents
- Printing
- Variables
- Basic maths operators (add, subtract, multiply)
- Basic variable types (strings, integers)
- Concatenating strings
- Casting an integer to a string
- Booleans (True / False)
- Inequalities (Greater Than / Less Than)
- If/Else statements
- Lists
- List methods (append, extend)
- Adding lists together with +
- Sets
- For Loops
- Indexing strings
- Splitting strings
- Tuples
- Dictionaries
How to use
Go to Python intro.py and download the file (right click & save) then open the file in IDLE or IDLE3.
Tips: How to install Python IDLE IDE on pcDuino Lubuntu
OR
$ git clone https://github.com/pcduino/pythonintro
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.
OR
$python pythonintro.py
Python intro.py
# Welcome! This is a Python program file for pcDuino Lubuntu # The lines that start with a hash (#) are comments # They are for you to read and are ignored by Python # When you see 'GO!', save and run the file to see the output # When you see a line starting with # follow the instructions # Some lines are python code with a # in front # This means they're commented out - remove the # to uncomment # Do one challenge at a time, save and run after each one! # 1. This is the print statement print("Hello pcDuino") #Go! # 2. This is a variable message = "Go to Level Two" print(message) # Add a line below to print this variable # GO! # 3. The variable above is called a string # You can use single or double quotes (but must close them) # You can ask Python what type a variable is. Try uncommenting the next line: # print(type(message)) # GO! # 4. Another type of variable is an integer (a whole number) a = 123 b = 456 c = a + b print("a=%s" % (a)) print("b=%s" % (b)) print("b=%s" % (c)) # Try printing the value of c below to see the answer # GO! # 5. You can use other operators like subtract (-) and multiply (*) # Try some below by replacing the word with the correct operator # a times b # b minus a # 12 times 4 # 103 add 999 # GO! # 6. Variables keep their value until you change it a = 100 print("a=%s" % (a)) # think - should this be 123 or 100? c = 50 print("a=%s" % (c)) # think - should this be 50 or 777? d = 10 + a - c print("a=%s" % (d)) # think - what should this be now? # GO! # 7. You can also use '+' to add together two strings greeting = 'Hi ' name = 'pcDuino' # enter your name in this string message = greeting + name print(message) # GO! # 8. Try adding a number and a string together and you get an error: age = 18 # enter your age here (as a number) #print(name + ' is ' + age + ' years old') # GO! # See the error? You can't mix types like that. # But see how it tells you which line was the error? # Now comment out that line so there is no error # 9. We can convert numbers to strings like this: print(name + ' is ' + str(age) + ' years old') # GO! # No error this time, I hope? # Or we could just make sure we enter it as a string: age = 19 # enter your age here, as a string #print(name + ' is ' + age + ' years old') # GO! # No error this time, I hope? # 10. Another variable type is called a boolean # This means either True or False pcDuino_is_fun = True pcDuino_expensive = False # We can also compare two variables using == pcDuino_age = 15 pcDuinoer_age = 20 # fill in your age print(pcDuino_age == pcDuinoer_age) # this prints either True or False # GO! # 11. We can use less than and greater than too - these are < and > pcDuino_is_older = pcDuino_age > pcDuinoer_age print(pcDuino_is_older) # do you expect True or False? # GO! # 12. We can ask questions before printing with an if statement money = 500 phone_cost = 240 tablet_cost = 200 total_cost = phone_cost + tablet_cost can_afford_both = money > total_cost if can_afford_both: message = "You have enough money for both" else: message = "You can't afford both devices" # print(message) # what do you expect to see here? # GO! # Now change the value of tablet_cost to 260 and run it again # What should the message be this time? # GO! # Is this right? You might need to change the comparison operator to >= # This means 'greater than or equal to' pcDuino = 49 pcDuinoes = 3 * pcDuino total_cost = total_cost + pcDuino if total_cost <= money: message = "You have enough money for 3 pcDuinoes as well" else: message = "You can't afford 3 pcDuinoes" print(message) # what do you expect to see here? # GO! # 13. You can keep many items in a type of variable called a list colours = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'] # You can check whether a colour is in the list print('Black' in colours) # Prints True or False # GO! # You can add to the list with append colours.append('Black') colours.append('White') print('Black' in colours) # Should this be different now? # GO! # You can add a list to a list with extend more_colours = ['Gray', 'Navy', 'Pink'] colours.extend(more_colours) print more_colours # Try printing the list to see what's in it # GO! # 14. You can add two lists together in to a new list using + primary_colours = ['Red', 'Blue', 'Yellow'] secondary_colours = ['Purple', 'Orange', 'Green'] main_colours = primary_colours + secondary_colours print main_colours # Try printing main_colours # 15. You can find how many there are by using len(your_list). Try it below # How many colours are there in main_colours? # GO! all_colours = colours + main_colours print all_colours # How many colours are there in all_colours? # Do it here. Try to think what you expect before you run it # GO! # Did you get what you expected? If not, why not? # 16. You can make sure you don't have duplicates by adding to a set even_numbers = [2, 4, 6, 8, 10, 12] multiples_of_three = [3, 6, 9, 12] numbers = even_numbers + multiples_of_three print(numbers, len(numbers)) numbers_set = set(numbers) print(numbers_set, len(numbers_set)) # GO! colour_set = set(all_colours) print colour_set # How many colours do you expect to be in this time? # Do you expect the same or not? Think about it first # 17. You can use a loop to look over all the items in a list my_class = ['sky', 'young', 'Jim', 'darren', 'pillar', 'own', 'judy', 'alvin'] # Below is a multi-line comment # Delete the ''' from before and after to uncomment the block for student in my_class: print(student) # Add all the names of people in your group to this list # Remember the difference between append and extend. You can use either. # Now write a loop to print a number (starting from 1) before each name # 18. You can split up a string by index full_name = 'Young sky own jim darren' first_letter = full_name[0] last_letter = full_name[19] first_three = full_name[:3] # [0:3 also works] last_three = full_name[-3:] # [17:] and [17:20] also work middle = full_name[8:14] print first_letter print last_letter print first_three print last_three print middle # Try printing these, and try to make a word out of the individual letters # 19. You can also split the string on a specific character my_sentence = "Hello, my name is young" parts = my_sentence.split(',') print(parts) print(type(parts)) # What type is this variable? What can you do with it? # GO! my_long_sentence = "This is a very very very very very very long sentence" # Now split the sentence and use this to print out the number of words parts_long = my_long_sentence.split(',') print parts_long # GO! (Clues below if you're stuck) # Clue: Which character do you split on to separate words? # Clue: What type is the split variable? # Clue: What can you do to count these? # 20. You can group data together in a tuple person = ('Own', 26) print(person[0] + ' is ' + str(person[1]) + ' years old') # GO! # (name, age) students = [ ('Sky', 19), ('Young', 18), ('Jim', 14), ('Darren', 10), ('Own', 7) ] # Now write a loop to print each of the students' names and age for name,age in students: print name,age print "-------------------------" # GO! # 21. Tuples can be any length. The above examples are 2-tuples. # Try making a list of students with (name, age, favourite subject and sport) # Now loop over them printing each one out # Now pick a number (in the students' age range) # Make the loop only print the students older than that number number = 11 for name,age in students: if age > number: print name,age print "**********************" # GO! # 22. Another useful data structure is a dictionary # Dictionaries contain key-value pairs like an address book maps name # to number addresses = { 'Darren': '0161 5673 890', 'Jim': '0115 8901 165', 'Cherish': '0114 2290 542', 'Jarek': '999' } # You access dictionary elements by looking them up with the key: print(addresses['Jim']) # You can check if a key or value exists in a given dictionary: print('Jim' in addresses) # [False] print('Cherish' in addresses) # [True] print('999' in addresses) # [False] print('999' in addresses.values()) # [True] print(999 in addresses.values()) # [False] # GO! # Note that 999 was entered in to the dictionary as a string, not an integer # Think: what would happen if phone numbers were stored as integers? # Try changing Cherish's phone number to a new number addresses['Cherish'] = '0115 236 359' print(addresses['Cherish']) # GO! # Delete Jarek from the dictinary print('Jarek' in addresses) # [True] del addresses['Jarek'] print('Jarek' in addresses) # [False] # GO! # You can also loop over a dictionary and access its contents: for name in addresses: print(name, addresses[name]) # GO! # 23. A final challenge using the skills you've learned: # What is the sum of all the digits in all the numbers from 1 to 1000? # GO! #Clue: range(10) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #Clue: str(87) => '87' #Clue: int('9') => 9 #sum(),range() print sum(range(1001)) #reduce(),lambda(),range() print reduce(lambda x,y:x+y,range(1,1001))
Python intro outputs:
Hello pcDuino Go to Level Two a=123 b=456 b=579 a=100 a=50 a=60 Hi pcDuino pcDuino is 18 years old False False You have enough money for 3 pcDuinoes as well False True ['Gray', 'Navy', 'Pink'] ['Red', 'Blue', 'Yellow', 'Purple', 'Orange', 'Green'] ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet', 'Black', 'White', 'Gray', 'Navy', 'Pink', 'Red', 'Blue', 'Yellow', 'Purple', 'Orange', 'Green'] ([2, 4, 6, 8, 10, 12, 3, 6, 9, 12], 10) (set([2, 3, 4, 6, 8, 9, 10, 12]), 8) set(['Blue', 'Gray', 'Indigo', 'Purple', 'Yellow', 'Navy', 'Green', 'Pink', 'Violet', 'Orange', 'Black', 'White', 'Red']) sky young Jim darren pillar own judy alvin Y a You ren y own ['Hello', ' my name is young'] <type 'list'> ['This is a very very very very very very long sentence'] Own is 26 years old Sky 19 ------------------------- Young 18 ------------------------- Jim 14 ------------------------- Darren 10 ------------------------- Own 7 ------------------------- Sky 19 ********************** Young 18 ********************** Jim 14 ********************** 0115 8901 165 True True False True False 0115 236 359 True False ('Jim', '0115 8901 165') ('Darren', '0161 5673 890') ('Cherish', '0115 236 359') 500500 500500
Issues and Pull Requests
If you have an issue and don’t know how to fix it, please comment .
[/vc_column_text][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.