Log in / Register
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.1 Chapter Goals

By the end of this chapter, you should be able to answer these questions.

• What two parts are needed for the accumulator pattern?

• When do you need to use the loop and a half pattern for reading from a file?

• What is the purpose of a class definition?

• What is an object and how do we create one?

• What is a mutator method?

• What is an accessor method?

• What is a widget and how does one use widgets in GUI programming?

1.2 Creating Objects

Python is an object-oriented language. All data items in Python are objects. In Python, data items that could be thought of as similar are named by a type or class. The term type and class in Python are synonymous: they are two names for the same thing. So when you read about types in Python you can think of classes or vice versa.

There are several built-in types of data in Python including int, float, str, list, and dict which is short for dictionary. These types of data and their associated operations are included in the appendices at the end of the text so you have a quick reference if you need to refer to it while programming. You can also get help for any type by typing help(typename) in the Python shell, where typename is a type or class in Python. A very good language reference can be found at, the official Python documentation website.

1.2.1 Literal Values

There are two ways to create objects in Python. In a few cases, you can use a literal value to create an object. Literal values are used when we want to set some variable to a specific value within our program. For example, the literal 6 denotes any object with the integer value of 6.

x = 6

This creates an int object containing the value 6. It also points the reference called

x at this object as pictured in Fig. 1.2. All assignments in Python point references

Fig. 1.2 A Reference and Object

at objects. Any time you see an assignment statement, you should remember that the thing on the left side of the equals sign is a reference and the thing on the right side is either another reference or a newly created object. In this case, writing x = 6 makes a new object and then points x at this object.

Other literal values may be written in Python as well. Here are some literal values that are possible in Python.

int literals: 6, 3, 10, −2, etc.

float literals: 6.0, −3.2, 4.5E10

str literals: 'hi there', “how are you”

list literals: [], [6, 'hi there']

dict literals: {}, {'hi there':6, 'how are you':4}

Python lets you specify float literals with an exponent.

So, 4.5E10 represents the float 45000000000.0. Any number written with a decimal point is a float, whether there is a 0 or some other value after the decimal point. If you write a number using the E or exponent notation, it is a float as well. Any number without a decimal point is an int, unless it is written in E notation. String literals are surrounded by either single or double quotes. List literals are surrounded by [ and ]. The [] literal represents the empty list. The {} literal is the empty dictionary.

You may not have previously used dictionaries. A dictionary is a mapping of keys to values. In the dictionary literal, the key 'hi there' is mapped to the value 6, and the key 'how are you' is mapped to 4. Dictionaries will be covered in some detail in Chap. 5.

1.2.2 Non-literal Object Creation

Most of the time, when an object is created, it is not created from a literal value. Of course, we need literal values in programming languages, but most of the time we have an object already and want to create another object by using one or more existing objects. For instance, if we have a string in Python, like '6' and want to create an int object from that string, we can do the following.

y = '6'

x = int(y)


In this short piece of code, y is a reference to the str object created from the string literal. The variable x is a reference to an object that is created by using the object that y refers to. In general, when we want to create an object based on other object values we write the following:

variable = type(other_object_values)

The type is any type or class name in Python, like int, float, str or any other type. The other_object_values is a comma-separated sequence of references to other objects that are needed by the class or type to create an instance (i.e. an object) of that type. Here are some examples of creating objects from non-literal values.

z = float('6.3') w = str(z)

u = list(w) # this results in the list ['6', '.', '3']

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science