4-2. Sequences - 5 SEQUENCES STRINGS LISTS AND FILES(PART...

Info icon This preview shows pages 1–12. Sign up to view the full content.

5. SEQUENCES: STRINGS, LISTS AND FILES (PART II) Rocky K. C. Chang September 16, 2015 (Adapted from John Zelle’s slides)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Objectives To understand the concept of objects, an active data type. To understand how string objects and list objects works in Python. To understand basic file processing concepts and techniques for reading and writing text files in Python. To be able to understand and write programs that process textual information.
Image of page 2
Native data types So far, we have learned some native data types in Python. These data types are “passive” in the sense that they are just data and they cannot compute. Some problems: We cannot easily use these data types to model real-life objects in our problem solving. E.g., a circle (object) needs three float data, and a student record (object) needs numbers and strings.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

The concept of objects Languages that support object-oriented features provide object data types, such as strings and lists. Example: myName = “Rocky Chang” myGrades = ["A+", "A", "B+", "B"] The difference with the object data types? Each object contains data (which are generally more complex). Each object also has methods operated in the data. In a program, we could request an object to perform operation for us.
Image of page 4
EXERCISE 5.1 o Create a string object, such as student = “your_first_name, your_last_name, your student ID” . o Invoke some methods on the object, such as student.split(), student.lower(), student.upper(), student.capitalize() .
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Rocky Chang Methods split() lower() upper() capitalize() Data A string object student Your program
Image of page 6
String methods For Python 3.0: str.capitalize() str.casefold() str.center(width[, fillchar]) str.count(sub[, start[, end]]) ... str.title() str.translate(map) str.upper() str.zfill(width)
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

EXERCISE 5.2 Implement your pseudo-code for exercise 4.12. You may consider using the split() method.
Image of page 8
Consider a solution to Exercise 5.2.
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Lists are also objects. One of the methods is append() . What do these codes give us? squares = [] for x in range(1,10): squares.append(x*x) print(squares ) Other methods in https:// docs.python.org/3/library/stdtypes.html#common-sequenc e-operations .
Image of page 10
Turning a list into a string s.join(list) : concatenate list into a string, using s as a separator.
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 12
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '16
  • Rocky Chiang

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern