Last Week Doctests Lists List methods Nested Lists Aliasing.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Lists CMSC 201. Overview Today we will learn about: For loops.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Repetition. Examples When is repetition necessary/useful?
Loops - Monday, Week 4  What are loops?  While loops  Classic loop example - the for loop  Parts of a loop  Examples.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
More Looping Structures
AE1205 Programming Python Quiz: so how do we generate Numbers 1 to 10? range( ) Numbers 5,10,15,20,25 to 100? range( ) Numbers 20,18,16, to 0? range( )
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.219, Problem 08.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
For loops in programming Assumes you have seen assignment statements and print statements.
Last Week if statement print statement input builtin function strings and methods for loop.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
For Loops CMSC 201. Overview Today we will learn about: For loops.
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
Last Week Lists List methods Nested Lists Looping through lists using for loops.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
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.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
More about Iteration Victor Norman CS104. Reading Quiz.
String and Lists Dr. José M. Reyes Álamo.
Example: Vehicles What attributes do objects of Sedan have?
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 14 – For Loops
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.
Iterations Programming Condition Controlled Loops (WHILE Loop)
CMSC201 Computer Science I for Majors Lecture 15 – For Loops
Visual Basic .NET BASICS
Determinate Loops with the
Decision Structures, String Comparison, Nested Structures
Class 14 functions in Python can return multiple values counting loops
4. sequence data type Rocky K. C. Chang 16 September 2018
Higher Computing Using Loops.
Repetition Structures
String and Lists Dr. José M. Reyes Álamo.
More Looping Structures
Remembering lists of values lists
Lists in Python Outputting lists.
CS 1111 Introduction to Programming Spring 2019
Python Basics with Jupyter Notebook
The structure of programming
Loops and Simple Functions
Programming Dr. Jalal Kawash.
Repetition (While Loop) LAB 9
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
More Looping Structures
Lecture 8 – Practice Exam
Introduction to Python
Tuple.
Presentation transcript:

Last Week Doctests Lists List methods Nested Lists Aliasing

This Week Looping though lists Looping using range While loops

Visiting the Items in a List L Printing out the list English: for each item in L print the item Python: for item in L: print(item)

For Loops -- Revisited Want to print the numbers from for num in [0, 1, 2, 3, 4, …, 99, 100]: print(num) Is there an easier way to do this? for num in range(0, 101): print(num) range(start, stop[,step]) returns a list of integers beginning with start to the last integer before stop. start can be omitted and defaults to 0 step can be omitted and defaults to 1

For Loops -- Revisited L1 = [1, 2, 3, 4] L2 = [‘A’, ‘B’, ‘C’, ‘D’] Want to print each element from L1 followed by the corresponding element from L2. for num in L1: print(num, ??) # How do we print the item from L2? Loop over indices: for index in range(len(L2)): print(L1[index], L2[index])

While Loops Sometimes we need to loop until a condition is met. For example: –Ask user for a password twice –If the passwords don’t match, ask again until they match or the user quits

While Loop Example English example: Ask for password. Ask for password again. While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) Ask for password again. While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) While the passwords don’t match: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: Ask again.

While Loop Example Python example: password1 = input(‘Enter password: ’) password2 = input(‘Re-enter password: ’) while password1 != password2: password2 = input(‘Re-enter password:’)