Topic: Python’s building blocks -> Statements

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Fundamentals of Python: From First Programs Through Data Structures
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Variables, Expressions and Statements
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Foundations of Programming: Java
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: Python’s building blocks -> Variables, Values, and Types
Topics Designing a Program Input, Processing, and Output
Topic: Functions – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
Topic: Conditional Statements – Part 1
COMPSCI 107 Computer Science Fundamentals
Chapter 2 - Introduction to C Programming
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Conditional Statements – Part 2
2.5 Another Java Application: Adding Integers
Revision Lecture
Design & Technology Grade 7 Python
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
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.
Programming Funamental slides
Wednesday 09/23/13.
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Programming Basics.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Introduction to C Programming
CMPT 120 Lecture 3 - Introduction to Computing Science – Programming language, Variables, Strings, Lists and Modules.
Topic: Iterative Statements – Part 2 -> for loop
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Lecture 20 – Practice Exercises 4
Data Types and Expressions
Presentation transcript:

Topic: Python’s building blocks -> Statements CMPT 120 Topic: Python’s building blocks -> Statements

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

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: Expressions Operators Statements Create (design) simple algorithms: Solve problems by designing simple algorithms, e.g., basic calculations 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 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

Statements Like most programming languages, Python programs are composed of statements A Python program may contain many statements What is a statement? 1 computer program instruction, made of expressions A Python statement may contain many expressions What is an expression?

What is an expression? literal values examples of literal values: 3, 5.6, “Hello” Expression Definition: - Fragment of code - Can be composed of … built-in functions methods variables operators (+ operands) examples of operators: +, -, *, /, **, string concatenation

Examples of Expressions 1. width/2 2. width/2.0 3. height/3 4. 1 + 2 * 5 5. delimiter * 5 Variables: width, height, and delimiter Literal values: 2, 2.0, 3, 1, and 5 Operators: /, +, and *

Terminology related to expression Expressions are interpreted (i.e., executed or evaluated) by Python interpreter Once interpreted, an expression produces a result If the expression is part of a statement, its result is used to evaluate the rest of the statement Example: print( len(“Anne”) + 26 ) Expression 1 Expression 2 Expression 3 Expression 4

Python statements Categories: Assignment statement Input statement Conversion function Output statement Operational statements Mathematical/arithmetic operator String manipulation operator Conditional statement Iterative statement Some of them are built-in function or method Today We’ll see Strings in a few lectures! We’ll see these statements soon!

1. Assignment statement Operator: = < … > signifies “replace < … > with the name of a variable” Operator: = Syntax: <variable> = <expression> Examples: gradeMT = 0 gradeMT = float(input("Please, enter MT grade: ")) newGradeMT = gradeMT * percentOfFinalGradeMT / gradedOutOfMT How it works: The expression on the right of the assignment operator is evaluated If the variable on the left of the assignment operator does not exist … it is then created (given space in memory) the result of the expression is assigned to this variable (stored in the space in memory) otherwise its value is changed to (overwritten by) the result of the expression Caution: In Python, = does not mean equality, it means “assigns” < … > signifies “replace < … > with an expression”

2. Input statement input( ) is a built-in function < … > signifies “replace the whole thing with a prompt” input( ) is a built-in function Syntax -> input(<prompt>) What is a function? It represents an action our program does What is a prompt? It is an instruction to the user Prompt must be a string (data of str data type) What is a user? The person that executes our program and types in data Example: gradeMT = float(input("Please, enter MT grade: ")) How it works: The prompt is printed on the screen The user enters the requested data then presses Enter This data is returned from the function input( ) into our program as a string GPS: When prompting the user, we must give her/him clear and unambiguous instructions: the value to enter, the format of this value (if possible) and any other useful information such as the range of the value, etc...

Conversion functions int( ) float( ) are built-in functions str( ) Syntax -> int(<expression>) float(<expression>) str(<expression>) Examples gradeMT = float(input("Please, enter MT grade: ")) < … > signifies “replace the whole thing with an expression”

3. Output statement print( ) is a built-in function < … > signifies “replace the whole thing with expressions, variables or literal values” print( ) is a built-in function Syntax -> print(<argument(s)>) What is an argument? Data we want the function to use and act upon Example: print("Final grade is %0.2f." %finalGrade ) How it works: The value of the arguments are printed on the screen Often, format instructions are included in the arguments as in our example above The formatting instructions here are saying: print the value of the expression (variable) finalGrade as a float value (f in %0.2f) with only 2 decimal numbers to the right of the decimal point (0.2 in %0.2f) GPS: The output the program produces must be labelled clearly

GPS related to input and output statements and user interaction From a user perspective, i.e., from the perspective of the person typing her/his name, which user interaction would we prefer? Why? User interaction # 1 User interaction # 2

4. Operational statement Mathematical/arithmetic operator: Addition: + Subtraction: - Multiplication: * Division: / Floor division: // Modulus: % Exponentiation: ** Syntax: <operand> <operator> <operand> Example: newGradeMT = gradeMT * percentOfFinalGradeMT / gradedOutOfMT < … > signifies “replace with an operand, i.e., an integer or a float” < … > signifies “replace with one operator”

Let’s Practise!

Summary Python’s building blocks Categories of Statements Expressions Categories of Statements Assignment statement Input statement Output statement Operational statements

Next Lecture Continue learning Python statements (Python’s building blocks) Categories of Statements Operational statements Mathematical/arithmetic operators Order of operations Augmented assignment operators Function terminology as we practice Step 5 of the software development process -> Testing and Debugging 3 kinds of errors