200 likes | 336 Views
Programming in Python Part III. Dr. Fatma Cemile Serçe Atılım University 2009-2010. 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.
E N D
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University 2009-2010
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) -1
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]