Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.

Similar presentations


Presentation on theme: "Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3."— Presentation transcript:

1 Python Lesson 1 1

2 Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3 A4 divided by B4

3 Objective of the lesson Write simple programs using Python. All of you will: – Create simple code including the input and print scripts. Most of you will: – Create variables and declare the data types for variables. Some of you will: – Use operators to effectively create programs to solve a number of scenarios. 3

4 Everybody should complete Tasks 1 to 3 to download Python and create the program to email to your teacher. Some of you may also want to get the extra marks by completing the extension activity. This homework is due in next lesson. Make sure you have written your homework clearly in your planner. 4 Homework

5 Python Python is a programming language which was developed to help you learn programming. You will learn about the basics of programming using Python. To use Python load Python (IDLE). 5

6 The Python Shell Shell The first screen you see is called the “Shell”. You can type in simple commands straight into the shell. 6

7 Typing into the Shell Type in 4+2 at the >>> prompt and hit your Enter key. What happens? You can type in calculations into the Shell exactly as you would normally. What symbols would you use for multiply and divide? 7

8 Typing calculations into the shell Try typing the following calculations into the shell: – 7 take away 3 – 4 multiplied by 3 – 10 divided by 3 – 2 add 4 then multiply the total by 5 (remember how you can specify the order of calculation by using brackets) – 12345 take away 321 and then divide by 2 8

9 Creating a simple program If you want to create a program that has more than one line of code in it then you will need to use a new window to do this. File New Window Click on the File menu and select New Window. 9

10 Type into the new window Type the following into the new window (it is case sensitive): What do you think this code will do? Hello.py Save the code as Hello.py (It is important to add the.py at the end as your code will not work without it). F5 To run the code in the Shell hit the F5 key on your keyboard. 10

11 Change your code Change your code to the following: 11 Input is used to tell Python that you want the user to enter a response Variable Name tells Python that you want it to remember the answer you type in and call that “Firstname”

12 Change your code 12 {0} is known as a placeholder. This tells Python that you want to put something in this place..format(Variable Name) tells Python what you want to put into the placeholder

13 Change your code 13 Be careful: it is case sensitive (input and print should NOT have capital letters) Save and run your code (F5) to try it out

14 Keywords 14 WordWhat this does print This will show whatever you want in the Python Shell input This will ask the user a question in the Python Shell and expect a response variable These are pieces of data that Python needs to remember for later use placeholder This tells Python that you want to insert something (e.g. the data stored in the variable)

15 What will this code do? Try it out. You can use more than one placeholder as shown below: Placeholders are numbered {0}, {1}, {2} … etc in the order that the variable names appear in the format brackets. 15

16 Data types Try entering the following: When you run the script it will not work as you may expect. Try to work out what it is actually doing. 16

17 Data Types When the user inputs data it is automatically classed as text, even if they only enter a number. This is known as a string. In Python, if you want to treat the value as a number you will need to specify the data type when you create the variable. 17

18 Declaring a data type What is different in this line of code? 18 int( tells Python that the input is going to be treated as an integer (number). ) closes the “int” bracket

19 Change your code Change the code you have just created so that it treats both inputs as a number. Run the code again and you should see that it now works. 19

20 Operators OperatorWhat it meansExample + Addition >>> 4+5 >>> 9 - Subtract >>> 5 – 1 >>> 4 * Multiply >>> 7 * 3 >>> 21 / Divide >>> 7/4 >>> 1.75 // Whole number division >>> 20 // 3 >>> 6 % Remainder after whole number division >>> 20 % 3 >>> 2 20

21 Python Exercises Work through the worksheet creating the programs you are asked to. The operator table at the top of the page will help you complete the programs. Make sure you save them with sensible names. 21


Download ppt "Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3."

Similar presentations


Ads by Google