Python Workbook.docx
Document Details

Uploaded by ResourcefulCoralReef
Full Transcript
Working with Text (Strings) Go to: pythonsandbox.com On line 1, type the following code exactly as below: Now press the icon. You’ve written your first line of code! Now practise a little by writing some sentences of your own. Question Time… Storing Data (Variables) You can store data in python usin...
Working with Text (Strings) Go to: pythonsandbox.com On line 1, type the following code exactly as below: Now press the icon. You’ve written your first line of code! Now practise a little by writing some sentences of your own. Question Time… Storing Data (Variables) You can store data in python using Variables. A variable is like data storage container. 1. Type the following code exactly as below: 3. Well done, you have used a variable within a string! 4. Instead of ‘Peter’ change the Variable to your name. 5. Now add new variable by changing the code to the following: Question Time… Challenge Task: Now create some code which includes the following: A variable called school which will store the string “New-Bridge Integrated College” A variable called subject which will store the string “ICT” A variable called year which will store the string “Year 10” A print command that will use all the variable and say: I am a Year 10 student studying ICT at Newbridge Integrated College. Question Time… User Inputs We can also ask the user to enter data and store it as a variable. 1. Type the following code exactly as below: 2. Now press the icon. Using this method, we can easily change what the variable is storing by asking the user to enter the data. 3. Type the following code exactly as below: Working with Numbers (Integers) When coding we refer to whole numbers are integers (not numbers). If the number has a decimal, we call this a float. For example 3.14 would be a float. 1. Type the following code exactly as below: 2. Now press the icon. As you can see, we have assigned the variables with two integers and then added them together using the + symbol. Notice we didn’t use any speech marks when assigning the variables. Challenge Task: Now create some code which will have: Variable x which stores the number 12 Variable y which stores the number 2 Variable z which stores the number 6 Print to show the total of all three variables added together Extension: Try using other calculation operators. i.e Subtraction, multiplication and division. Challenge Task: Let’s create a basic calculator which can work out the area of a room. We know (should know..) that to find the area of a room we must multiply the width of the room by its length. For example, our ICT room is 5 metres wide and 6 metres long, therefore: 5 x 6 = 30. The ICT room is 30 Metres ². Let’s create a program that will do this for us: Create a variable called width and assign it 5 Create a variable called length and assign it 6 Create another variable called area that will store the value of width * length Add a print function that will say: “The total area of the room is: area metres squared” Extension Task: Change your code so the program asks the user to enter the width and length of the room. N.B. You will need to declare the variables like this: Change your code so the program can work out the area of a Triangle. Assessment: IF ELSE Statements One of the most common functions used in coding the IF and Else statement. It is very simple to use but very powerful. Type the following code exactly as below: Change the variable x to 6 and run the code again, notice nothing happens when we run the code. That because we haven’t told it to do anything. Let’s fix that by adding the ELSE function to our code: Now if we run the code you will get a message saying it is either equal to or not. Challenge Task: Write a program that has the following code: A variable called X which has the value 11 A variable called Y which has the value of 7 Add an IF statement that checks to see IF Variable X is bigger than variable Y. If X is bigger than Y, it should say “X is bigger than Y” , otherwise it should say “X is not bigger than Y” Extension Task: Practise using each of the different IF Statement Operators from the list above. Create a new program that: Asks the user to answer a quiz question i.e What is the capital of France etc… The answer should be stored in a variable called answer. Create an IF statement that checks if the answer is Correct or Incorrect. Challenge Task (Login): For this task we are going to create a program that will ask the user to enter a Password. We will then let the program decide if the user is allowed to login or not based on the password entered. Create a variable called password which asks the user to enter a password. Create an IF Statement that check whether the password is correct. The correct password should be: python123 If the password matches, the program should say: Login successful – Welcome If the password doesn’t match the program should say: Login Failed- Try again Challenge Task Create a variable called username which asks the user to enter their username. Change the IF Statement so it checks both the username and password. The correct login details should be: Username: newb Password: python123 Nested IF A Nested IF can be used if we want to check lots of different possibilities. In this task we are going to create our very own calculator! Type out the following code exactly as shown: Tracking Assessment: You must create a program that will work out a student’s average exam score across 3 subjects. The program should: Ask the students to enter their name. Their name will be stored in a variable called forename. They will then be asked to enter the first exam score in a variable called score1 They will then be asked to enter the second exam score in a variable called score2 Lastly, they will then be asked to enter the third exam score in a variable called score3 The program will then calculate their average score and store it in a variable called average_score. Finally the following message will be displayed Hello [forename], your average exam score is: [average_score] Challenge Task: Have the program identify which Exam score was highest i.e “You performed best in Exam 2” Have the program convert the average_score to a grade were: A=80+ B=70-79 C=55-69 D=40-54 E=0-39