100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Computer Science 144 3.1 summary $3.99   Add to cart

Summary

Computer Science 144 3.1 summary

 36 views  0 purchase
  • Course
  • Institution
  • Book

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

Preview 2 out of 6  pages

  • No
  • Unknown
  • September 28, 2023
  • 6
  • 2023/2024
  • Summary
avatar-seller
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.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller jaidendutoit. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $3.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

62491 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$3.99
  • (0)
  Add to cart