 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Advertisements

Statement-Level Control Structures
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
CS0004: Introduction to Programming Repetition – Do Loops.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
ISBN Chapter 8 Statement-Level Control Structures.
Structured programming
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Python Control of Flow.
4. Python - Basic Operators
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 >>> while b < 10:... print b... a, b = b, a+b WHILE.
If statements while loop for loop
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
ISBN Chapter 8 Statement-Level Control Structures.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
More about Iteration Victor Norman CS104. Reading Quiz.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Chapter 6: Loops.
Computer Programming Fundamentals
Loops in Java.
Computer Programming Fundamentals
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CHAPTER FOUR Functions.
Call Stacks, Arguments and Returns
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Statement-Level Control Structures
Outline Altering flow of control Boolean expressions
Flow Control Presented by: Md Fashiar Rahman.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
© 2016 Pearson Education, Ltd. All rights reserved.
Statement-Level Control Structures
CHAPTER 6: Control Flow Tools (for and while loops)
CHAPTER 21 LOOPS 1.
The structure of programming
COMPUTER PROGRAMMING SKILLS
Python While Loops.
LOOP Basics.
Presentation transcript:

 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string" for char in x : print(char, end = “ “) #char is not a reserved word #Prints: s t r i n g

 Use slice notation to create a copy to loop through rather than looping through the original list Example) x = [1, 3, 5]; for num in x[:]: x.append(num) print(x) #Prints: [1, 3, 5, 1, 3, 5] rather than creating an # infinite loop

 The built-in range() function produces intervals that can be designated with parameters Example) for x in range(5): print(x, end = " ") # Prints:

Examples) for x in range(5, 10): print(x, end = " ") #Prints: for x in range(1, 10, 3): # 3 indicates the step print(x, end = " ") #Prints: 1 4 7

 The break statement can be used to exit the loop in which the break statement has been placed  The continue statement moves to the next iteration of the loop without executing lines below it

 Loop statements may now use an else clause  The else is executed when a loop exhausts all items in a list or hits the terminating condition, but not when a break statement is used Example) x = 0 while x < 10: x+=1 else: print("In the Else") #Prints: In the Else

 The pass statement does nothing  This statement is used when code is required, but nothing useful needs to be written while True: pass #Infinite loop that does nothing

 The keyword def is used to define functions  def must be immediately followed by the function’s name and a list of parameters  Parameters that are passed in functions are always references to an existing object, not the object itself  No return types are required

Example) def sum(a, b): return a + b print(sum(5, 6)) #Prints: 11

 The same function can have multiple names def sum(a, b): return a + b print(sum(5, 6)) #Prints 11 s = sum print(s(3, 5)) # Prints 8

 A function without a return statement, returns “None”  A function that does not reach its return statement will return “None” Example) def mystery(): pass print(mystery()) #returns: None

 Use “in” to determine if an element is part of a list x = 5 y = [2, 5, 6, 7] if x in y: print("5 exists") # Prints: 5 exists