course a201: introduction to programming
DESCRIPTION
Course A201: Introduction to Programming. 09/30/2010. Outlines for this week. How to write for loops Function range() Python membership operator: in Write nested for loops to print out certain shapes More on Strings String Indexing Function len () String functions - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/1.jpg)
Course A201:Introduction to Programming
09/30/2010
![Page 2: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/2.jpg)
Outlines for this week
• How to write for loops– Function range()– Python membership operator: in– Write nested for loops to print out certain shapes
• More on Strings– String Indexing– Function len()– String functions
• Finish Part-1, understand what to do in Part-2
![Page 3: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/3.jpg)
for loops vs while loops
number = 1While number< 11:
print(number) count += 1
number_list = range(1, 11, 1)for number in number_list:
print(number)
The output will be exactly the same
![Page 4: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/4.jpg)
Function range()
[functions]
I am a function.I will perform a
certain job.
input1, input2, input3, …
output1, output2, output3, …
You give me some inputs
I give you some outputs back, explicitly or implicitly
![Page 5: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/5.jpg)
Function range()
• Example of how to use range():>>> range()ERROR! range expected at least 1 arguments>>> range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> ans = range(10)>>> ans[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10 is not included!
![Page 6: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/6.jpg)
Function range()
• So, we know that range() will require at least one argument, let’s see how it works when there’re two:
>>> range(3, 11)[3, 4, 5, 6, 7, 8, 9,10]>>> range(-10,1)[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]>>> range(10,1)[]
Empty! when second argument is smaller than the first one, and there is no third argument.
![Page 7: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/7.jpg)
Function range()
• Three arguments?>>> range(3, 11, 2)[3, 5, 7, 9]>>> range(10, 1, -1)[10, 9, 8, 7, 6, 5, 4, 3, 2] >>> range(-10, -1, -1)[]>>> range(2, 8, 0)ERROR! range() step argument must not be zero
Empty!
The 3rd argument is the ‘step argument’. The default value is 1.
![Page 8: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/8.jpg)
For loops
name = “Linger Xu”for aa in name:
print(aa)
number_list = range(-10, 1)for number in number_list:
print(number)
![Page 9: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/9.jpg)
Python membership operator: in
• The variable name is a string, “Linger Xu”
[for aa in name] fetch every letter in this string sequentially, and put it into aa
• The variable number_list contains a list of numbers: [for number in number_list ] fetch every number in this list sequentially, and put it into number
Go throught every member of a specified sequence: -10, -9, -8, …
![Page 10: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/10.jpg)
Python membership operator: in
• user = “5198”if “1” in user:
print(“Number 1 is in”, user)if “0” not in user:
print(“Number 0 is not in”, user)
membership operator: in / not in
![Page 11: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/11.jpg)
Python membership operator: in
• The variable after in must be holding a sequence of values, such as string and list.
number_list = 9for number in number_list:
print(number)
ERROR! 'int' object is not iterable
![Page 12: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/12.jpg)
For loops
• Finish first problem in Part1 , Assignment 4
Write a program that counts for the user using a for loop as shown in class. Let the user enter the starting number, the ending number, and the amount by which to count.
![Page 13: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/13.jpg)
Write nested for loops
• How to print out:
1 2 3 4 5 6 71 2 3 4 5 6 71 2 3 4 5 6 71 2 3 4 5 6 71 2 3 4 5 6 7
![Page 14: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/14.jpg)
Write nested for loops
row_size = 5column_size = 7for row in range(row_size):
for column in range(column_size ):print(column+1, end=“ ”)
print()
• Loop within a loop. Look out for indentation!!
![Page 15: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/15.jpg)
Write nested for loops
• What will happen if you indent the second print also?
row_size = 5column_size = 7for row in range(row_size):
for column in range(column_size ):print(column+1, end=“ ”)print()
![Page 16: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/16.jpg)
Write nested for loops
• How to print out:
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5
![Page 17: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/17.jpg)
Write nested for loops
row_size = 5column_size = 7for row in range(row_size):
for column in range(column_size ):print(row+1, end=“ ”)
print()
• Just change column in 5th line to row
![Page 18: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/18.jpg)
Write nested for loops
• Finish second problem in Part1 , Assignment 4
Scalable Patterns: What do the following codes print?
• Understand the 1st problem in Part2
![Page 19: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/19.jpg)
String: Indexing
• str=“killer rabbit”
• Ex: str[0] returns “k” str[1] returns “i”
str[3] returns “l” str[-3] returns “b” str[-14] returns Error! Index out of range!
k i l l e r r a b b i t0 1 2 3 4 5 6 7 8 9 10 11 12
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
![Page 20: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/20.jpg)
String: Indexing
• Try this program (in textbook Chapter 4):import randomword = "index"print("The word is: ", word, "\n“)high = len(word)low = -len(word)for i in range(10): position = random.randrange(low, high) print("word[", position, "]\t", word[position])
![Page 21: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/21.jpg)
String: Indexing
• Count the occurrence of one letter in a string
str1=“killer rabbit”target =“i”count = 0for letter in str1:
if letter == target:count += 1
print(“There’re ” + str(count) + “ ‘i‘s in string: ” + str(str1) )
![Page 22: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/22.jpg)
Function len()
• Return an integer that represents how many elements are there in this specified sequence
>>> user = input(“Type a word: “)>>> user = “I like Python.”>>> len(user)14>>> user = “5198”>>> len(user)4
![Page 23: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/23.jpg)
String: methods
quote = "I like Python.“• quote.upper() -> capitalize everything
–“I LIKE PYTHON.”• quote.lower() -> small letter everything
–“I like python.” • quote.title() -> capitalize the first letter of every word
–“I Like Python.”
![Page 24: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/24.jpg)
String: methods
quote = "I like Python.“• quote.strip() -> removes spaces, tabs, newlines before and
after• quote.replace(“like”, “dislike programming in”)
–“I dislike programming in Python.”• Try quote.center(50)
![Page 25: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/25.jpg)
String methods vs Built-in functions
• When you want to use a string method, you’ve got to use the dot “.”:
>>> quote = "I like Python.">>> quote.upper()'I LIKE PYTHON.'>>> upper(quote) <- Error!• While built-in function>>> len(quote)>>> range(10)
![Page 26: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/26.jpg)
String: Indexing and slicing
• Understand the 2nd and 3rd problem in Part2
![Page 27: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062501/56816049550346895dcf716a/html5/thumbnails/27.jpg)
Have a nice evening! See you tomorrow~