Learning to Program in Python

Slides:



Advertisements
Similar presentations
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Advertisements

Data Structures Introduction. What is data? (Latin) Plural of datum = something given.
Introduction to a Programming Environment
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
By the end of this session you should be able to...
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Variables and Math in Code. Variables A variable is a storage block for information in your program “A” “A” Computer Program Memory Computer Program.
Python Programming Using Variables and input. Objectives We’re learning to use basic knowledge of variables combined with user input. Outcomes Continue.
Introduction to Python Lesson 1 First Program. Learning Outcomes In this lesson the student will: 1.Learn some important facts about PC’s 2.Learn how.
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.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Getting Started With Python Brendan Routledge
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Introduction to Python Lesson 2a Print and Types.
Python Fundamentals: Hello World! Eric Shook Department of Geography Kent State University.
Input, Output and Variables GCSE Computer Science – Python.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Foundations of Programming: Java
Lesson 03: Variables and Types
CST 1101 Problem Solving Using Computers
GCSE COMPUTER SCIENCE Practical Programming using Python
Introduction to Computing Science and Programming I
Topic: Python’s building blocks -> Variables, Values, and Types
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Lesson 1 - Sequencing.
Introduction to Computer Science / Procedural – 67130
Introduction to Python Data Types and Variables
IF statements.
Unit 2 Smarter Programming.
A Level Computing Component 2
Learning to Program in Python
Math in C The math blocks you've used in Scratch can all be recreated in C!
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Learning to Program in Python
Learning to Program in Python
IDENTIFIERS CSC 111.
Computer Science and an introduction to Pascal
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Lesson 4: Controlling Memory with Variables
Fill the screen challenge!
Learning to Program in Python
Learning to Program in Python
Learning Outcomes –Lesson 4
Learning to Program in Python
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Learning to Program in Python
Learning to Program in Python
Units with – James tedder
Lesson 03: Variables and Types
Learning to Program in Python
Variables In today’s lesson we will look at: what a variable is
PYTHON: BUILDING BLOCKS Sequencing & Selection
Boolean Expressions to Make Comparisons
Introduction to Python
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Hardware is… Software is…
Introduction to Python
Python Creating a calculator.
Presentation transcript:

Learning to Program in Python Concept 1 VARIABLES Variables will have been used in the starter program. This is a full introduction to variables and data types, and a second lesson introduces operators.

Learning Intentions Assign Variables Use Data Types CAST variables From this lesson the students will be able to: Assign Variables Use Data Types CAST variables A variable is allocated some memory in which data is stored. Students will learn how to store different types of data within a named variable. The main Learning Outcomes (LO) achieved are highlighted throughout the lesson and tasks. The list is not exhaustive, and consultation with the LCCS spec is encouraged at all times throughout the implementation of the course. LO 2.16 students should be able to use data types that are common to procedural high-level languages

Previously we looked at how to write a program to ….. 1. Ask the user of your program for their age. 2. Add 100 to their age. 3. Print out their age in 100 years. A very first program / algorithm to perform a very simple task. LO 1.22 students should be able to read, write, test, and modify computer programs

Remember this first program 1. Ask the user of your program for their age. 2. Add 100 to their age. A very first program / algorithm to perform a very simple task. Lines 1 and 2 assign values to variables. 3. Print out their age in 100 years.

Assigning a Variable 1. Ask the user of your program for their age. userAge is an example of a Variable. We could have called it User_Age or ageOfStudent. It is up to the programmer. In Python, input from the user is in the form a STRING. Ask students for another suitable name for the variable. Emphasise that it must be a continuous set of letters, with no spaces allowed.

Assigning a Variable A variable is a piece of memory that stores a value. The name of the piece of memory is the name of the variable. (userAge for example) myVar = 17 The equals sign means assign. (store the value on the right (17) in the variable on the left(myVar)) Ask students for another suitable name for the variable myVar. Emphasise that it must be a continuous set of letters, with no spaces allowed.

Variables 2. Add 100 to their age. Name the 2 variables above. newAge and userAge are the 2 variables. Ask the students if we could replace the 100 with a variable and what would we call it?

Variables store Data 2. Add 100 to their age. newAge and userAge store different types of data. How do you think they are different? userAge stores data in the form of a STRING. newAge stores data in the form of an INTEGER. The variables store 2 different data types. LO 2.18 students should be able to collect, store and sort both continuous and discrete data

4 most common DATA TYPES A STRING is a data type for characters (“A” or “1”) An INTEGER is a data type for whole numbers (-2 or 45). A FLOAT is a data type for decimal numbers. (3.14 or -10.93). A BOOLEAN is a data type for True or False. The reason they are underlined is these are the built-in functions for CASTing variables to different data types. So int(x) casts x as an integer, str(c) casts c as a string, float(y) casts y as a floating number, and bool(p) casts p as a Boolean data type. LO 2.16 students should be able to use data types that are common to procedural high-level languages

type(x) gives you the DATA TYPE Students may want to type this into their IDE or reading through it may be enough.

Variables store different Types of Data 2. Add 100 to their age. Why do you think int(userAge) is needed above ? userAge is a STRING int(userAge) is an INTEGER userAge is a STRING. Casting it as an integer means we can add on 100. Then newAge will be interpreted by Python as an Integer.

Fill in the Data Type of the assigned variable - can you predict the value in each case ? - Q Code Variable Data Type var1 = 10 INTEGER 1 var2 = 3.14 FLOAT 2 myVar = var1 + var2 3 var1 = True BOOLEAN 4 myVar = “123” STRING 5 myVar = int(var2) 6 myVar = myVar + 3.14 7 myVar = str(var1) 8 ? Be Careful ? The worksheet above can be downloaded from the website, completed by the students and stored in their online space. Or it can be printed out for a written class task. The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make) The answers are revealed when the slide show is run.

Correct order but Remove INT() 1. 2. 3. Ask students to predict the error. Then students can modify their program. There are 2 KEY lessons to draw from this error. Python reads input from the user as text. Even numbers look like text/characters/strings to Python. INT() changes the input from the user to an integer. INT stand for integer. This is known as CASTING a variable. PREDICT what happens when INT() is removed around userAge on line 2, as shown above.

userAge = int(userAge) PYTHON and Data Types Python can switch data types really easily … it is known as CASTING a variable. userAge = int(userAge) What do you think this line will change with regard to the variable userAge? userAge = int(userAge) will CAST userAge as an integer, and change its data type from string to integer.

Predict what this code will print? What number will print? Encourage students to try it out in their IDE. The key is that the value in myNumber is overwritten by its original value plus 2.

This program will output 2 lines. Predict the 2 lines. Encourage students to try it out in their IDE. The key is that the value in myNumber is overwritten twice. Also the syntax of myNumber += 3 is important.

How do you know how to write correct PYTHON? Consult your Cheat Sheet Google “python 3.6 cheat sheet” Or search for Python style guides Students need to be able to find lots of their own answers.

How do you know how to write correct PYTHON or find out how to do stuff? Some of the recommended sites for writing Python…. 1. docs.python.org … tutorial 2. Runestone Academy - How to Think Like a Computer Scientist 3. Stack Overflow A resourceful student is an extra teacher and one less student.

REVISIT - FIRST PROGRAM CHALLENGE 0. Ask the user of your program for their name. 1. Ask the user for the year they were born. 2. Calculate their age this year. If the students haven’t done this previously, set it now as a task. 3. Print out a message with their name and age in the message.

2. 2018 – Google “python how to get the current year” optional CHALLENGE Write the program using only variables (The number 2018 not allowed) 0. As before .. 1. As before … 2. 2018 – Google “python how to get the current year” A solution is given below. Encourage students to find and modify the code so the program uses the current year, and never goes out of date. from datetime import datetime thisYear = datetime.now().year # check we have the right year and correct syntax print("current year", thisYear) 3. As before … but watch your variable names in line 2. LO 1.2 students should be able to explain how the power of computing enables different solutions to difficult problems

Sample Code for (enhanced) First Challenge CHANGE THIS LINE OF CODE Sample Code to carry out the task required in the enhanced First Program Challenge on the previous slides. Point out that Python ignores anything written after a hash symbol. (#This is ignored by Python.)

Learning Review Assign Variables Use Data Types CAST variables From this lesson I am able to: Assign Variables Use Data Types CAST variables Students should become comfortable at assigning and casting variables, and have an understanding of DATA TYPES.