Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 6 pages
Summary

Computer Science 144 3.1 summary

Document preview thumbnail
Preview 2 out of 6 pages

Detailed summary of chapter 3.1 about using data types in Python for CS144. All you need to study for chapter 3.1 for the A1

Content preview

3.1 USING DATA TYPES
Introduction:
- Object-oriented programming = a style of programming that integrates code and
data.
- Client programs use existing data types
- Constructors create objects from a data type, methods operate on their values

Methods:
A method is a function associated with a specified object; it corresponds to data-type
operation. We call (invoke) a method by using a variable name, followed by dot operator (.),
followed by the method name, followed by a list if arguments delimited by commas and
enclosed in brackets. Example: bits = x.bit_length().
Methods are similar to functions (except that each
method call is associated with an object), so you can
use it anywhere in your program where you would use
an expression.

Distinction between methods and functions:
A function call typically uses a module name whereas a
method call typically uses a variable name.

String processing:
Mantra: you do not need to know how a data type if implemented to be able to use it
Python’s string API can be divided into 3 categories:
1. Built-in operators: +, +=, [], [:], in, not in, and the comparison operators,
characterized by special symbols and syntax
2. A built-in function: len() with standard function-call syntax
3. Methods: upper(), startswith(), find(), etc. which are distinguished in the API with a
variable name followed by the dot operator

Built-in operators:
An operator (or function) that you can apply to more than one type of data = polymorphic
Examples: + can be applied to str and int; [] can be applied to str and lists/arrays

Built-in functions:
Polymorphic functions are like polymorphic operators, but without the special syntax.
Examples: len() makes sense for numerous data types.
Python automatically calls the implementation based on the type of argument

Methods:
We use built-in operators and functions for convenience, but we can also develop methods
that operate on object values.

, These 3 kinds of operations end up being the same in implementations. Python
automatically maps built-in operators and functions to special methods (special mehods
have double underscores before and after their names and start with a lower case letter).
Examples: s+t => s.__add__(t); len() => s.__len__() (*no double underscore in client code)


String processing applications: genomics:
Illustrates that a data type can be a well-developed encapsulation of an important
abstraction that is useful to clients.
potentialgene.py takes a DNA sequence as a command-line argument and determines
whether it corresponds to a potential gene: length is a multiple of 3, starts with the start
condon (ATG), ends with a stop condon (TAA, TAG, or TGA), and has no intervening stop
condos. -> uses Python’s language mechanisms, from polymorphic functions and operators
to methods that operate on object values.


A user-defined data type:
Example program: clientcharge.py -> computes the potential at various points in the plane
due to a given set of charged particles. Program creates and manipulate Charge objects.

Application programming interface:
The first entry in the API, which has the same name as the data type = constructor
The other two entries define the data-type operations
The built-in function str(), which returns a string representation of the charged particle

File conventions:
We define each data type in a distinct .py file, with the same name as the data type (but not
capitalized)
The format of the import statement that we use with user-defined data types differs from
the format we use with functions
Example for importing a data-type in client code: from charge import Charge

Creating objects:
To create an object from a user-defined data type, you call its constructor
Example, Charge(x0, y0, q0) creates a new Charge object with position (x0, y0) and charge
value q0 and returns a reference to the new object (the values x0, y0, and q0 belong to the
object). You can create any number of objects of the same data type each object has its own
identity, type, and value.

Connected book
 image
Robert Sedgewick, Kevin Wayne, Robert Dondero Introduction to Programming in Python
Publisher: 2015 ISBN: 9780134076430 Edition: Unknown

Document information

Summarized whole book?
No
Which chapters are summarized?
Unknown
Uploaded on
September 28, 2023
Number of pages
6
Written in
2023/2024
Type
Summary
$4.52

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
6
Followers
2
Items
14
Last sold
1 month ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions