Control 9 / 25 / 19.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Conditional Statements Introduction to Computing Science and Programming I.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 2 Control.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
Decision Structures and Boolean Logic
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Python Conditionals chapter 5
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Xiaojuan Cai Computational Thinking 1 Lecture 7 Decision Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
Control statements Mostafa Abdallah
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control flow Ruth Anderson UW CSE 160 Spring
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control flow Ruth Anderson UW CSE 160 Winter
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
G. Pullaiah College of Engineering and Technology
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Python Review 1.
ARITHMETIC IN C Operators.
CSc 120 Introduction to Computer Programing II Adapted from slides by
Chapter 5: Control Structures II
Introduction to Python
Making Choices with if Statements
Module 3.
Chapter 5: Control Structures II
Topics The if Statement The if-else Statement Comparing Strings
Ruth Anderson UW CSE 160 Winter 2017
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
Control flow : if statements
Summary Two basic concepts: variables and assignments Basic types:
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Types, Truth, and Expressions (Part 2)
Nate Brunelle Today: Conditional Decision Statements
COMPUTER PROGRAMMING SKILLS
By Ryan Christen Errors and Exceptions.
Types, Truth, and Expressions
Nate Brunelle Today: Conditional Decision Statements
The Python interpreter
Michael Ernst UW CSE 190p Summer 2012
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Types, Truth, and Expressions
Lecture 2 - Names & Functions
Lecture 6 - Recursion.
Presentation transcript:

Control 9 / 25 / 19

Print and None Demo

None Indicates that Nothing is Returned The special value None represents nothing in Python A function that does not explicitly return a value will return None Careful: None is not displayed by the interpreter as the value of an expression >>> def does_not_return_square(x): ... x * x ... No return >>> does_not_return_square(4) None value is not displayed The name sixteen is now bound to the value None >>> sixteen = does_not_return_square(4) >>> sixteen + 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Pure Functions & Non-Pure Functions just return values Return value -2 abs Argument 2 2, 100 pow 2 Arguments 1267650600228229401496703205376 Non-Pure Functions have side effects Return None! print -2 None A side effect isn't a value; it's anything that happens as a consequence of calling a function Python displays the output “-2”

Nested Expressions with Print >>> print(print(1), print(2)) 1 2 None None print None, None None display “None None” Does not get displayed None print(print(1), print(2)) func print(...) print(1) func print(...) 1 print(2) func print(...) 2 None None print 1 None display “1” print 2 None display “2”

print vs return Demo

Control

Control Expressions in programs evaluate to values Statements are executed to perform actions Ex: assignment and def statements With what we have seen so far, a lot of useful programs have been left out For example: returning ‘hot’, ‘warm’, or ‘cold’ depending on an argument temp To do this we introduce the concept of control Special expressions and statements can control how the program is executed by the interpreter

Conditional statements (if statements) Clause Syntax: Always start with if clause Zero or more elif clauses Zero or one else clause, always at the end if <conditional expression>: <suite of statements> elif <conditional expression>: else: Execution Rule for Conditional Statements: Each header is considered in order Evaluate the header’s conditional expression if the header is not an else If the expression evaluates to true or the header is an else, execute the suite and skip the remaining headers

if examples Demo

Boolean Contexts def absolute_value(x): “““Return the absolute value of x.””” if x < 0: return -x elif x == 0: return 0 else: return x Two boolean contexts George Boole Boolean context is any place where an expression is evaluated to check if it’s a True value or a False value False values in Python: False, None, 0, ‘’ (more to come) True values: everything else

Boolean Expressions Boolean expressions contain special operators and, or, not <exp1> and <exp2> and <exp3> and ... Evaluate to the first false value. If none are false, evaluates to the last expression <exp1> or <exp2> or <exp3> or ... Evaluate to first true value. If none are true, evaluates to the last expression not <exp> Evaluates to True if <exp> if a false value and False if <exp> is a true value

Short-Circuiting Demo

Iteration Demo

While statements i, total = 0, 0 while i < 3: i = i + 1 Demo While statements i, total = 0, 0 while i < 3: i = i + 1 total = total + i Execution Rule for While Statements: Evaluate the header’s expression If it is a true value, execute the (whole) suite, then return to step 1 Python Tutor

The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ... def fib(n): """Compute the nth Fibonacci number, for N >= 1""" pred, curr = 0, 1 # 0th and 1st Fibonacci numbers k = 1 # curr is the kth Fibonacci number while k < n: pred, curr = curr, pred + curr k = k + 1 return curr The next Fibonacci number is the sum of the current one and its predecessor Python Tutor

Summary print vs None Control Iteration None represents when an expressions doesn’t evaluate to a value print displays values in the interpreter Control Allow for the interpreter to selectively or repetitively execute parts of a program Iteration A particular variant of control which is based in the idea of repeating operations to compute a value