loops for loops iterate over a given sequence.

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Python Control of Flow.
Simple Python Loops Sec 9-7 Web Design.
Python quick start guide
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Programming for Linguists An Introduction to Python 24/11/2011.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Course A201: Introduction to Programming 11/04/2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
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.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
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.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
String and Lists Dr. José M. Reyes Álamo.
Chapter 6: Loops.
Python Variable Types.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
When to use Tuples instead of Lists
Python: Control Structures
Basic operators - strings
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.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
JavaScript: Control Statements.
Engineering Innovation Center
Chapter 5 Structures.
For Loops October 12, 2017.
Arrays, For loop While loop Do while loop
PYTHON Varun Jain & Senior Software Engineer
CHAPTER THREE Sequences.
CS190/295 Programming in Python for Life Sciences: Lecture 6
The University of Texas – Pan American
Iteration: Beyond the Basic PERFORM
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
3 Control Statements:.
String and Lists Dr. José M. Reyes Álamo.
LabVIEW.
Introduction to Python: Day Two
(more) Python.
Python While Loops.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Dictionary.
Introduction to Python
Presentation transcript:

loops for loops iterate over a given sequence. for loops can iterate over a sequence of numbers using the range and xrange functions #!/usr/bin/python primes = [2, 3, 5, 7] for prime in primes: print prime #!/usr/bin/python for x in xrange(5): # or range(5) print x # Prints out 3,4,5 for x in xrange(3, 6): # or range(3, 6) # Prints out 3,5,7 for x in xrange(3, 8, 2): # or range(3, 8, 2) primes = [2, 3, 5, 7] for prime in primes: print prime prints in range 3-8 in steps of 2

while loops while loops repeat as long as a certain boolean condition is met. #!/usr/bin/python # Prints out 0,1,2,3,4 count = 0 while count < 5: print count count += 1 # This is the same as count = count + 1

while loops, break, continue while loops repeat as long as a certain boolean condition is met. break is used to exit a for loop or a while loop. continue is used to skip the current block, and return to the for or while statement #!/usr/bin/python # Prints out 0,1,2,3,4 count = 0 while count < 5: print count count += 1 # This is the same as count = count + 1 #!/usr/bin/python # prints out 0,1,2,3,4 count = 0 while True: print count count += 1 if count >= 5: break #!/usr/bin/python # Prints out only odd numbers - 1,3,5,7,9 for x in xrange(10): # Check if x is even if x % 2 == 0: continue print x

functions Functions in python are defined using the block keyword def, followed with the function's name as the block's name. #!/usr/bin/python def my_function(): print "Hello From My Function!” def sum_two_numbers(a, b): return a + b my_function() x = sum_two_numbers(1,2) print x function w/o argument function w argument(s) calling functions

dictionaries A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. Each value stored in a dictionary can be accessed using a key, which is any type of object (a string, a number, a list, etc.) instead of using its index to address it. phonebook = {} phonebook["John"] = 938477566 phonebook["Jack"] = 938377264 phonebook["Jill"] = 947662781 alternatively: phonebook = { "John" : 938477566, "Jack" : 938377264, "Jill" : 947662781 }

dictionaries contd. Dictionaries can be iterated over, just like a list. However, a dictionary, unlike a list, does not keep the order of the values stored in it. To iterate over key value pairs, use #!/usr/bin/python phonebook = {} phonebook["John"] = 938477566 phonebook["Jack"] = 938377264 phonebook["Jill"] = 947662781 for name, number in phonebook.iteritems(): print name, phonebook #or for i in phonebook.keys(): print i, phonebook[i]

dictionaries contd. To remove a specified index, use either one of the following notations: #!/usr/bin/python phonebook = {} phonebook["John"] = 938477566 phonebook["Jack"] = 938377264 phonebook["Jill"] = 947662781 del phonebook["John"] # or phonebook.pop("John") for name, number in phonebook.iteritems(): print name, phonebook

Classes and objects Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects. To access the variable inside of the newly created object "MyObject" you would do the following: To change the variable: #!/usr/bin/python class MyClass: variable = "blah" def function(self): print "This is a message inside the class.” myobjectx = MyClass() print myobjectx.variable myobjectx.variable = "yackity" assign the above class(template) to an object