2 Python Data Types V2
2 Python Data Types V2
Data types
Sequences types: Lists, Tuples, and
Strings
Mutability
Understanding Reference Semantics in
Python
Basic Datatypes
Integers (default for numbers)
z = 5 / 2 # Answer 2, integer division
Floats
x = 3.456
Strings
• Can use ”…" or ’…’ to specify, "foo" == 'foo’
• Unmatched can occur within the string
“ John’ s” or ‘ John said “ foo!” .’
• Use triple double-quotes for multi-line strings or
strings than contain both ‘ and “ inside of them:
“““a‘ b“ c”””
Python’s data types
Everything is an object
Python data is represented by objects or by
relations between objects
Every object has an identity, a type and a value
Identity never changes once created Location
or address in memory
Type (e.g., integer, list) is unchangeable and
determines the possible values it could have
and operations that can be applied
Value of some objects is fixed (e.g., an integer)
and can change for others (e.g., list)
Python’s built-in type hierarchy
Sequence types:
Tuples, Lists, and
Strings
Sequence Types
Sequences are containers that hold objects
Finite, ordered, indexed by integers
Tuple: (1, “a”, [100], “foo”)
An immutable ordered sequence of items
Items can be of mixed types, including collection types
Strings: “foo bar”
An immutable ordered sequence of chars
• Conceptually very much like a tuple
List: [“one”, “two”, 3]
A Mutable ordered sequence of items of mixed types
Similar Syntax
All three sequence types (tuples,
strings, and lists) share much of the
same syntax and functionality.
Key difference:
• Tuples and strings are immutable
• Lists are mutable
The operations shown in this section
can be applied to all sequence types
• most examples will just show the
operation performed on one
Sequence Types 1