COSC 1336 | PyThon

ACC

Data Types

Foreword

In this course, we will cover the most commonly used Python data types as outlined in the Python Language Reference. The following four general data types are discussed in the course and, unless otherwise stated, will be used in examples: Numbers (Integer, Boolean, Real, Complex), Sequences (Immutable - String, Tuple, Bytes; Mutable - Lists, Byte Arrays), Mappings (Dictionary), and Sets.

Tuples are created with parentheses (), lists are created with square brackets [], dictionaries and sets are both created with braces {}. Sequences are covered below. Students are encouraged to experiment with sequence operations using both tuples and strings.

Data Types - Sequences

Sequences - the Python Language Reference describes sequences as finite, ordered, and indexed by non-negative numbers. This means the collections (or groups of items) are placed in order (one after another but not necessarily sorted) and each item can be accessed by an index number. Items are also known as elements. Sequences come in two categories: immutable and mutable. Let's look at the immutable sequence types first.

Immutable - sequences that cannot be changed after creation. These include strings, tuples, and bytes.

  • String - a sequence of Unicode characters such as: 'abc', '123', 'Hello my name is...'.
    • 128 ASCII characters; 1,112,064 Unicode possibilities
  • Tuple - a sequence of arbitrary Python objects such as: (1, 'a', 'Greetings'). Items in a tuple can be accessed with index values (a.k.a the 'ith item').
  • Bytes - a sequence of bytes (8-bits) in an immutable array represented by integers in the range of: 0<=x<256. Not covered in this course.

The operations in the table below are supported by most sequence types (both mutable and immutable).

Operations in Both Mutable and Immutable Sequences
x in s True if x in s
x not in s True if x not in s
s + t concatenation
s * n or n * s add s to itself n times
s[i] ith item
s[i:j] slice from i to j-1
s[i:j:k] slice from i to j-1 step k
len(s) length
min(s) smallest item
max(s) largest item
s.index(x[, i[, j]]) index of first x [at or after i and before j]
s.count(x) Number of x in s

 

In the code example below, tuples are created and used to demonstrate the operations in the table above. It should be noted that the same operations can be performed with strings.

 

Here's the output.

 

Mutable - sequences that can be changed after creation.

  • Lists - a sequence of arbitrary Python objects such as: [1, 'a', 'Greetings']. Lists are similar to tuples but lists can be changed and tuples cannot be changed. Lists can contain items of different types but usually contain items of the same type. Items in a list can be accessed with index values (a.k.a the 'ith item').
  • Byte Arrays - a sequence with the same functionality as the bytes type except that a bytearray is a mutable array. Not covered in this course.

The operations in the table below are supported only by mutable sequence types (NOT the immutable sequence types).

Operations in Mutable Sequences Only
s[i] = x item i replaced by x
s[i:j] = t replace slice with t
del s[i:j] delete slice
s[i:j:k] = t elements i to j, step k replaced with t
del s[i:j:k] elements i to j-1, step k are deleted
s.append(x) append x to end of sequence
s.clear() deletes all elements
s.copy() creates a shallow copy
s.extend(t) or s += t extends s with contents of t
s *= n makes s its content n times
s.insert(i, x) insert x at index i
s.pop([i]) return and remove item i
s.remove(x) remove first item x found
s.reverse() reverses s

 

In the code example below, lists are created and used to demonstrate the operations in the table above. Students are encouraged to experiment with sequence operations using lists. Note: s.copy() is not covered below but rather, it is covered extensively in the chapter 'Shallow v. Deep Copy'.

 

Here's the output.