Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University 2009-2010.

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

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.
STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Strings. A string is a series of characters Characters can be referenced by using brackets The first character is at position 0 mystring = “the” letter.
Chapter 4: Conditionals and Recursion
Intro to Robots Lists in Python. Intro to Robots What is a List? An ordered set of values: –Ordered: 1 st, 2 nd, 3 rd, … –Values: can be anything, integers,
Geography 465 Assignments, Conditionals, and Loops.
Guide to Programming with Python
Programming for Linguists An Introduction to Python 17/11/2011.
4. Python - Basic Operators
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming for Linguists An Introduction to Python 24/11/2011.
Python Programming Chapter 8: Lists Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
Python for Informatics: Exploring Information
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Strings CS303E: Elements of Computers and Programming.
Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
G. Pullaiah College of Engineering and Technology
CSc 120 Introduction to Computer Programing II Adapted from slides by
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Module 3.
Basic operators - strings
Strings Part 1 Taken from notes by Dr. Neil Moore
CHAPTER 7 STRINGS.
Introduction to Strings
Introduction to Strings
Lists in Python.
Python - Strings.
CEV208 Computer Programming
Lists A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are.
String and Lists Dr. José M. Reyes Álamo.
CSC1018F: Intermediate Python
Introduction to Strings
The Data Element.
Introduction to Computer Science
Introduction to Strings
The Data Element.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Introduction to Strings
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
COMPUTING.
Presentation transcript:

Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University

Loops Loop is a statement or group of statements that execute repeatedly until a terminating condition is satisfied Infinite loop: A loop in which the terminating condition is never satisfied

while Statement def countdown(n): while n > 0: print n n = n-1 print “End!“ Explanation: “While n is greater than 0, continue displaying the value of n and then reducing the value of n by 1. When you get to 0, display the word End!”

while Statement(cont.) Exercise: write the function nLines to display a given amount of number times (n) empty lines def nLines(n): while n > 0: print n = n-1

Strings Strings are made up of smaller pieces—characters. The bracket operator selects a single character from a string. >>> fruit = "banana" >>> letter = fruit[1] >>> print letter Output: a The expression in brackets is called an index Index starts at zero

Length The len function returns the number of characters in a string: >>> fruit = "banana" >>> len(fruit) Output: 6 length = len(fruit) last = fruit[length] # ERROR! That won’t work. It causes the runtime error IndexError: string index out of range. The reason is that there is no 6th letter in "banana“ the six letters are numbered 0 to 5 length = len(fruit) last = fruit[length-1]

Exercise As an exercise, write a function that takes a string as an argument and outputs the letters backward, one per line.

for Loop for loop: for char in fruit: print char The loop continues until no characters are left.

for Loop(cont.) Example: prefixes = "JKLMNOPQ" suffix = "ack" for letter in prefixes: print letter + suffix The output of this program is: Jack Kack Lack Mack Nack Oack Pack Qack

String Slices A segment of a string is called a slice. Selecting a slice is similar to selecting a character: >>> s = "Peter, Paul, and Mary" >>> print s[0:5] Peter >>> print s[7:11] Paul >>> print s[17:21] Mary The operator [n:m] returns the part of the string from the “n- eth” character to the “m-eth” character, including the first but excluding the last

String Slices(cont.) If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string. >>> fruit = "banana" >>> fruit[:3] ’ban’ >>> fruit[3:] ’ana’ What do you think s[:] means?

String Comparison The comparison operators work on strings. if word == "banana“ print "Yes, we have no bananas!“ if word < "banana": print "Your word," + word + ", comes before banana." elif word > "banana": print "Your word," + word + ", comes after banana." else: print "Yes, we have no bananas!" In Python, uppercase letters come before all the lowercase letters. Zebra, comes before banana.

The string Module The string module contains useful functions ( import string) find function: >>> fruit = "banana" >>> index = string.find(fruit, "a") >>> print index 1

The string Module(cont.) >>> string.find("banana", "na") 2 >>> string.find("banana", "na", 3) 4 >>> string.find("bob", "b", 1, 2)

Lists A list is an ordered set of values, where each value is identified by an index. the elements of a list can have any type [10, 20, 30, 40] ["spam", "bungee", "swallow"] ["hello", 2.0, 5, [10, 20]]

Lists(cont.) Loop horsemen = ["war", "famine", "pestilence", "death"] i = 0 while i < 4: print horsemen[i] i = i + 1

List Length The function len returns the length of a list horsemen = ["war", "famine", "pestilence", "death"] i = 0 while i < len(horsemen): print horsemen[i] i = i + 1

List Membership in is a boolean operator that tests membership in a sequence >>> horsemen = [’war’, ’famine’, ’pestilence’, ’death’] >>> ’pestilence’ in horsemen True >>> ’debauchery’ in horsemen False >>> ’debauchery’ not in horsemen True

Lists and for Loops Syntax for VARIABLE in LIST: BODY for number in range(20): if number % 2 == 0: print number for fruit in ["banana", "apple", "quince"]: print "I like to eat " + fruit + "s!"

List Operations The + operator concatenates lists: >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print c [1, 2, 3, 4, 5, 6] The * operator repeats a list a given number of times: >>> [0] * 4 [0, 0, 0, 0] >>> [1, 2, 3] * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3]