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

Slides:



Advertisements
Similar presentations
Chapter 2: Input, Processing, and Output
Advertisements

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Recitation 1 Programming for Engineers in Python.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Documentation and Comments. What’s a comment? A comment is a simple form of documentation. Documentation is text that you the programmer write to explain.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Input, Output, and Processing
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Bill Tucker Austin Community College COSC 1315
Topic: Python Lists – Part 1
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: File Input/Output (I/O)
Topics Designing a Program Input, Processing, and Output
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
Topic: Introduction to Computing Science and Programming + Algorithm
Introduction to the C Language
Elementary Programming
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Chapter 2: Input, Processing, and Output
Topic: Python’s building blocks -> Variables, Values, and Types
It’s called “wifi”! Source: Somewhere on the Internet!
Introduction to Programming
CMPT 120 Topic: Functions – Part 4
Revision Lecture
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
Variables, Expressions, and IO
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Functions CIS 40 – Introduction to Programming in Python
Introduction to the C Language
Learning to Program in Python
Introduction to the C Language
IDENTIFIERS CSC 111.
Chapter 10 Programming Fundamentals with JavaScript
Topics Introduction to File Input and Output
Introduction to Programming
Programming Funamental slides
Variables ICS2O.
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Programming Funamental slides
Course websites CS201 page link at my website: Lecture slides
Introduction to Programming
Introduction to Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Chapter 2: Input, Processing, and Output
Introduction to Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to Programming
Topics Introduction to File Input and Output
CMPT 120 Lecture 2 - Introduction to Computing Science – Problem Solving, Algorithm and Programming.
CMPT 120 Lecture 3 - Introduction to Computing Science – Programming language, Variables, Strings, Lists and Modules.
CMPT 120 Lecture 4 – Unit 1 – Chatbots
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Presentation transcript:

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

Last Lecture i-clicker Session 1 Introduced Python Practiced the Problem Solving Process some more using Python Step 4 Step 1 Step 2 Step 5 Implement algorithm into computer program State problem Design algorithm + Identify data Test it

Learning outcomes At the end of this course, a student is expected to: Describe and apply (i.e., use in s/w dev.) 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)

Today’s Menu Start learning Python’s building blocks Variables Values Types and Literal values Hand tracing Execution Flow Python Comments and Comment Header Block

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

Hand Tracing What is it? Why doing it? When a software developer manually goes through her/his code (program) and “execute” it as if s/he was a computer, mimicking the Python Interpreter Why doing it? To figure out what our program does/produces, hence to verify whether our program is solving the problem To determine whether our program contains any errors

Hand Tracing Hand tracing often involves tracing the content of the memory (used by the executing program) as it would appear if the program was to be executed Tracing variables (drawing these variables as boxes) and their content (values) Example:

Execution Flow What is it? It is the path the Python Interpreter takes when it executes our program When we select the “Run/Run Module (F5)” menu option of the IDLE Python Program Editor window, the Python Interpreter starts interpreting each statement (line) of our program from its very top down to its last line, proceeding in a sequential fashion (one line at a time, top to bottom) We shall learn Python building blocks that will allow us to disrupt this sequential flow of execution later on this semester

Comments in Python Syntax: # some comments or Python statement # some comments How to create comments in our Python program We can use the steps of our algorithm as comments in our Python program Example: Let’s have a look at FinalCourseGrade.py Reasons for using comments in our Python program Explaining what the statements of our program do Temporarily ”removing” code from our program without deleting it, as we are developing and debugging our program The Python Interpreter ignores (i.e., does not interpret) anything written to the right of the # character, all the way to the end of a line

Header Comment Block Purpose: Give information about our program Composed of: Filename Description of program Author Date of creation Location: At the very top of our program Execution? Since we start each line of our header comment block with a # sign, i.e., making each line a comment, this signifies that the Python Interpreter skips the entire header comment block and start executing the first non-comment line below it

Header Comment Block Example: Let’s have a look at FinalCourseGrade.py on our course web site # FinalCourseGrade.py # # Compute course final grade from 3 activities: # - Assignment 1 -> Assn1 # - Midterm examination -> MT # - Final examination -> FE # Anne Lavergne # May 2017 … rest of program Filename Description of program Author Date of creation

Summary Started learning Python’s building blocks Variables Values Types and Literal values Hand tracing Execution Flow Python Comments and Comment Header Block

Next Lecture Continue learning Python’s building blocks Let’s have a look at the 2nd concept “Actions” Statements Expressions Categories of Statements Assignment statement Input statement Output statement Operational statements