1 / 32

Review

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.

tokala
Download Presentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review Bernard 2007

  2. Numbers Many different types, we only provide 3 here for now: • Integer • Floating-point • Long integer

  3. 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

  4. Number in Action >>> b / 2 + a >>> b/(2.0+a) >>> b/(2+a)

  5. 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

  6. 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)

  7. Indexing • S = ‘STRINGINPYTHON’

  8. 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]

  9. Slicing • You can regard the slicing function as

  10. Slicing • S[1:3] • S[1:] • S[:3] • S[:-1] • S[:]

  11. 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”)

  12. String Conversion • Therefore, you can force adding one string to another number, and vise versa >>> int(“42”) +1 >>> “42” + str(1)

  13. Changing Strings • There’s a function called replace() >>> S= ‘string in python’ >>> s.replace( ‘string’, ‘number’)

  14. Lists • Lists are Python’s most flexibleordered collection object type • Lists can contain any sort of object: numbers, strings and even other lists

  15. List in action • List respond to the + and * operations much like strings >>> aa=[1,2,3] >>> bb=[4,5,6] >>> aa+bb >>> aa*3

  16. 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]

  17. 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)

  18. 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)

  19. 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()

  20. 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:]

  21. 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

  22. Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”

  23. Outline • Simple If statement • If... else statement • If… else if … else statement

  24. 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>

  25. If… else if … else statement • a=10 • if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

  26. Some More Logic Expression • and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False

  27. Some More Logic Expression • or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True

  28. For loop • The for loop is a generic sequence iterator in Python • It can step through the items in ANYordered sequence object

  29. 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

  30. 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

  31. 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)

  32. 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]

More Related