Python Data Types and Structures

 

 

Programming languages store and process data of various types as variables, and are defined by their type. Datatypes dictate how the data is represented and what operations can be performed with the data. One method by which a datatype can be assigned to a variable is through casting, which is achieved using the appropriate constructor.

 

If a variable's data type is unknown, Python offers the following functionality to return the variable's type:

type(<variable>)

 

Python's built-in data types are broken out into five categories: Numeric, Boolean, Sequence, Mapping, and Sets. Generally, data types define the kind of data stored in a variable, and data structures define how the data is organized and stored. In other words, data structures are collections of data types.

 

These types are detailed below, along with some examples and useful Python methods that facilitate processing data of various types and structures. Practice implementing various data types through these exercises.

 

A screenshot of a computer

AI-generated content may be incorrect.

 

 

None



Numeric

 

 





Boolean

 



Sequence

 


String Methods

str()

Constructor

.upper()

Returns a variable with every letter in the string capitalized

.lower()

Returns a variable with every letter in the string lowercased

.title()

Returns a variable with the first letter in every word in the string capitalized

.strip()

Returns a variable with every letter in the string capitalized

.split(<delimiter>)

Returns a list strings from the original string split at the defined delimiter, if defined. No defined delimiter splits the string at the white spaces by default

.replace(<current_char>, <new_char>)

Returns a new string with all instances of the current string's selected characters, current_char, replaced with new characters, new_char

 

Strings can also contain escape characters, which generally represent illegal characters in a string. For example, double quotes are considered illegal characters because Python already recognizes single and double quotes as string syntax. Alternatively, a user can insert double quotes in a string by preceding that piece of the text with a backslash:

"My message to this planet we call \"Earth\" is Hello!"

 

Escape characters can also define how a string is spaced and distributed. These escape characters are placed directly in the text where the user wants a particular spacing or distribution to occur. Some escape characters include:

 

Strings can also be formatted to contain variables that are not explicitly defined. These variables might be defined programmatically or as user inputs and can be represented in a string with curly braces, {}. Examples of how this is achieved are shown below:

 

**A user is prompted to input the current_month and current_day as string values:

 

 

% is the placeholder for the variable in the string and 's' indicates that the variable is a string type. In this instance, we have multiple string inputs so the sequence in which the variables are defined determines the sequence in which they occur in the string

'Today\'s date is %s/%s' % (current_month, current_day)

 

 

Empty string variables are populated sequentially by the format variable's index

'Today\'s date is {}/{}'.format(current_month, current_day)

 

 

String variables containing integers are defined by the pertaining format variable's index

'Today\'s date is {0}/{1}'.format(current_month, current_day)

OR

'Today\'s date is {1}/{0}'.format(current_day, current_month)

 

 

String variables containing variable names are defined by format variable's assigned value

'Today\'s date is {month}/{day}'.format(month=current_month, day=current_day)

 


The 'f' is used to format the string literal directly (introduced in Python 3)

f'Today\'s date is {current_month}/{current_day}'



If a float type is being inserted into a string, it can be follow the methods above or be further formatted using the methods below:

 

** A user is prompted to input a price:



The price is inserted into a string and formatted to show 2 decimal places

'The price is {:.2f}'.format(price)



The price is inserted into a string literal and formatted to show 3 decimal places

f'The price is {price:.3f}'

 







Data Structures

·         Data structures are containers within which data can be stored. Data within data structures can be standalone data of the types defined above, or other data structures through a process known as nesting. Important data structure defining features include:

 

·         Ordered: Is the values' unique sequence in the structure constant throughout the structure's life, whereby the order of elements is an inherent characteristic?

·         Mutable: Can the structure's contents be modified or is it otherwise immutable?

·         Mixed Types: Does the structure only allow for one data type or can various data types be represented in the structure?

·         Repeatable: Are repeat values permitted in this structure or does it only contain unique values?

 

·         The four data structures to know are Lists, Tuples, Dictionaries, and Sets.

·         List | [ ]: List elements are accessed with their positional index value

o    Ordered: YES

o    Mutable: YES

o    Mixed Types: YES

o    Repeatable: YES

 

List Methods:

list()

Constructor

.append(<value>)

Appends value to the end of the list, in-place

.extend(<other_list)

Appends values in other_list to the end of the primary list, in-place

.insert(<index>, <value>)

Inserts value into the indicated index

.sort()

Sorts list values in-place

sorted(<list>)

Creates new list with sorted values

.remove(<value>)

Removes value from the list

.pop(<index>)

Removes value in indicated index from the list and returns it



o    Ordered: YES

o    Mutable: NO

o    Mixed Types: YES

o    Repeatable: YES

 

Tuple Methods

tuple()

Constructor

.count(<value>)

Returns the number of times the indicated value occurs in a tuple

.index(<value>)

Returns the index where the indicated value first occurs in a tuple

map(itemgetter(<index>), <tuple>)

Returns a list of values in each tuple's indicated index in a list of tuples

 

Values can be appended to tuples. However, given their immutable nature, there is a specific process by which this is achieved. Tuples can only be concatenated with other tuples, so the following process satisfies this requirement:

 

1.       Identify the primary tuple

primary_tuple=(1, 2, 3)

 

 

2.       Identify the other tuple within which values to append are contained

other_tuple=('a', 'b', 'c')

 

 

3.       Perform an addition of the primary tuple and other tuple, and assign to a variable

concat_tuple=primary_tuple + other_tuple

 

 

4.       The new tuple contains the primary tuple values and the other tuple values

concat_tuple=(1, 2, 3, 'a', 'b', 'c')

 


This is the only way to append values to an existing tuple. If an individual value is being appended to an existing tuple, it must first be cast as a tuple, thereby satisfying the tuples requirement:

-

primary_tuple=(1, 2, 3, 4)

concat_tuple=primary_tuple + (4, )

concat_tuple=(1, 2, 3, 4)

 

OR

 

primary_tuple=(1, 2, 3, 4)

concat_tuple=primary_tuple + ('a', )

concat_tuple=(1, 2, 3, 'a')

 

 

Note that the parentheses when defining a tuple are optional, as Python recognizes when a tuple is being created:

 

x = 5, 11

 

is the same as

 

x = (5, 11)

 

 

The parentheses indicate how the variables are stored in the collection. When the tuple is a part of another collection, the parentheses are necessary to define the structure. For example:

 

y = [5, 11]

 

is NOT the same as

 

z = [(5, 11)]

 

is NOT the same as

 

a, b = 5, 11

 

 

In the examples above, variable 'y' is a list of values, whereas variable 'z' is a list comprised of a tuple. The variables 'a, b' are assigned integers 5 and 11, respectively, through what is known as destructuring or decomposing.

 

Python can assign the appropriate data type and data structure with the syntax provided.



Mapping

 


Dictionary Methods

dict()

Constructor

.items()

Returns (key, value)

.keys()

Returns a list of all dictionary keys

.values()

Returns a list of all dictionary values

.get(<key_name>)

Returns values in the indicated key

.pop(<key_name>)

Removes indicated key-value pair from the dictionary and returns just the value

.update({<key_name>: <value>})

Inserts key-value pair into the dictionary

.clear()

Removes all elements from the dictionary





Sets

o    Ordered: NO

o    Mutable: NO

o    Mixed Types: YES

o    Repeatable: NO

 

NOTE: 1 and True, and 0 and False are considered the same value in sets, so the Boolean value and equivalent numeric value cannot exist in the same set

 

Set Methods

set()

Constructor

.add(<value>)

Adds indicated value to the set

.union(<other_set>)

Returns a set with the unique values from the primary set and other_set

.update(<other_set>)

Inserts values from other_set into the primary set, updating the original set

.remove(<value>)

Removes indicated value from the set, raising an error if the value does not exist

.discard(<value>)

Removes indicated value from the set, not raising an error if the value does not exist

.clear()

Removes all elements from the set

.difference(<other_set>)

​Returns a set with the items that exist in the primary set but not in other_set

.intersection_update(<other_set>)

Updates the primary set, retaining only values from the primary set that also exist in other_set

.intersection(<other_set>)

Returns a new set with values that exist in the primary set and other_set

.symmetric_difference(<other_set>)

​Returns a new set with values that exist in the primary set and other_set

 

 

 

Helpful Resources