Topic: Python’s building blocks -> Variables, Values, and Types CMPT 120 Topic: Python’s building blocks -> Variables, Values, and Types
Learning outcomes At the end of this course, a student is expected to: Describe and apply fundamental concepts and terminology of Python: Literals Variables Data types Execution flow Create (design) small size programs using Python: Hand trace code (programs) and predict results of executing code (determining contents of variables and output)
Last Lecture Evolution of programming languages Compiled versus interpreted programming languages Introduced Python Illustrated the Software Development Process using Python Implement algorithm into computer program State problem Design algorithm + Identify data Test it
Today’s Menu Start learning Python’s building blocks Variables Values Types and Literal values Hand tracing Execution Flow i-clicker
In the real world, there are … “Things” e.g.: and Actions e.g.:
In the computer world, there are … Real World “Things” Actions Computer World “Things” -> Actions -> Scratch: -> Python: Data Variables (objects) Literal values Statements (or blocks) Operators Functions + methods
Input-Process-Output Model Most computer programs follow this model Input Process Output Data Data Data + Actions
Variable We saw in Lecture 3 that computers are very good at manipulating lot’s of data But in order for the computer to manipulate this data, we must tell the computer to remember the data We do this by using variables in our programs
Variable Definition: A variable is a memory location to which we give a name to which we assign a value of a particular data type In a computer program, variables are referred to by their name
Variable name Syntax rule: Can contain: Cannot contain: Must start with
Variable name Convention 1: use camelCase (see wikipedia) Example: finalLetterGrade Convention 2: user underscore between words Example: final_letter_grade Python is case sensitive: variable PRICE variable Price variable price 3 different variables
Value and their data type A value can either be an integral (whole) number Example: in Python, this data type is called: int (integer) a floating point number in Python, this data type is called: float a sequence of character(s) in Python, this data type is called: str (string)
Variable – Demo How to create a variable in Python In doing so, we shall introduce the Assignment operator “=“ (assignment statement) How to use a variable In doing so, we shall introduce the output built-in function ”print” (output statement) How to assign another value to an already existing variable In doing so, we shall use the Assignment operator “=“ (assignment statement) Using Python IDLE Interpreter Shell & Program Editor
Weakly Typed Python is a weakly typed high level programming language This means that when we need to use a variable in our Python program, all we need to do is assign a value to our variable in order to create the variable (or the space in memory)
Strongly Typed Example: in C This is not the case in strongly typed high level programming languages like C, C++ and Java, where a variable must first be declared, i.e., one must first state the type of value this variable is to reference, before the variable is used Example: in C float radius; <- variable declaration float areaCircle; <- variable declaration float pi; <- variable declaration pi = 3.14; <- variable initialized radius = 2.5; <- variable initialized // area of circle = pi * radius2 <- comment areaCircle = pi * pow(radius,2)<- variables used
Literal Value Examples: The values in red are literal values: apple = 0.75 juice = 1.65 sandwich = 4.80 They are called “literal values” because they are literally typed into a program
GPS (Good Programming Style) – 1 Variable name must be descriptive, i.e., it must describe the purpose of the variable or the meaning of its value For example: Why? Creates self-documented code
It follows that … Reusing variable for different purposes is never a good idea! For example: Why?
GPS (Good Programming Style) – 2 We cannot use a Python keyword as a variable name Python keywords: Section 2.2 (page 10) of our online textbook For example: Why?
GPS (Good Programming Style) – 3 Have a look at our Good Programming Style web page and see what it says about Literal Values
Summary Started learning Python’s building blocks Variables Values Types and Literal values Hand tracing Execution Flow i-clicker
Next Lecture Continue learning Python’s building blocks -> statements Categories of Statements Assignment statement Input statement Output statement Operational statement