Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Python’s building blocks -> Variables, Values, and Types

Similar presentations


Presentation on theme: "Topic: Python’s building blocks -> Variables, Values, and Types"— Presentation transcript:

1 Topic: Python’s building blocks -> Variables, Values, and Types
CMPT 120 Topic: Python’s building blocks > Variables, Values, and Types

2 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)

3 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

4 Today’s Menu Start learning Python’s building blocks Variables Values
Types and Literal values Hand tracing Execution Flow i-clicker

5 In the real world, there are …
“Things” e.g.: and Actions e.g.:

6 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

7 Input-Process-Output Model
Most computer programs follow this model Input Process Output Data Data Data + Actions

8 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

9 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

10 Variable name Syntax rule: Can contain: Cannot contain:
Must start with

11 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

12 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)

13 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

14 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)

15 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

16 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

17 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

18 It follows that … Reusing variable for different purposes is never a good idea! For example: Why?

19 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?

20 GPS (Good Programming Style) – 3
Have a look at our Good Programming Style web page and see what it says about Literal Values

21 Summary Started learning Python’s building blocks Variables Values
Types and Literal values Hand tracing Execution Flow i-clicker

22 Next Lecture Continue learning Python’s building blocks
-> statements Categories of Statements Assignment statement Input statement Output statement Operational statement


Download ppt "Topic: Python’s building blocks -> Variables, Values, and Types"

Similar presentations


Ads by Google