Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Similar documents
Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Decision Making in C

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Flow Control: Branches and loops

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Flow Control. CSC215 Lecture

Conditionals and Recursion. Python Part 4

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Lecture 7 Tao Wang 1

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

LECTURE 04 MAKING DECISIONS

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Selection Statement ( if )

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

5. Control Statements

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

LECTURE 5 Control Structures Part 2

Introduction. C provides two styles of flow control:

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

bool bool - either true or false

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Text Input and Conditionals

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

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

CSI33 Data Structures

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Programming for Experimental Research. Flow Control

CP215 Application Design

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Computational Physics - Fortran February 1997

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Introduction to Python

Chapter 8. Statement-Level Control Structures

CS 112: Intro to Comp Prog

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

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

Advanced Computer Programming

Quiz Determine the output of the following program:

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Repetition Structures II

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

BIL101E: Introduction to Computers and Information systems Lecture 8

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

MITOCW watch?v=0jljzrnhwoi

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

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Lab 5 - Repetition. September 26, 2018

CS Summer 2013

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

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

Conditional Expressions and Decision Statements

Control Structures 1 / 17

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

CONTROL AND ENVIRONMENTS 1

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Lecture no

Chapter 2 Control in Programming. lecture 2 1

CONTROL AND HIGHER ORDER FUNCTIONS 1

Software Design & Programming I

Computer Programming

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

Chapter 8. Statement-Level Control Structures

SCoLang - Language Reference Manual

Score score < score < score < 65 Score < 50

Control Flow: Branching booleans and selection statements CS GMU

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 5 Tao Wang 1

ENGR 102 Engineering Lab I - Computation

REPETITION CONTROL STRUCTURE LOGO

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

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

Senthil Kumaran S

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Lists, loops and decisions

Chapter 3: Program Statements

Python for Informatics

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Introduction to Bioinformatics

Lecture Notes for Chapter 2: Getting Started

Transcription:

Loops and Conditionals HORT 59000 Lecture 11 Instructor: Kranthi Varala

Relational Operators These operators compare the value of two expressions and returns a Boolean value. Beware of comparing across data types, especially when reading values in from command line or files.

Relational Operators == equal True if expressions are equal!= not equal True if expressions are not equal > Greater than True if left is greater than the right < Less than True if left is less than the right >= greater than OR equal <= less than OR equal is identity True if the left is the same object as right in contains True if the object on left is contained in object on right (Useful for finding values in list)

Assignment Operators A += B A -= B A *= B A /= B A **=B A %= B A //= B increase A by value of B decrease A by value of B multiply A by B and assign value to A divide A by B and assign value to A raise value of A to the power of B modulus of A by B, assigned to A floor of A divided by B, assigned to A String context: S1 += S2 add string on right to the one on left S1 *= A Make A copies of S1 and concatenate them to S1

Boolean Operators Combines two or more statements that return a Boolean value. A and B True if both A and B are true A or B True if either A or B is true not A reverse the Boolean given by A xor(a,b) True if only one of A or B is True A B A and B A or B Not A xor(a,b) TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE

General Python Syntax rules End of line is end of statement Statements at the same indentation level are in the same block (e.g., within a loop or condition) End of indentation Exceptions: Semi colon ; separates statements on the same line Single line blocks are allowed without indentation

Branching logic Used to implement alternate paths for the logic flow. https://upload.wikimedia.org/wikipedia/commons/4/44/lampflowchart.png

If/elif/else statements if test1: statement 1 elif test2: statement 2 else: statement 3 Both the elif and else blocks are optional.

If/elif/else statements

Lamp flowchart with if/else Accepts input from user, as a string

Truth and Boolean tests in Python All objects in python have an inherent true or false value. Any nonempty object is true. For Integers : Any non-zero number is true Zero, empty objects and special object None are false. Comparisons return the values True or False

Loops/Iterations A loop is syntax structure that repeats all the statements within the loop until the exit condition is met. Statements in a loop are defined by indenting them relative to the loop start. Loop ends when indentation ends. Python has two forms of loops: for loop and while loop. E.g. >>> for x in range(10) E.g. >>>while (A==10)

while loops while condition: statement 1 statement 2.. Most generic form of loop, that checks whether the condition is true at the start of each iteration. Expects the condition to become false at some point during the iterations of the loop. If condition is never changed, this creates an infinite loop. i.e., the program will get stuck in this loop for ever.

Example while loops

Altering while loops Normal loop control will execute all statements in block on every iteration. Loop ends only when exit condition is met. break statement forces the current loop to exit. continue statement skips the rest of the block and goes to the next iteration of the loop. pass statement is a placeholder for empty blocks.

Altering while loops

for loops for item in sequence: statement 1 statement 2.. Generic iterator for items in a ordered sequence such as lists, tuples etc. On each iteration retrieves one item from the list and assigns it to the variable specified. Automatically moves to the next item in the order. Value of variable may be altered within the for loop, but change is not made in the list.

for loops

Looping over Strings and Lists List is a general sequence object while String is a character sequence object. Both can be iterated over by a for loop:

Looping over lists with and without index Looping with an index allows accessing the item within the list and changing it.

Looping over Tuples and Dictionaries

Nested Loops Loops can be nested just like the if/else statements. Indentation is again the key to creating nested loops. In a 2 level nested loop with x iterations on the outer loop and y iterations in the inner loop: All statements in the outer loop will be executed x times All statements in the inner loop will be executed x*y times

MultiDimensional Lists my2dlist = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] my2dlist 0 1 2 3 0 1 5 9 13 1 2 6 10 14 2 3 7 11 15 3 4 8 12 16

MultiDimensional Lists my2dlist = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] for x in range(len(my2dlist)): insidelist=my2dlist[x] my2dlist 0 1 2 3 0 1 5 9 13 1 2 6 10 14 2 3 7 11 15 3 4 8 12 16

MultiDimensional Lists my2dlist = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] for x in range(len(my2dlist)): for y in range(len(my2dlist[x])): print my2dlist[x][y] 0 1 2 3 my2dlist 0 1 5 9 13 1 2 6 10 14 2 3 7 11 15 3 4 8 12 16

Arbitrary dimensional Lists subl = [[ p, q ],[ r, s ]] my2dlist = [[1,2,3, a ],[5,6,7, cat ],[9,10, e,12],[ beta,14,15,subl]] my2dlist 0 1 2 3 Loop1 0 1 5 9 beta Loop2 1 2 2 3 6 7 10 e 14 15 0 Loop3 0 1 p q 3 a cat 12 1 r s

Summary: Conditions and loops Conditional statements with the proper comparison and boolean operators allow the creation of alternate execution paths in the code. Loops allow repeated execution of the same set of statements on all the objects within a sequence. Using an index based for loop is best suited for making changes to items within a list. Always ensure that your exit condition will be met.