0% found this document useful (0 votes)
11 views21 pages

CMPT 120: Topic: Python's Building Blocks - Variables, Values, and Types

notes

Uploaded by

Alex Wu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

CMPT 120: Topic: Python's Building Blocks - Variables, Values, and Types

notes

Uploaded by

Alex Wu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CMPT 120

Topic: Python’s building blocks


-> Variables, Values, and Types
Last lecture
• Computer programming and computer
program
• Programming language
• Python

2
In this lecture we shall cover
• Python’s building blocks
• Variables
• Values
• Types
• and Literal values

3
Learning outcomes
At the end of this course, a student is expected to:
• Describe and apply fundamental concepts and
terminology of Python:
• Literals, variables, types,

4
From what we already know
to new knowledge
• As we learn to develop software this term
• we shall acquire new knowledge and skills

AND
• we shall also make use of knowledge we
have already learnt

5
In the real world, there are …
Real World
• Objects e.g.:

and

• Actions e.g.:

6
In the computer world, there
are …
Real World Computer World
• Objects • Data
• Variables (objects)
• Literal values

Expressions
• Actions • Statements
• Operators
• Functions + methods
7
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

8
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
memory

snack
“Apple”

• In a computer program, variables are referred 9


to by their name
Variable name
• Can contain letters, numbers and underscores
(no spaces, no nothing else  ), and must start
with a letter or an “_” underscore
• Examples:
• 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 3 different
10
variables
• variable price
Value and data type
• A value can either be
• an integral (whole) number
• Example: 26
• in Python, this data type is called: int (integer)
• an floating point number
• Example: 3.14
• in Python, this data type is called: float
• A sequence of character(s)
• Example: “Hello!” “” ‘1234567’
• in Python, this data type is called: str (string) 11
Variable – Demo - 1
• Let’s demonstrate
• How to create a variable in Python
• In doing so, we shall introduce the
Assignment operator “=“ (assignment statement)

1. Using Python IDLE Interpreter Shell

2. Using Python IDLE Program/Script Editor

12
Variable – Demo - 2
• Let’s demonstrate
• How to use a variable
• In doing so, we shall introduce the
output built-in function ”print” (output statement)

1. Using Python IDLE Interpreter Shell

2. Using Python IDLE Program/Script Editor

13
Variable – Demo - 3
• Let’s demonstrate
• How to assign another value to an already
existing variable
• In doing so, we shall use the
Assignment operator “=“ (assignment statement)

1. Using Python IDLE Interpreter Shell

2. Using Python IDLE Program/Script Editor


14
In summary
• 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
pi = 3.14

• Assigning a value to a variable creates that


variable
15
In summary
• 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 pi; <- variable declaration
pi = 3.14; <- variable initialized
// area of circle = pi r2
areaCircle = pi * pow(radius,2)<- variable used

16
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

17
It follows that …
• Reusing variable for different purposes is never a
good idea!

• For example:

• Why?

18
GPS (Good Programming
Style) - 2
• We cannot use a Python keyword as a variable
name
• Python keywords: Section 2.3 (page 11) of our
online textbook

• For example:

• Why?

19
Skill: Hand-tracing Python code
• Let’s “hand-trace” the following Python code:
apple = 0.75
juice = 1.65
sandwich = 4.80
lunch = apple + juice + sandwich
and in doing so, let’s see how we can represent
a variable (and its value) in memory:

20
Lastly: 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

21

You might also like