Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.

Similar presentations


Presentation on theme: "Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types."— Presentation transcript:

1 Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types like strings and integer To be able to: Setup variables and allow a user to create input to a program

2 Key Words IDLE FunctionProgramCode Starter Task: You are learning to program. What is programming and why is it important to learn how to program? Answer your questions on a word document and discuss your ideas. Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types like strings and integer To be able to: Setup variables and allow a user to create input to a program

3 Why should we learn to program?

4 Key Words IDLE Function ProgramCode The Python IDLE Load It up – you should see a screen like the one shown – this is the Python Shell IDLE stands for “Integrated Development Environment” – it is the place where a set of tools have been created that allow us to code. Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program

5 Key Words IDLE Function ProgramCode The Python IDLE Type in the following print(“Hello World”) Then press enter Hello World You have just (re)written your first Python program – well done Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program

6 Learning Objectives To understand: The Python IDLE and how it is used To be able to: Use the IDLE to create a simple program and understand how it worked Key Words IDLE Function ProgramCode Task 1 Try the following programs Print (“Hello World”) PRINT (“Hello World”) print (“Hello World’) print (“Hello World” Why do you think these do not work?

7 Key Words IDLE Function ProgramCode Syntax Highlighting Special Python words such as Print are displayed in Purple Strings like “Hello World” are displayed in Green The output or program result – Hello World is displayed in Blue Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program

8 Key Words IDLE Function ProgramCode Python Files When you create a program directly in the Python shell it can be only be used once. You should always create separate Python files that can be saved for use later. Open the editor/script by creating a new file Learning Objectives To understand that variables are a named location to store data To be able to: Setup variables and allow a user to create input to a program

9 Key Words IDLE Function ProgramCode Adding Comments To make your program easier to understand you may wish to add comments to a program - this is easy, you simply use the # key print(“Hello World”) # prints onto screen You will notice that your comments are displayed in red – this is another example of Syntax highlighting Learning Objectives To understand that variables are a named location to store data To be able to: Setup variables and allow a user to create input to a program

10 Key Words IDLE Function ProgramCode Task 3 Main Name program Create a program by open a new Python file. Print out your full name Print out your favourite colour Include a comment Save it in your Python Programming folder as 1.Full name Extension – Use the Python Option menu to configure IDLE further – have a go at the various options available Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program

11 Understanding Variables A variable is a way of labelling and accessing information from a stored location. In the example ‘name’ is the variable. ‘Larry’ is a string that has been assigned to the variable. Changing Larry changes the variable but ‘name’ remains the same. Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types like strings and integer To be able to: Setup variables and allow a user to create input to a program

12 Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program Getting User Input If we create the variable as an Input there is a lot more we can do with it. Copy the code shown into a new Python file. Save it as 1.personal greeter Extension Try to add more questions into your program to find out information about someone. Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types like strings and integer To be able to: Setup variables and allow a user to create input to a program

13 Learning Objectives To understand: Variables – how they work and are created To be able to: Setup variables and allow a user to create input to a program Extension work Line Breaks / New Lines You can also write your string over several lines by using “\n”. Like this: Try writing a few of your own! Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types like strings and integer To be able to: Setup variables and allow a user to create input to a program

14 Part 2

15 Key Words IDLE FunctionProgramCode Starter task On your post it note try to explain the what is meant by: 1.A variable 2.A String Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

16 Key Words IDLE FunctionProgramCode Starter task 1.A variable A variable is a memory location used to store data, it can be thought of as an empty box that can be filled with data, this data can change throughout the program 2.A String In programming, a string is a data type, the sequence of characters is held between “Speech marks ” or ‘Apostrophes’ and have a syntax highlight, in IDLE it is green. Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

17 Open Python > IDLE (python GUI ) Exercise 1 Try typing each of the following at the command prompt / Shell: (press Enter after each 1 ). 2+2 17-9 16/4 3*7 Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

18 Using integers Copy the code below. Save your program as 2.Adding. Can you spot what has gone wrong? Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

19 Using integers Integers are essentially whole numbers. Copy the code below. Now edit your program so that it treats the data as integers. Learning Objectives To understand that DATA can be cast into other data types “Casting” int() To be able to create Python programs that make use of integers. By putting int( at the front of the input command we are telling Python we want it treat the data as a whole number and not a String(text). For each set of brackets you have opened you must close the same amount at the of the line.

20 Using integers – Task Subtraction Edit your code to make it subtract the two numbers. Save it as “2.subtraction” and test it works. Multiplication Edit your code to make it multiply the two numbers. Save it as “multiplication” and test it works Division Edit your code to make it divide the two numbers. Save it as “division” and test it works. Mod Edit your code to make it calculate the modulus (remainder) of a division. Save it as “mod.py” and test it works. (Hint: use x%y) Square Edit your code to make it calculate x 2. Save it as “square” and test it works. Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

21 Using integers – Task 2 Order of Operations / BIDMAS · Brackets · Indices (powers) · Division and Multiplication. Start on the left and work them out in the order that you find them. · Addition and Subtraction. When only addition and subtraction are left in the sum work them out in the order you find them. starting from the left of the sum and working towards the right Try writing a program that will take a number, multiply by three and then add four. Try writing a program that will take a number, add four and then multiply by three. Put the number 7 into both programs and check that they work correctly. Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

22 Part 3 Learning Objectives To understand what a selection means in programming. To be able to create Python programs that make use of IF statements.

23 Key Words IDLE SelectionIF statementCode Selection Selection means selecting (or choosing) what to do next. Should I cycle to school, or ask for a lift? If it’s a sunny day I might cycle. If it’s raining, I’ll ask for a lift. Learning Objectives To understand what a selection means in programming. To be able to create Python programs that make use of IF statements.

24 IF... ELSE Create a new file and type in the following code, Save it as 3.IF: There are a number of key points here: Notice how the colon (:) is used to say what should happen in each case. Also notice that the indentation is VERY important. Python only knows when your IF statement is finished by looking at the indentation! Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

25 IF... ELIF... ELSE Sometimes there are more than two options: I could walk OR cycle OR get the bus OR get a lift. As well as IF and ELSE, we can stick an ‘ELSE IF’ (or ELIF) in the middle: Create a new file and type in the following code, save it 3.ELIF: Learning Objectives To understand what an integer is. To be able to create Python programs that make use of integers.

26 IF... ELIF... ELIF... ELIF... ELSE You can include an unlimited number of ELIFs if you need to. Try the following: There are a couple of important bits here: You can put a line break in your string by using “\n”. You can continue a line of code by putting a “\” at the end. If you are testing for equality, use a double equals (is 3x2 = = 6?) Make your own program that uses a menu system and give the user a response


Download ppt "Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types."

Similar presentations


Ads by Google