CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Similar documents
CS Boolean Statements and Decision Structures. Week 6

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 22, 2018

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

SYNERGY INSTITUTE OF ENGINEERING & TECHNOLOGY,DHENKANAL LECTURE NOTES ON DIGITAL ELECTRONICS CIRCUIT(SUBJECT CODE:PCEC4202)

Conditionals & Control Flow

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Here n is a variable name. The value of that variable is 176.

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Propositional Calculus. Math Foundations of Computer Science

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Beyond Blocks: Python Session #1

Introduction to Python

Data 8 Final Review #1

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

Conditionals and Recursion. Python Part 4

Python 1: Intro! Max Dougherty Andrew Schmitt

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Combinational Circuits Digital Logic (Materials taken primarily from:

Introduction to computers and Python. Matthieu Choplin

Visualize ComplexCities

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

MEIN 50010: Python Flow Control

LSN 4 Boolean Algebra & Logic Simplification. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Lecture 8. Conditionals & Control Flow

Circuit analysis summary

Date Performed: Marks Obtained: /10. Group Members (ID):. Experiment # 04. Boolean Expression Simplification and Implementation

Bawar Abid Abdalla. Assistant Lecturer Software Engineering Department Koya University

PREPARING FOR PRELIM 1

Booleans, Logical Expressions, and Predicates

Lecture 6: Specifications & Testing

Computer Organization and Levels of Abstraction

Lists, loops and decisions

Logic Gates and Boolean Algebra ENT263

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Recitation Session 6

Computer Organization and Levels of Abstraction

Flow Control: Branches and loops

CS 111X - Fall Test 1

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Lecture 10: Floating Point, Digital Design

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

How Do Robots Find Their Way?

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

CS 115 Lecture 4. More Python; testing software. Neil Moore

Assignment (3-6) Boolean Algebra and Logic Simplification - General Questions

Python for Non-programmers

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Python Programming: An Introduction To Computer Science

CS100 Spring 2012 Midterm 1 Practice

Statements 2. a operator= b a = a operator b

Announcements For This Lecture

Basics of Programming with Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Python 2 Conditionals and loops Matthew Egbert CS111

Logic Design: Part 2

Python Programming: An Introduction to Computer Science

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Programming Logic & Pseudocode. Python Bootcamp, Day 1 Anna Rosen

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

PYTHON- AN INNOVATION

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

Chapter 2 Writing Simple Programs

CSE 115. Introduction to Computer Science I

Python: Functions. Thomas Schwarz, SJ Marquette University

Key Differences Between Python and Java

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Spring Semester 13, Dr. Punch. Exam #1 (2/14), form 1 A

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming Training. Main Points: - Python Statements - Problems with selections.

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

C++ for Python Programmers

3. Conditional Execution

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

cs1114 REVIEW of details test closed laptop period

Final Project. Project Idea. Sample Project Idea 2/11/2019. CS 362: Computer Design Lecture 7: DeMorgan s, XOR, Universal Gates

Bits and Bytes. How do computers compute?

Conditionals. C-START Python PD Workshop

CSE 115. Introduction to Computer Science I

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CS150 - Sample Final

Boolean Expressions. Is Equal and Is Not Equal

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Boolean relationships on Venn Diagrams

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Programming with Python

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Logical and Function Constructions

C ONTROL AND H IGHER O RDER F UNCTIONS

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Lecture 4. Defining Functions

Python Programming, bridging course 2011

Transcription:

CS177 Recitation Functions, Booleans, Decision Structures, and Loop Structures

Functions Collection of instructions that perform a task as: o Printing your name and course. o Calculating the average of set of numbers. o Editing a picture or video.

Functions Like in algebra, a function is a kind of box into which you put one value and out comes another. We represent ( denote ) a function by a name (in math we use f or F). Input output F

Why to use a function? If we define a function to perform a task, then we will write it once then we can use it (or call it) many times.

How to write functions? def functionname(): statement #1 statement #2

def SayHello(): print( Hello world! ) print( --From Python ) Note: 1. Never forget the colon(:) 2. Align the statements in one function

No input arguments, no variable returned def Greet(): print("hello Jack") print("hello Tom") def main(): Greet()

Multiple arguments, returns one result def Average(a, b): return (a+b)/2 def main(): ave = Average(3, 4)

Returns more than one variable def getabc(): a = "Hello" b = "World" c = "!" return a,b,c def main(): a, b, c = getabc()

def Sum(a, b, c): return (a+b+c) def Greet(name, GPA): print("hello", name) print("you have a GPA of ", GPA) def Div(a, b): return a/b def Mul(a, b): return a*b main()

# assign values to variables G1 = 4 G2 = 3.7 G3 = 3.7 C1 = 3 C2 = 1 C3 = 2 name = "Tom

GPA = Div(Sum(Mul(G1, C1), Mul(G2, C2), Mul(G3, C3)), Sum(C1, C2, C3)) Greet(name, GPA) input() The output of Mul() is input of Sum() The output of Sum() is the input of Div()

Booleans Boolean (logical) expressions: An expression that can assume only the true or false value We use logical expression in everyday language: If today is raining, then bring an umbrella when you go out. today is raining is a logical expression: its value can be either true or false. Other examples: assume x=4 x>3 (true) assume str= abc type(str)==int (false)

Booleans Boolean operators: And, or, not a and b a and true x > 0 and x <=2 y > 0 and y >= 3(overlapped) P Q P and Q T T T T F F F T F F F F

Booleans Boolean operators: And, or, not a or b a or true x <= 0 or x > 2 x > 5 or x < 10 (always true) P Q P or Q T T T T F T F T T F F F

Booleans Boolean operators: And, or, not not a not (not a) not x > 3 DeMorgan s law not (a or b) == (not a) and (not b) not (a and b) == (not a) or (not b) P T F not P F T

Booleans (P and (not Q)) or ((not P) and Q) It has a name: XOR Can you do this? P Q P xor Q T T T F F T F F

Booleans Does the result look familiar? How about this: Let T=1, F=0 P Q P xor Q 1 1 0 1 0 1 0 1 1 0 0 0 Yes, it is the sum of binary numbers

Decision Structures If statement An if statement takes a logical expression and evaluates it. If it is true, the statements in if block are executed, otherwise, they are not executed. Simple decision x = 5 if x>1: print( print something ) The string is printed x = 0 if x>1: print( print something ) Does nothing!

Decision Structures Two-way decision a = 45 if a < 100: print( a is small ) else: print( a is large ) >>> a is small a = 153 if a < 100: print( a is small ) else: print( a is large ) >>> a is large

Decision Structures Two-way decision no a < 100? yes a is large a is small

Decision Structures Multi-way decision a = 1.5 if a > 2: print( a>2 ) else: if a > 1: print( 1<a<=2 ) else: print( a<=1 ) >>> 1<a<=2 a = 1.5 if a > 2: print( a>2 ) elif a > 1: print( 1<a<=2 ) else: print( a<=1 ) >>> 1<a<=2

Decision Structures Multi-way decision no a >2? yes no a >1? yes a>2 a<1 1<a<=2 23

For vs While Loop How long do I have to shower? You have two replies you could make: a) 10 minutes b) Until you are clean a) When programming, the first answer would be portrayed as a for-loop because we focus on exactly how long the shower will continue: for minutes in range (0,9): shower b) The second answer would be portrayed as a while-loop because the length of the shower is undetermined; we instead focus on the condition of cleanliness: while you are not clean: shower

For vs While Loop for count in range(0,9): print( The count is:, count) print( for loop ended ) count = 0 while count < 9: print( The count is:,count) count = count + 1 print( while loop ended )

Rules of While Loops Command name: while Boolean condition (in the previous example: count < 9) determines the termination of loop A colon ( : ) And a block (the indented lines of code) 26

Interactive loop Using while loop, we can write interactive loops count = 0 str = Yes while str == Yes : print( The count is:,count) count = count + 1 str = input( continue? Yes or No: ) print( while loop ended ) 27

Question?