160 likes | 176 Views
This lesson covers the basics of working with numbers in Python, including storing numbers, performing mathematical operations, and using input commands. Students will learn about the difference between integers and floats and how to convert between them. The lesson includes practical exercises and a recap of key concepts.
E N D
Bell work str chr print 1. What is the name of the programming language that we started to learn last lesson? 2. Which of these commands did we learn last lesson: ord float input int
Objectives To be able to write a program that works with inputs and numbers.
Starter We are learning to use Python. Open the starter from the lesson 2 folder and drag the pieces of code into the correct order..
Task 1 – working with numbers in Python Open PyScripter… If you type in a sum then Python can do the maths for you. Try these sums – what happens? print(3+3) print(4*25) print(10/5) print(2-10)
Task 1a – storing numbers Last week we used the input command to ask the user to enter data. We’re going to use this again to work with numbers. What do you think this code will do? Type this in: numberOne = input(“Enter a number between 0 and 10 ") numberTwo = input(“Enter a number between 0 and 10 ") print (numberOne + numberTwo) Press the RUN icon. What happened and why do you think it happened?
Task 1a – storing numbers You should have seen this in the compiler window. It doesn’t add the two numbers together because Python thinks you are inputting text unless we tell it otherwise.
Task 1b – storing numbers Change what you just typed in to this numberOne = input(“Enter your name") numberTwo = input(“Enter a friend’s name") print (numberOne + numberTwo) Press the RUN icon. What happened and why do you think it happened?
Task 1b – storing numbers You should have seen the two names joined together. What does this mean?
Task 2a – storing numbers Last week we used the input command to ask the user to enter data. We’re going to use this again to work with numbers. What do you think this code will do? Type this in: numberOne = int(input(“Enter a number between 0 and 10 ")) numberTwo = int(input(“Enter a number between 0 and 10 ")) print (numberOne + numberTwo) Press the RUN icon. What happened and why do you think it happened?
Task 2a – storing numbers I typed in 5 and 5 with this code and now it added them together. What does int do? What happens if you type in 1.5 and 5.5 as your numbers when Python asks you to enter them?
Task 2b – storing numbers We know from the last slide that int will not allow us to type in decimal numbers. Let’s change our code and replace int with float. Type this in: numberOne = float(input(“Enter a no. between 0 and 10 ")) numberTwo = float(input(“Enter a no. between 0 and 10 ")) print (numberOne + numberTwo) Press the RUN icon. Type in 2 and 5.5 as your numbers when it asks you to enter data.
Task 2b – storing numbers I typed in 2 and 5.5 with this code and now it added decimal numbers together. What is the difference between int and float?
RECAP! RECAP! RECAP! What does this button do? What do these do? Why would we change int to float?
In your booklets… Complete the task on page 7 of your booklet – build a calorie counter.
1. What 2 commands have we used to store numbers today? 2. What command do you type in to get the user to input a number? 3. What is the variable in this: StudentName = input(“Enter name”) 4. What is wrong with this statement: X = input(int(“Please enter a number”) 5. Is a decimal or whole number stored in an int? 6. Is a decimal or whole number stored in a float? Plenary