Presentation is loading. Please wait.

Presentation is loading. Please wait.

Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson.

Similar presentations


Presentation on theme: "Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson."— Presentation transcript:

1 Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson 3 Mathematical functions »Lesson 4 ErrorsLesson 4 Errors »Lesson 5 VariablesLesson 5 Variables »Lesson 6 StringsLesson 6 Strings »Lesson 7 Relational operatorsLesson 7 Relational operators »Lesson 8 using ELIFLesson 8 using ELIF

2 Lesson 1

3 Programming screen Activity 1.1- Write a program to print out “Hello World” Python Shell Did you have to correct any errors? If so, what were they? Common mistakes include missing the parenthesis and capitalising the P in print.

4 Programming screen Activity 1.2- Write a program that writes your name on the screen Python Shell Did you have to correct any errors? If so, what were they? Common mistakes include missing the parenthesis and inverted commas. The +name must also be outside of the inverted commas.

5 Activity 1.3 practice- Copy the following code into python

6 Programming screen Activity 1.3- Write a program that prints your name six times Python Shell Did you have to correct any errors? If so, what were they?

7 Lesson 2

8 Activity 2.1- True or false Python was released in 2010false Python was named after the TV series “Monty Python’s Flying Circus” True Python is proprietary software which is expensive to buy false Python was written by Bill Gatesfalse YouTube is written in PythonTrue Are the below statement True or False? Highlight the correct answers!

9 Activity 2.2- Keyboard shortcuts Make a note of what these useful keyboard shortcuts do! Keyboard shortcut What does the keyboard shortcut do? Control c Copy selected text Control v Paste Control a Select all Control x Cut the selected text Control f Find Control n Opens a new window Control p Prints Control s Saves Control z Undo

10 Activity 2.3 practice- Copy the following code into python

11 Programming screen Activity 2.3- Write a program that writes four lines of the lyrics of your favourite song on the screen Python Shell Did you have to correct any errors? If so, what were they?

12 Activity 2.4 – Copy and run this program Python Shell 1.What happens? The program prints the words the rain falls from the sky. 2.What effect does the ”,“ have? There is one white space in the place of the comma.

13 Escape sequences can be used to alter how the information is displayed on the screen. An escape sequence is a back slash “\”. Experiment with these escape sequences and complete the table. Hint: Use alt p to display the last command entered in the IDLE shell Activity 2.5 – Escape sequences Escape sequenceEffect \t Tab \n New line \\ Displays \ \’ Displays ‘ \” Displays “

14 Programming screen Activity 2.6- Print this text using one line of code Did you have to correct any errors? If so, what were they? Common errors include placing a space after the \n and using the wrong forward slash e.g. /.

15 Activity 2.7- Write a program using print commands to display your initials five characters high on the screen. For example:

16 Programming screen Activity 2.7- Write a program using print commands to display your initials five characters high on the screen. Did you have to correct any errors? If so, what were they? Python Shell

17 A Activity 2.8- An algorithm is a sequence of steps needed to perform a particular task. Algorithms can be represented in different ways. Match the words to the representations on the next three slides. Program code

18 B RECEIVE myName FROM (STRING) KEYBOARD RECEIVE myAge FROM (INTEGER) KEYBOARD SET AgeInTen TOmyAge + 10 SEND myName “will be” AgeInTen “in 10 years time” TO DISPLAY C Write a program that prompts the user to enter their name and age. The program then adds 10 onto the age and displays the text “ will be in ten years time.” Match the words to the representations. Pseudocode Written description

19 E D Match the words to the representations. If animal has 4 legs then If animal has a tail then If animal answers to “puss” then animal = cat endif Flowchart Structured English

20 Activity 2.9- Explore ASCII art (Wikipedia) and write a Python program to display your ASCII art creation. For example:

21 Programming screen Activity 2.9- Explore ASCII art (Wikipedia) and write a Python program to display your ASCII art creation. Python Shell Did you have to correct any errors? If so, what were they?

22 Lesson 3

23 Activity 3.1- Copy and run these lines of code. Complete the table to explain what the mathematical operators do. >>> 60/5 >>> 987+34 >>> 564*89 >>> 2**5 >>> 43-5 >>> 11//2 >>> 11%2 Mathematical operator symbol Operation /Divide +add *multiply **exponential -subtract //Integer division %Modulus (Remainder after the division)

24 Activity 3.2- Make up some mathematical calculations of your own and add an example to the table for each mathematical operator. Mathematical operator symbol ExampleAnswer / 10/2 5 +10+212 *10*220 **10**2100 -10-28 //10//25 %10%20

25 Programming screen Activity 3.3.1- Write a program to display this text on the screen and fill in the missing number. 8 cats have 4 legs each The cats have ___ legs in total Python Shell

26 Programming screen Activity 3.3.2- Write a program to display this text on the screen and fill in the missing number. A farmer with 1089 sheep sells 56 of them The farmer has _____ sheep left Python Shell

27 Programming screen Activity 3.3.3- Write a program to display this text on the screen and fill in the missing number. 4 children pick 56 flowers each The children each have ____ flowers Python Shell

28 Programming screen Activity 3.4- Copy and run these lines of code. What effect do the parentheses () have? The parentheses control the order in which the numbers are calculated. Anything in parentheses is evaluated first. Python Shell

29 Activity 3.5- Predict what you think will be displayed on the screen when this line of code is executed. Prediction: 24.5 Now copy and run the code. Explain the answer you get. Is it what you predicted? Multiplication and division have higher precedence and take place first from left to right. First the division: 7.5 * 3 + 2 Then the multiplication giving: 22.5 + 2 Then the addition giving: 24.5

30 Activity 3.6- Make up some multiple-choice questions on precedence. Each question must have four possible answers, one of which is correct. Check your answers are correct by using the interactive Python shell. Try your questions out on other people and document this using the template on the next slide. You must be able to explain to them why the answers are correct.

31 Question Activity 3.6- Make up some multiple-choice questions on precedence. Can you explain how you got the outcome you did? Outcome

32 Activity 3.7- Devise your own mnemonic for remembering the order of precedence. Python order of precedenceMnemonic Parenthesis (brackets) Exponential Division and multiplication Addition and subtraction

33 Lesson 4

34 Activity 4.1 - Copy the following code into python Explain the results SyntaxError: invalid syntax In computer science ‘syntax’ means the set of rules that describes how the language is constructed. A syntax error means that Python does not know what to do because the language used does not follow the correct rules. It does not print hello world as it is not within parenthesis or inverted commas. Programming screenPython Shell

35 Activity 4.2 - Copy the following code into python Explain the results – It prints hello world as it is within parenthesis or inverted commas. Programming screen|Python Shell

36 Activity 4.3- Python produces different types of error. Match up the type of error with its description. Description of the error Dividing a number by zero. When a program is interrupted from the keyboard by pressing control+c. When a name is used that is not known about (often a variable name that is misspelt). When an operation is attempt that is invalid for that type of data. An error occurs when the program is running. Type of error TypeError RuntimeError NameError ZeroDivisionError KeyBoardInterrupt Hint: A full list of Python errors can be found here: http://www.t utorialspoin t.com/python /standard_ex ceptions.htm http://www.t utorialspoin t.com/python /standard_ex ceptions.htm

37 Activity 4.4 - Copy the following code into python Explain the results Nothing happens as the # shows that this is a comment statement which the Python program ignores. Always use comments to make your programs readable. Include your name and date and an explanation of what the program does at the beginning of every program. Programming screenPython Shell

38 Activity 4.5 - Copy the following code into python Use Copy this code into the file. Save the file using file/save. Always give the filename an extension of.py. Use a meaningful filename and store in a suitable folder. Run the commands by using run/run module Close Python and then restart Python, open the file and run the program again.

39 Activity 4.6 - Copy the following code into python Following the same steps as in Activity 4.5, write a program that displays your name and then your age. Hint: Use file/new window and save the file using file/save. Don’t forget to use a meaningful filename Programming screen Python Shell

40 Activity 4.7 – Using the python IDLE Write your own summary sheet to describe how to run Python programs, including how to save and open.py files. Include any other commands that you have found useful. Using the Python IDLE It helps to avoid feature clutter How to open a new window Open up a fresh shell to begin typing up new code How to save a file Ctrl + S How to open a file Open a file that has been saved but it will only work if it is saved as a.py file. How to run a program f5 Useful tips alt pDisplays the last line you entered Commands in the file are not colour coded Save as a.py file

41 Lesson 5

42 Activity 5.1 - Where does the name ‘Boolean’ come from? Boolean variables are named after George Boole who invented the mathematics of digital logic. Find a picture of George Boole and insert it here. When was he born? He was born on November 2 nd, 1815 in Lincoln..

43 Activity 5.2 - Type commands Use the “type” function to find out the data types for these values. Explain the results It tells you what data type they are. Programming screen| Python Shell

44 Activity 5.3 – Type commands The type function returns the data type of an expression. For each of these expressions, first predict the data type then use the type command to see if your prediction was correct. ExpressionPredicted data typeType commandResult “hello world” type("hello world") string False type(False) boolean 15 type(15) integer 35.6 type(35.6) float -999 type(-999) integer “15” type(“15”) integer “False” type(“False”) string True type(True) boolean 0.001 type(0.001) float

45 Activity 5.4 - Variables The interactive shell is a useful place to explore variables. Copy and run this code into python. Create a variable called myName and assign it to your name. Create a variable called myAge and assign it to your age Create a variable called myEyes and assign it to your eye colour. Create a variable called myHeight and assign it to your height. Write the commands to display on the screen your name, age, eye colour and height. Evidence both python shells the next slide Hint: Remember that a single ‘=’ is used to assign a value to a variable.

46 Activity 5.4 - Variables Explain the results I created the variables and then printed them in a sentence. Programming screenPython Shell

47 Activity 5.5 – Variable names A programmer is trying to decide on a valid name for a variable that represents a house number. Which of the following variable assignments are valid? Why are the others not valid? Valid or invalid variable name? Reason why not valid 8HouseNumber = 288Invalid Variable names must begin with a letter houseNumber = 288Valid house Number = 288Invalid Variable names cannot have spaces in them house_number = 288Valid “_” is a valid character in a variable name import = 288Invalid Import is a Python command What type of error do you get when using an invalid variable name? Invalid syntax

48 Activity 5.6- Type commands Copy and run this code and explain the result. Amend the program to add another variable called numberThree. Assign the value 76 to this variable. The program should all up all three numbers and print the result. Evidence both python shells the next slide

49 Activity 5.6 - Variables Explain the results The program adds the two numbers together and displays the answer. Programming screenPython Shell

50 Activity 5.7 – Data types in Python Complete this table to describe the four data types. Data type Python abbreviat ion ExplanationExample integerint A whole number 45 stringstr A sequence of characters which can include letters, spaces and other characters ”Have a nice day” float A number with a fractional part 16.76 booleanbool Boolean or logical data can only have one of two values. In Python they are either True or False True or False Hint: Quotation marks are used to show the start and end point of a string, e.g. “this is a string”.

51 Lesson 6

52 Activity 6.1 – Input function The input function allows the user to input a value and assign it to a variable. Copy and run this program. Re-run the program several times, entering different names each time. Hint: Quotation marks are used to show the start and end point of a string, e.g. “this is a string”.

53 Activity 6.1.1 - Input function Write a program that asks the user for their name and favourite food and displays these on the screen. Did you have to correct any errors? If so, what were they? Programming screen| Python Shell

54 Activity 6.2 – Int function The input function allows the user to input a value and assign it to a variable. Copy and run this program. Explain why It doesn't work The program cannot add the number 10 to a string, which is what the user is asked to input in this code.

55 Activity 6.2.1 – Int function Explain the results I Used the int function to convert from a string to an integer if you want to input whole numbers. Programming screen Python Shell Correct the program from the previous page and display below.

56 Activity 6.3 – Int function Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter a number then displays the number doubled.

57 Activity 6.4 – String formatting The string method.format gives you more control over the formatting of your output than printing using space-separated values. The string formatting commands are given in curly brackets {}. Copy the following programs into python and explain what happens and why. CodeWhat happens and why? foodOne="fish" foodTwo="chips" print("{0} and {1}".format(foodOne,foodTwo)) The program prints fish and chips. foodOne="fish" foodTwo="chips” print("{1} and {0}".format(foodOne,foodTwo)) The program prints chips and fish. foodOne="fish" foodTwo="chips" print("{1} {1} {1} and {0} {0} {0}".format(foodOne,foodTwo)) The program prints chips chips chips and fish fish fish.

58 Activity 6.4.1 – String formatting Create these variables: one = “cheese” two =”onion” Use the.format command to display: My favourite crisps are cheese and onion. I love them! cheese and onion and cheese and onion and cheese and onion cheesecheesecheese and oniononiononion You guessed it. The best crisps are onion and … cheese. Display your code on the next slide…

59 Activity 6.4.1 – String formatting Did you have to correct any errors? If so, what were they? The.format method is about formatting strings so the.format fields {0} must be a string. The format field {0} placeholders are replaced (in order) by the values given in.format brackets. The values in the.format brackets are separated by commas. Programming screen Python Shell

60 Activity 6.5 – Formatting numbers You can use the.format method to format the number of decimal places. Copy the following programs into python add the answer and explain what happens and why. CodeWhat happens and why? number = 98.1468297645 print("The answer is {0:.5f}".format(number)) The answer is 98.14682 It prints 98.14682 because the field format has requested the number is printed to 5 decimal places. number = 98.1468297645 print("The answer is {0:.4f}".format(number)) The answer is 98.1468 It prints 98.1468 because the field format has requested the number is printed to 4 decimal places. number = 98.1468297645 print("The answer is {0:.3f}".format(number)) The answer is 98.1468 It prints 98.146 because the field format has requested the number is printed to 3 decimal places. number = 98.1468297645 print("The answer is {0:.1f}".format(number)) The answer is 98.1 It prints 98.1 because the field format has requested the number is printed to 1 decimal places. number = 98.1468297645 print("The answer is {0:.0f}".format(number)) The answer is 98 It prints 98 because the field format has requested the number is printed to 0 decimal places.

61 Activity 6.5.1 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number 765.87641987 to a variable and display the number with 5 decimal places using the.format command.

62 Activity 6.5.2 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number 765.87641987 to a variable and display the number with 2 decimal places using the.format command.

63 Activity 6.5.3 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number 765.87641987 to a variable and display the number with no decimal places using the.format command.

64 Activity 6.6 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks how much your bill is at a restaurant and then asks you to enter the % you want to give in a tip. The program should display the amount of tip to give to the waiter.

65 Activity 6.6.1 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Amend the program so that it also displays the total cost of the meal including the tip.

66 Activity 6.7 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that displays the square of a number.

67 Activity 6.7.1 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program to find the perimeter of a rectangle.

68 Activity 6.7.2 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that finds the area of a square.

69 Lesson 7

70 Activity 7.1 – Relational operators Password checking is an example of a relational operator. If the password entered is the same as the password stored then the condition is true. The operator is ‘is equal to’. Brainstorm other examples of condition statements and decide what the operator is. Relation statementOperator Entered password equal to saved passwordEqual to People more than 18 years old can voteGreater than Temperature less than zero degrees water freezes Less than Cost of tickets for a concert greater than £30 I cannot afford to go Greater than Temperature in the room greater than 23 degrees – open the window Greater than Exam result greater than or equal to 75% get an A Greater than or equal to Hint: Greater than is an example of an operator

71 Activity 7.2 – Relational operators Complete this table of the Python relational operators. Give an example of each and say whether it will evaluate to true or false. Hint: Greater than is an example of an operator Relational operator OperatorExampleEvaluates to Equal to==“fred” == “sid”False Not equal to!=8 != 8False Greater than>10 > 2True Greater than or equal to >=5 >= 5True Less than<40 < 34False Less than or equal to <=2 < = 109True

72 Activity 7.3 – Greater than and less than Find a way that works for you of remembering the difference between “less than”. Hint: As there are only two options you only need to learn one! Operator>< Operator meaningGreater thanLess than How I remember thisThe number is at the largest part of the operator. The number is at the smallest part of the operator.

73 Activity 7.4 – Operators Programming screenPython Shell This program asks if it is snowing and if so tells you to take a sledge otherwise to have a good day. The condition is missing. Copy and complete the condition to make the program work correctly.

74 Activity 7.5 – Operators What condition is needed in this program to display ‘pass’ if the exam mark is 40 or more?

75 Activity 7.6 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter the colour of the light at a pedestrian crossing. If the light is green it tells you it is safe to cross, otherwise it tells you to stop.

76 Activity 7.7 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you for your password. It then asks you to re- enter your password. If they are the same the message “access granted” is displayed. If the passwords are not the same the message “access denied” is displayed.

77 Lesson 8

78 Activity 8.1– Using elif This program simulates a fortune cookie. A random number is used to decide your ‘fortune’. Copy and run this program. # a random number is given by the randint() function import random answer= random.randint(1,6) if answer == 1: print("You will make a new friend this week") elif answer == 2: print("You will do well in your GCSEs") elif answer == 3: print("You will find something you thought you’d lost") The program is not yet complete. Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide.

79 Activity 8.1.1– Using elif Programming screen Python Shell Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide. Note: random.randint(1,6) is a function that returns a random number between 1 and 6. The ‘import random’ command allows the program to access the random.randint() function.

80 Activity 8.2– Writing readable code: a style guide Program code is read more often that it is written. Here are some guidelines on how to write Python code so that it is easy to read. A style guide for Python code Indent by four spaces for each indentation level. Use blank lines to separate different parts of the program. Use two blank lines to separate functions. Choose meaningful names for variables, using CamelCase or with words separated by underscores. Put imports at the top of the file. Include one space around each side of an assignment and other operators. Use complete sentences with the first word capitalised for comments. Write comments that add clarity and explain what the program does. Do not simply repeat what the code already says. Write function names in lowercase, with words separated by underscores. Use meaningful function names which describe the purpose of the function. Write constants in CAPITAL_LETTERS. Use meaningful constant names, which describe the purpose of the constant.

81 Activity 8.2.1 – Writing readable code Did you have to correct any errors? If so, what were they? def a(s): if s<50: print("You have lost") else: print("You have won") Print screen of amended code using the style guide Apply the rules in the style guide above for this Python code.

82 Activity 6.6.1 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Amend the program so that it also displays the total cost of the meal including the tip.

83 Activity 6.7 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that displays the square of a number.

84 Activity 6.7.1 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program to find the perimeter of a rectangle.

85 Activity 6.7.2 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that finds the area of a square.

86 Lesson 7

87 Activity 7.1 – Relational operators Password checking is an example of a relational operator. If the password entered is the same as the password stored then the condition is true. The operator is ‘is equal to’. Brainstorm other examples of condition statements and decide what the operator is. Relation statementOperator Entered password equal to saved passwordIs equal to People more than 18 years old can voteGreater than Temperature less than zero degrees water freezes Less than Cost of tickets for a concert greater than £30 I cannot afford to go Greater than Temperature in the room greater than 23 degrees – open the window Greater than Exam result greater than or equal to 75% get an A Greater than or is equal to Hint: Greater than is an example of an operator

88 Activity 7.2 – Relational operators Complete this table of the Python relational operators. Give an example of each and say whether it will evaluate to true or false. Hint: Greater than is an example of an operator Relational operator OperatorExampleEvaluates to Equal to==“fred” == “sid”False Not equal to!=/<>“fred” != “sid”True Greater than>1>2False Greater than or equal to >=2.5 >= 5 / 2True Less than<5<10True Less than or equal to <=6<=10True

89 Activity 7.3 – Greater than and less than Find a way that works for you of remembering the difference between “less than”. Hint: As there are only two options you only need to learn one! Operator>< Operator meaningGreater thanLess than How I remember thisMouth points to biggest number

90 Activity 7.4 – Operators Programming screenPython Shell This program asks if it is snowing and if so tells you to take a sledge otherwise to have a good day. The condition is missing. Copy and complete the condition to make the program work correctly. Having two variables here doesn’t always work

91 Activity 7.5 – Operators What condition is needed in this program to display ‘pass’ if the exam mark is 40 or more? If mark >= 40:

92 Activity 7.6 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter the colour of the light at a pedestrian crossing. If the light is green it tells you it is safe to cross, otherwise it tells you to stop.

93 Activity 7.7 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you for your password. It then asks you to re- enter your password. If they are the same the message “access granted” is displayed. If the passwords are not the same the message “access denied” is displayed.

94 Lesson 8

95 Activity 8.1– Using elif This program simulates a fortune cookie. A random number is used to decide your ‘fortune’. Copy and run this program. # a random number is given by the randint() function import random answer= random.randint(1,6) if answer == 1: print("You will make a new friend this week") elif answer == 2: print("You will do well in your GCSEs") elif answer == 3: print("You will find something you thought you’d lost") The program is not yet complete. Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide.

96 Activity 8.1.1– Using elif Programming screen Python Shell Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide. Note: random.randint(1,6) is a function that returns a random number between 1 and 6. The ‘import random’ command allows the program to access the random.randint() function.

97 Activity 8.2– Writing readable code: a style guide Program code is read more often that it is written. Here are some guidelines on how to write Python code so that it is easy to read. A style guide for Python code Indent by four spaces for each indentation level. Use blank lines to separate different parts of the program. Use two blank lines to separate functions. Choose meaningful names for variables, using CamelCase or with words separated by underscores. Put imports at the top of the file. Include one space around each side of an assignment and other operators. Use complete sentences with the first word capitalised for comments. Write comments that add clarity and explain what the program does. Do not simply repeat what the code already says. Write function names in lowercase, with words separated by underscores. Use meaningful function names which describe the purpose of the function. Write constants in CAPITAL_LETTERS. Use meaningful constant names, which describe the purpose of the constant.

98 Activity 8.2.1 – Operators Did you have to correct any errors? If so, what were they? def a(s): if s<50: print("You have lost") else: print("You have won") Print screen of amended code using the style guide Apply the rules in the style guide above for this Python code.


Download ppt "Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson."

Similar presentations


Ads by Google