320 likes | 418 Views
Review. Bernard 2007. Numbers. Many different types, we only provide 3 here for now: Integer Floating-point Long integer. Number in Action. >>> a=3 >>> b=4 >>> a%2, b**2 #remainder, power (1,16) >>> 2+4.0, 2.0**b #mix type (6.0,16.0) >>> c*2 #error.
E N D
Review Bernard 2007
Numbers Many different types, we only provide 3 here for now: • Integer • Floating-point • Long integer
Number in Action >>> a=3 >>> b=4 >>> a%2, b**2 #remainder, power (1,16) >>> 2+4.0, 2.0**b #mix type (6.0,16.0) >>> c*2 #error
Number in Action >>> b / 2 + a >>> b/(2.0+a) >>> b/(2+a)
Some Numeric Tools >>> abs(-42), 2**4, pow(2,4) (42, 16, 16) >>>int(2.567), round(2.567), round(2.4) (2, 3.0, 2.0) >>> round(2.567, 2) 2.569999999999998
Strings • The next major build-in type is the Python STRING --- an ordered collection of characters to store and represent text-based information • Python strings are categorized as immutable sequences --- meaning they have a left-to-right order (sequence) and cannot be changed in place (immutable)
Indexing • S = ‘STRINGINPYTHON’
Indexing • As in the C language, Python offsests start at zero and end at one less than the length of the string • S[0] fetches the item at offsest 0 from the left • S[-2] gets the item offset 2 from the end >>> S[0], S[5], S[-2]
Slicing • You can regard the slicing function as
Slicing • S[1:3] • S[1:] • S[:3] • S[:-1] • S[:]
String Conversion • You cannot add a number and a string together >>> “42” + 1 • You can use int() function to convert string into integer >>> int(“42”)
String Conversion • Therefore, you can force adding one string to another number, and vise versa >>> int(“42”) +1 >>> “42” + str(1)
Changing Strings • There’s a function called replace() >>> S= ‘string in python’ >>> s.replace( ‘string’, ‘number’)
Lists • Lists are Python’s most flexibleordered collection object type • Lists can contain any sort of object: numbers, strings and even other lists
List in action • List respond to the + and * operations much like strings >>> aa=[1,2,3] >>> bb=[4,5,6] >>> aa+bb >>> aa*3
Changing Lists in-place • When using a list, you can change its contains b assigning to a particular item (offset), or an entire section (slice) >>> aa=[1,2,3] >>> aa[0]=4 >>> aa[1:]=[5,6]
List method calls The list append method simply tacks a single item onto the end of the list >>> aa=[] >>> aa.append(1) >>> aa.append(2) >>> aa.append(3) >>> aa.append(‘4’) >>> aa.append(5.0)
List method calls • The sort function orders a list in place (in ascending fashion) >>> aa=[4,2,6,8,1,3,4,10] >>> aa.sort() (also try strings in lists)
List method calls • ‘reverse’ reverse the list in-place >>> aa=[1,2,3,4] >>> aa.reverse() • ‘pop’ delete an item from the end >>> aa.pop()
List method calls • ‘del’ can used to delete an item or section >>> aa=[1,2,3,4,5,6] >>> del aa[0] >>> del aa[2:]
If Statement • The main statement used for selecting from alternative actions based on test results • It’s the primary selection tool in Python and represents the Logic process
Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”
Outline • Simple If statement • If... else statement • If… else if … else statement
If… else if … else statement • It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if <test1>: <statement1> elif <test2>: <statement2> else: <statement3>
If… else if … else statement • a=10 • if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”
Some More Logic Expression • and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False
Some More Logic Expression • or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True
For loop • The for loop is a generic sequence iterator in Python • It can step through the items in ANYordered sequence object
For loop • The Python for loop begins with a header line that specifies an assignment target along with an object that you want to step through for <target> in <object>: <statement> • When Python runs a for loop, it assigns item in the sequence object to the target “one by one”, and then executes the loop body
For loop • The name used as the assignment target in a for header line is usually a variable in the scope where the for statement is coded • After the loop, this variable normally still refers to the last item visited
Counter loops: range • The range function is really independent of for loops; although it’s used most often to generate indexes in a for loop • There are three formats of range: >>> range(5) >>> range(2,5) >>> range(0,10,2)
range in for loop aa= [90,80,75,60,80,77,65,30,50,100] • If we only want to compute the sum of first 5 scores: >>> sum=0 >>> for i in range(5): >>> sum=sum + aa[i] • If we only need the even number student’s score: >>> sum=0 >>> for i in range(0,len(aa),2): >>> sum=sum + aa[i]