Python Flow of Control CS 4320, SPRING 2015. Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.

Slides:



Advertisements
Similar presentations
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Advertisements

Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Chapter 2 Writing Simple Programs
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.
Python Control of Flow.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python Control Flow statements There are three control flow statements in Python - if, for and while.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Python 2 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Xin Liu Feb 11, * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'
For loops in programming Assumes you have seen assignment statements and print statements.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Control flow Ruth Anderson UW CSE 160 Spring
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control flow Ruth Anderson UW CSE 160 Winter
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
(Monty) Python for loops Mr. Neat. Comparison JavaC++Python source code name.java.cpp.py Object Oriented? required optional functions/ methods ( bob.hide()
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
More about Iteration Victor Norman CS104. Reading Quiz.
Chapter 2 Writing Simple Programs
Control Flow (Python) Dr. José M. Reyes Álamo.
Java Arrays and ArrayLists COMP T1 #5
CS1022 Computer Programming & Principles
Computer Programming Fundamentals
Selection and Python Syntax
Loops in Java.
Computer Programming Fundamentals
Think What will be the output?
Repetition: the for loop
Engineering Innovation Center
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Multiple Selections (ELIF Statements)
Control flow : if statements
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming Using Python PART 2
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Types, Truth, and Expressions (Part 2)
Loops.
CHAPTER 6: Control Flow Tools (for and while loops)
CS 1111 Introduction to Programming Spring 2019
Program Flow.
Python Basics with Jupyter Notebook
Repetition: the for loop
Introduction to Computer Science
Print the following triangle, using nested loops
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
Understanding Code Workshop 2.
Python While Loops.
Comparing Python and Java
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

Python Flow of Control CS 4320, SPRING 2015

Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the list s 1/7/2015CS 4320, SPRING for x in s: print(x)

Selection The semantics of the if statement are familiar, however the syntax is different from Java The body of each part of the ‘if’ must be indented. Four spaces is pretty standard. ◦The indentation must be consistent throughout the body 1/7/2015CS 4320, SPRING If x < 3: print(‘small’) elif x < 5: print(‘medium’) else: print(‘large’)

Example Get a running total of the positive elements in a list of numbers 1/7/2015CS 4320, SPRING

Ranges For stepping through ranges of integers, the range function is available range(10) is the numbers from 0 to 9, inclusive range(5,10) is the numbers from 5 to 9, inclusive range(5,10,2) is the numbers 5, 7 and 9 1/7/2015CS 4320, SPRING