Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
� /261
CS 1113-300 Introduction to Programming
Fall 2016
Exam 3 Review - Part 2 (Python) Friday, December 2nd, 2016
Ahmed Ibrahim
� /263
Touch-up
• Alexander’s office hours are on Sunday 2-5.
• Any problems with using py in MATLAB, Piazza.
• P3 is due by the end of Monday 12/5.
� /264
About the Test• Thursday, December 2, 2016.
• Your lecture hall (Thornton E316).
• Time: 3 hours (9:00AM - 12:00PM).
• Format: writing on paper; bring pen/pencil (nothing else).
• Bubble your first page (2 points).
� /265
About the Test• Students with accommodations should contact the SDAC to
schedule a time slot any time on Wednesday to take the test.
• There is Office Hours on Monday and Tuesday.
• No Office Hours will be held on Wednesday. (You can still e-mail and we’ll figure something out)
• Be here early!
� /266
What is on the Test• Python:
Arithmetic / Boolean / Strings / Expression Evaluation / Conditions / Loops (while / for) / Functions / Lists Call-by-value / Call-by-reference / Files / … etc (NO DICTIONARIES / RANDOM / Input Validation)
• MATLAB: Arithmetic / Boolean / Strings / Expression Evaluation / Conditions / Loops (while / for) / Spacing / Functions Multiple Output / … etc (Simple Plotting)
� /267
Type of Questions• True / False (Python & MATLAB)
• Multiple Choice (Python & MATLAB)
• Expression Evaluation (Python & MATLAB)
• Short Answer & Tracing (Python & MATLAB)
• Writing functions (Python & MATLAB)
• One “big” coding question (!?)
� /269
Remember• The algorithm must be:
- Unambiguous - Executable- Terminating
• Control Structures: sequence / condition / loop / subprogram
• Types of Errors: Syntax / Logical / Runtime
� /2610
Strings• The split() method:
fn, ln = input("Please enter your name: ").split() name_list = input("Please enter your name: ").split()
• String indexes:what is str[3]? what is str[-1]? what is str[len(str)-1]?what is str[ : 5]? what is str[3:7]? what is str[11 : ]?
str="WelcometoCS1113!"
� /2611
String Replication / Concatenation
• String Replication:
• String Concatenation:
• String Length:
str="10"*3#"101010"
len(str)#GETSTHELENGHTOFTHESTRING
str="10"+"3"#"103"
� /2612
Boolean Operators• Evaluate the following expressions:50 == “50” 0 < temp < 1006 > 2 and not ( 7 > 5 ) 6 > 2 or not ( 7 < 5 )
� /2613
Functions• Simpler code
• Code reuse
• Write the code once and call it multiple times
• Better testing and debugging
• Faster development
• Easier facilitation of teamwork
� /2614
Functions
• Can have 0 or more arguments
• Number of parameters passed MUST be the same as the number of args - unless arguments are optional!
• Can have 0, 1 or multiple return values
• Local variables can’t be accessed outside the function
� /2615
Functions• A function starts with a header def function_name (0_or_more_arguments): # code # x = 10 -> local variable # code # return statement # code
� /2618
Lists#1:Creatingalist values=[32,54,67.5,29,35,80,115,44.5,100,65]
#2:Accessingalistelement values[5]=87
� /2619
Out of Range Errors• Perhaps the most common error in using lists is accessing a nonexistent
element
• If your program accesses a list through an out-of-range index, the program will generate an exception at run time.
values=[2.3,4.5,7.2,1.0,12.2,9.0,15.2,0.5] values[8]=5.4 #Error––valueshas8elements, #andtheindexcanrangefrom0to7
� /2620
• The concatenation of two lists is a new list that contains the elements of the first list, followed by the elements of the second
• Two lists can be concatenated by using the plus (+) operator:
List Concatenation
myFriends=["Fritz","Cindy"] yourFriends=["Lee","Pat","Phuong"]
ourFriends=myFriends+yourFriends #SetsourFriendsto["Fritz","Cindy","Lee","Pat","Phuong"]
� /2621
• Replication of two lists is a new list that contains the elements of the first list, followed by the elements of the second.
• Results in the list [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2 ,3]
• The integer specifies how many copies of the list should be concatenated
• One common use of replication is to initialize a list with a fixed value
List Replication
month_in_quarter=[1,2,3]*4
monthly_scores=[0]*12
� /2622
Looping Over the List• Given a list that contains n elements, you can loop over the
list using one of the following ways:
#listindexusedforiinrange(len(values)):print(i,values[i])
#indexvaluesnotneeded(traverselistelements)forelementinvalues:print(element)
� /2623
Loop Over a Tabletable=[[3,4,5],[6,7],[8,9,10,11,12]]
forrowinrange(len(table)):forcolumninrange(len(table[row])):print(table[row][column])
� /2624
Reading From a File - for
infile=open("input.txt","r")
forlineininfile:#Processtheline.
infile.close()
� /2625
Writing to a File• To open a file for writing, you provide the name of the file as the first
argument to the open function and the string "w" as the second argument:
• If the output file already exists, it is emptied before the new data is written into it
• If the file does not exist, an empty file is created
outfile=open("output.txt","w")outfile.write("text\n")#stringonly