computing subject knowledge enhancement python. session outline audit of your current position...
TRANSCRIPT
ComputingSubject Knowledge Enhancement
Python
SESSION OUTLINE
• Audit of your current position• Practical Investigation• GCSE Controlled Assessment• Q&A
AUDIT
Please complete the following audit
Audit
PYTHON 2 or 3
Python has two versions which are commonly used. St James’ currently use Python 2.7.8 because;
– It has more support for the libraries available– It is the version the Universities I visited used– It was the final and most stable version from
Python 2
WHAT WE ARE COVERING
From the results of your audit I am going to cover the following sections;
– IDLE and how to use it– Basic Python Syntax– Data Types and Variables– Additional Modules– For and While Loops– List and Dictionaries
IDLE
IDLE is the application we use to develop Python programs. IDEL is;
– Free to download– Easy to install as it requires no additional
permissions and does not make an executable– Provides some syntax highlighting and error
handling– Interactive and script modes
THE BASICS
When coding in Python for the first time you need to know a few important things;
– Syntax• Red = Comments• Green = String• Black = Normal Text• Builtin Functions = Purple• Key Words = Orange
THE BASICS
In addition to syntax highlighting;– Python offers object help using the tab key– Python is a little forgiving when using variables– Script mode can be executed on the fly pressing
F5 or by running the .py file
DATA TYPES
There are many different data types available but the three which we are going to use are;
– Strings – Also known as text and must be placed within a set of speech marks e.g. “I am a string”
– Integers - Also known as whole numbers between -2,147,483,648 and 2,147,483,647
– Floats - Also known as real numbers which include decimal places e.g. 19.99
DATA TYPES
A few rules that you need to be aware of when trying to manipulate data;
–“A” + “A” = “AA”–“A” + 1 = ERROR–“A” * 2 = “AA”–2 * 2 = 4
VARIABLES
In all of the programming languages you have used, variables have played a really important role. This is because;
– A variable can store any type of data– A variable can change the value of the data it stores– A variable has to be given a useful name– A variable allow your program to remember specific things
studentName = “Dave”studentAge = 14
PYTHON VARIABLES
What makes Python good for first time coders is;
– Variables don’t need to be declared• Dim interestRate As Integer (VB)• Int InterestRate (JAVA)
– Python allows you to name a variable and just use it regardless of the type of data
LESSON NUMBER 1
The most important lesson that new coders need to learn is;
GETTING IT WRONG = GETTING IT RIGHT
NAME AND AGE
Produce a simple program which can do the following;
– Store your own first name (name)– Store your own age (age)– Print out a your name and age e.g. Dave is 48
CODE SNIPPETS
Even the most experience of programmers need to use the internet to gain code snippets;
– Site 1– Site 2– Site 3– Site 4 (MOST USEFUL)
A DIFFERENT WAY
Another way I give learners coding challenge is to;
– Give a part complete program with comments– A complete program with errors– An entire challenge coded in pairs
INPUT vs RAW_INPUT
Gathering user input is always on GCSE controlled assessment and this can be done two ways in Python;
INPUT RAW_INPUT
Gives a customized message Gives a customized message
Can detect data type based on syntax Always stores value as string
Can produce error based upon input Requires type conversion/cast
Input(“Enter a phone number”) Raw_input(“Enter new cost”)
HOW TO CONVERT/CAST
To convert a value or variable from one data type to another you need to use a range of functions;
– Str() function will convert any value or variable into a string• The str() function requires a single argument, this goes within the
parentheses ()• The argument is the value or variable that you want to change e.g.
str(10.99) or str(phoneNumber) – To convert to integers you use int() and to convert to a
float you use float()
NAME AND AGE ENHANCED
Enhance your name and age program so that;– Ask the user to enter their name (name)– Ask the user t o enter their age(age)– Print out a the name and age e.g. Dave is 48
VAT PROBLEM
You must now produce a solution to the following problem
– Stores the value of VAT in a variable– Asks the user to enter the COST of the product– Calculates the COST + VAT– prints() out a message plus the TOTAL
POSSIBLE SOLUTION
A solution of the problem could something like the following;
IMPORT MODULES
One useful thing within programing languages are modules;
– Use the key word import– Import your module at the very start of your
program using the module name– To access the modules specific operations use the
name of the module followed by a full stop and the operation
USEFUL MODULESHere is a list of useful modules which you may need to use at some point or you may want to experiment with;• Random – number focused module• PyGame – GUI tools for games• TkInter – GUI tools for any type of program• Math – simple math focused module• Decimal – exactly what it states, decimal numbers• Datetime – as above, focused on date and times• Time – as above but just time focused• Re – useful for checking a range of operations on strings• Pickle – used for storing values in external files• Operator – specific operations e.g. add(a,b) instead of a + b
FOR LOOP
If you consider doing something over and over again, this is technically called a loop. When something gets repeated;
FOR LOOP STEPS
The for loop consists of the following steps;– For – This tells your program that you are about to perform a
loop– Variable e.g. counter – This will store how many times the loop
has occurrd, allowing the loop to know when it has reached its target
– Range – This specifies how many times the loop should occur, important note here, if we did 1-5 the loop would only occur 4 times as we say loop 1, 2, 3, 4 and stop before 5.
– Repeat statement(s) – This is the statements that will be repeated until the loop reaches the target number
FOR LOOP PRACTICE
You are now going to practice the following programs;
– Repeat the statement “I can use a for loop correctly” 3 times
– Repeat “I have been looped” 5 times plus the value of the counter variable
WHILE LOOP
Similar to a for loop is a while loop, these both can do the same job but a for loop repeats a specific amount of times, a while loop repeats until a condition has been met.
WHILE LOOP STEPS
The while loop consists of the following steps;– Variable(s) declared and values assigned – This will
be the section which stores a value that a while loop will check
– While – This will check if a condition is true, while this condition is true the statements below will run
– Statement(s) – Below the while condition check are the statements which will be performed whilst the condition is true
WHILE LOOP PRACTICE
You are now going to practice the following programs;– Countdown from 50 and show the current number you are
on– Count up to 50 and show the current number you are on
PROBLEM
Produce a program that will check if variable (password) matches the string entered by a user. Until the password and the guess match, show an error message but if they do match show a message that tells the user this.
– password equals a specific value– While true– Ask a user to enter a guess and store this in a variable– If the guess equals the password show a message and
break– Else show an error message
SOLUTION
GUESS THE NUMBER DESIGN
You must now produce a quick design in pairs, this design must be in Pseudo Code or in a flow diagram. Consider the following;
– What does the program need to do overall– What values does the program need to store– How does the program know if the use guessed correct or
incorrect– How does the program know when to finish or loop over
again and allow the user to guess again
PSEUDO CODEYour design in Pseudo code should look something like the following;• Import random• number = random.randint(0,10)• while True
Guess = Ask user to guess numberIf Guess equal to Number
You guessed correctlyEnd loop
elseYou guessed incorrectly
SOLUTION
EBI
How could you make your guess the number program even better?
– Tell the user if the number they guessed was too low– Tell the user if the number they guessed was too high– Let the user pick the range of numbers that the
random number can be generated from– Keep count of how many times the user has guessed
SOLUTION
LISTS IN PYTHON
A list is a type of variable that can store more than one item. I would describe the characteristics of a list as;
– A variable that uses brackets [ ] – A variable that can store more than one value– A variable that stores the values in a number list– The list always starts at position zero and not 1
LIST METHODS
• shoppingList = [“Bread” , “Milk” , “Jam”]– This will add three values to single variable, also known as a list
• print(shoppingList[0])– This will print the first item in the shoppingList, that should be Bread
• print(shoppingList[1:3])– This will print the items in the range of position 1 and 2 only
• shoppingList[0] = “Brown Bread”– This will change/overwrite the value stored in position 0 from Bread to Brown Bread
LIST METHODS• shoppingList.append(“Rice Pudding”)
– This will add the value Rice Pudding to the end of the list, position 3
• Del shoppingList[0]– If I buy the Brown Bread and want to remove it from my list I can do using the above
statement, this statement only allows you to remove by position
• shoppingList.remove(“Brown Bread”)– This will do a very similar job to above but will delete by the value and not position
of an item in the list
• shoppingList.sort()– This will sort the items in the shopping list into ascending order e.g. A-Z or lowest to
highest
DICTIONARIES
A dictionary is type of variable and very similar to a list but it has two very important differences;
– We use Braces { and Brackets [ like we did in a list– A dictionary contains a key and value, the key is the item
we can search for and the value will be shown
Key Value
2010001 Dave Smith
2010002 Kirsty Graham
2010003 Daniel Taylor
DICTIONARY METHODS
• users = {“2010001” : “Dave Smith” , “2010002” : “Kirsty Graham”}– This will produce a dictionary called users– The dictionary will store two items– The keys for the items are 2010001 and 2010002– The values for the keys are “Dave Smith” and “Kirsty Graham”– Two add an entry you use a a key and value with a colon showing the separation;– “I am the key” : “I am the value of the key”
• print(users[“2010001”])– This will search for the key within the dictionary and print out the value, this should
be Dave Smith from the above entries• del users[“2010001”]
– This will delete Dave Smith from the dictionary
• Users[“2010002”] = “Helen Baxter”– This will update/overwrite the value stored for the key 2010002, so Kirsty Graham will
change to Helen Baxter
• Users.get(“2010002”)– This will get the value for the key 2010002, this should now be Helen Baxter
• Users[“2010004”] = “John Wilson”– This will add John Wilson to the dictionary with the key 2010004
DICTIONARY METHODS
TASKYour task is to complete an address book or contacts list as you may know it. The contacts list must do the following;
– Show a list of options for the user to select, the list should be;1. To add a new contact2. To see the phone number of a contact3. To update the number of a contact4. To remove a contact
– You may need to make use of;• A python dictionary• A range of variables• A set of IF, ELIF and ELSE statements
Q&A
?
Thank You
Email: [email protected]: 01204 333 000Twitter: @TheRealMrPearce