0% found this document useful (0 votes)
5 views1 page

Dsaa 20

d

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Dsaa 20

d

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

4 1 Python Programming 101

Fig. 1.2 A Reference and Object

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 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 deci-
mal 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 there is a string like ‘6’ and want to create an int
object from that string, you write code like that in Listing 1.1.

You might also like