2.3. Built-in Types and Functions#

Hide code cell source

import sys
from pathlib import Path

current = Path.cwd()
for parent in [current, *current.parents]:
    if (parent / '_config.yml').exists():
        project_root = parent  # ← Add project root, not chapters
        break
else:
    project_root = Path.cwd().parent.parent

sys.path.insert(0, str(project_root))

from shared import thinkpython, diagram, jupyturtle, download, structshape

# Register as top-level modules so direct imports work in subsequent cells
sys.modules['thinkpython'] = thinkpython
sys.modules['diagram'] = diagram
sys.modules['jupyturtle'] = jupyturtle
sys.modules['download'] = download
sys.modules['structshape'] = structshape

Python has standard types built into the interpreter. The principal built-in types are: numerics, sequences, mappings, classes, instances, and exceptions. The commonly used data types can be organized as Fig. 2.1.

This chapter builds upon the variables and data types introduced earlier, providing deeper coverage of Python’s type system and advanced operations.

2.3.1. About Types#

Python is dynamically typed, which means you don’t have to declare the data type when creating a variable. The interpreter will figure it out at run time. For example, the x below will receive proper types without user intervention.

x = 10
x = "hello"
x = [1, 2, 3, ]
x[0] = "M S&T"

A kind of value is called a type. Actually, every value has a type – or we sometimes say it “belongs to” a type. Python provides a function called type() that tells you the type of any value.

a = type(1)
b = type(2.0)
c = type(3/2)
d = type('Hello, World!')
e = type('126')       ### note this is a string, not a number

print(a, b, c, d, e, sep='\n')
<class 'int'>
<class 'float'>
<class 'float'>
<class 'str'>
<class 'str'>

type and the built-in function isinstance() work similarly, except isinstance() allows you to check the type you assume and returns True or False.

print(isinstance(1, int))
print(isinstance('126', str))
True
True

2.3.1.1. Common Built-in Types#

Python has standard types built into the interpreter. The principal built-in types are: numerics, sequences, mappings, classes, instances, and exceptions. The commonly used data types can be organized as table below.

In the Python standard library, they are grouped into 8 categories:

Group

No.

Category

Types

Remarks

Sample Use Case

Literals

1

Numeric

int, float, complex

Numbers for mathematical operations

Counts, indices, ages, prices, measurements

2

Text sequence

str

Text and character data

Names, messages, file paths

3

Boolean

bool

Logical values (True/False)

Flags, conditions, toggle states

4

Null

NoneType

Represents absence of value

Absence of value, default state

Collections

5

Sequence

list, tuple, range

Ordered collections of items

Shopping cart items, student grades, coordinates, RGB colors

6

Binary

bytes, bytearray, memoryview

Binary data and memory manipulation

Image data, encrypted content

7

Set

set, frozenset

Unordered collections of unique items

Removing duplicates, membership testing

8

Mapping

dict

Key-value pairs

User profiles, configuration settings

../../_images/python-data-types-2.png

Fig. 2.1 Python built-in data types #

2.3.1.2. Type Hierarchy#

The standard type hierarchy in the Python Language Reference lists 8 different built-in data types with a slightly different categorization:

No.

Category

Type(s)

Remarks

1

Null

NoneType

None, represents absence of value

2

NotImplemented

NotImplementedType

NotImplemented, a built-in constant indicating that an operation is not implemented for a particular type combination

3

Ellipsis

Ellipsis

..., a placeholder, explicitly signals intentional incompleteness

4

Numeric

integers (int and bool), float, complex

Numbers for mathematical operations and Boolean (True/False)

5

Sequence

str, tuple, bytes

immutable; ordered , indexed, and slicible collections of items; support slicing

list, bytearray

mutable; ordered, indexed, and slicible collections of items

6

Set

set, frozenset

Unordered collections of unique items; set is mutable and frozenset is not

7

Mapping

dict

Key-value pairs

8

Callable

User-defined functions

Instance methods

Generator functions

Coroutine functions

Asynchronous generator functions

Built-in functions

Built-in methods

Classes

Class Instances

2.3.1.3. Type Checking#

The built-in function type() returns the data type of the object; in this case, variables.

num1 = 10                   ### integer
num2 = 10.1                 ### floating-point number
greeting = "hello, world"   ### text/string
fruits = ['Apple', 'Banana', 'Cherry']      ### lists are enclosed with square brackets

print(type(num1))
print(type(num2))
print(type(greeting))
print(type(fruits))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>

2.3.1.4. Type Conversion#

Type conversion is the general process of converting a value from one data type to another. There are two ways of type conversion to change type:

  • Type Casting (explicit conversion) is performed by the programmer (e.g., using int("42")) using the type constructors.

  • Type Coercion (implicit conversion) is performed automatically by the Python interpreter (e.g., 3 + 4.5 results in 7.5, where the integer 3 is coerced to a float to match the other operand).

2.3.1.4.1. Type Constructors#

Core Python types that are also constructor functions include:

Function

Converts To

Example

Explanation

int()

Integer

int("10") 10

Converts string “10” to integer 10

float()

Floating-point number

float("3.14") 3.14

Converts string “3.14” to floating-point 3.14

str()

String

str(25) "25"

Converts integer 25 to string “25”

bool()

Boolean (True/False)

bool(0) False, bool(5) True

0 converts to False, non-zero values convert to True

list()

List

list("abc") ['a','b','c']

Converts string into list of individual characters

tuple()

Tuple

tuple([1,2,3]) (1,2,3)

Converts list to immutable tuple

set()

Set

set([1,1,2]) {1,2}

Converts list to set, removing duplicate values

As an example of type casting, let’s cast an integer to a float.

num = 1
print(num)
print(type(num))

num = float(num)
print(num)
print(type(num))
1
<class 'int'>
1.0
<class 'float'>

Type coercion is performed automatically by the Python interpreter at runtime. The Python interpreter decides the data type.

### type coercion

a = 7             # int
b = 3.0           # float
c = a + b         # Python automatically converts 'a' to a float (7.0) before addition
print(c)          # Output: 10.0
print(type(c))    # Output: <class 'float'>
10.0
<class 'float'>

Examples of type casting vs type coercion:

### type casting
num = 5
print("num's type:\t", type(num))
print("num's new type:\t", type(str(num)))  ### casting (explicit conversion) by programmer

### type coercion
x = 3               ### integer
y = 0.14            ### float
print(f"{x} + {y} is {x+y} and has the type: {type(x + y)}")        
                    ### int + float = float done automatically by Python interpreter
                    ### coercion (implicit conversion)  
num's type:	 <class 'int'>
num's new type:	 <class 'str'>
3 + 0.14 is 3.14 and has the type: <class 'float'>
### Exercise: Type Conversion
### Print the data type of variable 'num' after the addition
### The result should be as the cell below
### Your code begins here

num = "100.1"
num = float(num) + 1.0


### Your code ends here

Hide code cell source

print(type(num))
<class 'float'>

2.3.1.5. Type Hinting#

Type hinting, also called type annotation, lets you write the expected type next to a variable name. A type hint is a note for humans and development tools; Python does not enforce it by itself when the program runs.

At this point, focus on the basic idea:

age: int = 20
name: str = "Alice"
height: float = 5.9
is_active: bool = True

The annotation after the colon says what kind of value the variable is expected to hold. Later, when we define our own functions, we will use the same notation for function parameters and return values:

def add(a: int, b: int) -> int:
    return a + b

For now, remember two rules:

  • type hints improve readability and help tools catch mistakes early;

  • type hints do not convert values or stop the program at runtime.

num: int = 10               ### type hinting for integer
name: str = "Alice"         ### type hinting for string
is_active: bool = True      ### type hinting for boolean
height: float = 5.9         ### type hinting for float

2.3.2. Python Built-in Functions#

In Python, built-in functions and built-in modules are both part of the standard tools the language gives you, but they serve different purposes. Built-in functions are ready to use without requiring any imports. They are automatically available in every Python program. Modules, on the other hand, need to be imported to use. For the functionalities that Python does not provide, we either

  1. Find a third-party package (module or library) with the functions, install the package, and use the contained functions, or

  2. Write a user-defined custom function.

Built-in vs custom functions: Python ships with functions like print() and len(), and you can also create your own custom functions.

Python built-in functions are tools for quick operations (such as length, conversion, and output). A few of them that you will use constantly:

print("Hello!")              # Output to screen

len_num = len([1, 2, 3])     # 3: len() get the length of the argument
num = int("42")              # 42 (string → int); int() is a type constructor
sum_num = sum([1, 2, 3])     # 6 (sum a list sequence)
max_num = max(5, 2, 9)       # 9

print(len_num)
print(num)
print(sum_num)
print(max_num)
Hello!
3
42
6
9

In the Python Standard Library , you can find all the Python built-in functions listed:

../../_images/python-builtin-functions.png

Fig. 2.2 Python Built-In Functions #

We may group the 71 built-in functions by their purposes.

Group

Functions

Notes

Numbers & math

abs, divmod, max, min, pow, round, sum

pow(a, b, mod=None) supports modular exponentiation.

Type construction/conversion

bool, int, float, complex, str, bytes, bytearray, memoryview, list, tuple, set, frozenset, dict, range

Convert or construct core types.

Object/attribute introspection

type, isinstance, issubclass, id, hash, dir, vars, repr, ascii

vars(obj)obj.__dict__ when available.

Attribute access

getattr, setattr, delattr, hasattr

Dynamic attribute management.

Iteration & functional tools

iter, next, enumerate, zip, map, filter, sorted, reversed

Prefer comprehensions when clearer.

Sequence/char helpers

len, ord, chr, slice

len() works on many containers.

I/O

print, input, open

open returns a context manager; prefer with open(...) as f:.

Formatting / representation

format, bin, oct, hex

Also see f-strings for formatting.

Object model (OOP helpers)

object, property, classmethod, staticmethod, super

Define descriptors and class behaviors.

Execution / metaprogramming

compile, eval, exec

Use with care; security concerns for untrusted input.

Environment / namespaces

globals, locals

Introspection of current namespaces.

Help/debugging

help, breakpoint

breakpoint() respects PYTHONBREAKPOINT.

Import

__import__

Low-level import; usually use import statement instead.

But Python has more than the built-in functions listed above. Let’s look at arithmetic/math functions as an example. You see that Python provides three groups of arithmetic functions:

  1. built-in functions (the 7 functions as listed in the table above)

  2. operator module functions

  3. math module functions

For the operator module, there are math functions to perform the same operations as the arithmetic operators, which you will need to perform operations such map and reduce:

import operator
operator.add(3, 2)       # 5
operator.sub(3, 2)       # 1
1

The math module functions do better with high-level arithmetic, as shown below. These functions are self-explanatory.

import math

Function

Purpose

Example

math.sqrt(x)

Square root

math.sqrt(16)

4.0

math.factorial(x)

Factorial

math.factorial(5)

120

math.ceil(x)

Round up

math.ceil(3.2)

4

math.floor(x)

Round down

math.floor(3.9)

3

math.prod(iterable)

Multiply all items

math.prod([2, 3, 4])

24

math.fabs(x)

Absolute (always float)

math.fabs(-7)

7.0

math.isfinite(x)

Finite number?

math.isfinite(2)

True

2.3.2.1. Arguments#

When you call a function, the expression in parentheses is called an argument. For example:

int('101')           ### take one argument
math.pow(5, 2)       ### take two
int('101', 2)        ### take additional optional arguments, such as base 2 here
round(math.pi, 3)    ### takes an optional second argument, **decimal places** 
print('Any', 'number', 'of', 'arguments')   ### multiple arguments
Any number of arguments

2.3.2.2. Return Values#

When you call built-in functions such as abs, round, sqrt, and pow, they return a value you can assign to a variable or use as part of an expression.

Some use the print function to display values, but they don’t return values we assign to variables or use in expressions.

import math

math.sqrt(42 / math.pi)            ### returns value
3.656366395715726
radius = math.sqrt(42 / math.pi)    ### assign to variable
radius
3.656366395715726

If a function doesn’t have a return statement, it returns None, which is a special value like True and False.

You have used the print function to display a string, but it does not use a return statement to return a value. If we assign the result to a variable, it still displays the string.

print(print())   ### returns None
None

2.3.3. Basic Types#

2.3.3.1. Numbers#

Python has three basic number types: integers (e.g., 1), floating-point numbers (e.g., 1.0), and complex numbers. The standard mathematical order of operations is followed for basic arithmetic operations. Note that dividing two integers results in a floating-point number, and dividing by zero will generate an error.

integer = 42             # integer (natural number)
floating = 3.14          # floating-point number (real number)
complex_num = 3 + 4j     # complex number

print(f"Integer: {integer}")
print(f"Floating-point number: {floating}")
print(f"Complex number: {complex_num}") 
Integer: 42
Floating-point number: 3.14
Complex number: (3+4j)
num1 = 1 + 1
num2 = 1 * 3         
num3 = 1 / 2
num4 = 2 / 2             ### output 1.0, not 1

print(num1, type(num1))
print(num2, type(num2))
print(num3, type(num3))
print(num4, type(num4))
2 <class 'int'>
3 <class 'int'>
0.5 <class 'float'>
1.0 <class 'float'>

The modulus operation (also known as the mod function) is represented by the percent sign. It returns what remains after the division:

print(4 % 2)
print(5 % 2)
print(9 // 2)
0
1
4
### Exercise: How many hours and minutes are there in 12500 seconds?
### Use numeric operators
### Your code starts here



### Your code ends here

Hide code cell source

### solution
total_seconds = 12500

remaining_seconds = total_seconds % 60
minutes = total_seconds // 60
remaining_minutes = minutes % 60
# hours = total_seconds // 3600  # 3600 seconds in an hour
hours = minutes // 60  # 60 minutes in an hour

print(f"{hours} hours, {remaining_minutes} minutes, and {remaining_seconds} seconds")
3 hours, 28 minutes, and 20 seconds

2.3.3.2. Strings#

Python represents sequences of letters, which are called strings because the letters are strung together like beads on a necklace. Strings are one of the most commonly used built-in types.

Strings represent text (character sequences) and are created using quotes. Strings can be created using single or double quotes. You can also wrap a single quote in double quotes if you need to include a quote within the string.

Some features about strings in Python:

  • A string is a sequence of characters enclosed in quotes.

  • You can use single, double, or triple quotation marks to create a string; they are all legal.

  • Double quotes make it easy to write a string that contains an apostrophe, which is treated the same as single quotes in programming.

2.3.3.2.1. Creating String Variables#

name1 = "Alice"
name2 = 'Alice'
name3 = """Alice"""
print(name1, name2, name3)
Alice Alice Alice

2.3.3.2.2. Quotation Marks & Escaping#

An escape sequence is a combination of a backslash \ with a special character/symbol to treat the special characters as regular strings.

Observe the following two strings. You see that we are trying to honor the single quote in a pair of double quotes. For that, you can either:

  • enclose the single quotation mark inside the double quotation marks as in the first example, or enclose the double quote with single quotes like the 2nd.

  • use an escape sequence. Note that in s3, we have three single quotation marks, and we are able to produce the same results as the first example.

  • Starting from s4, we have a syntax error because the quotation marks are not properly closed.

s1 = "It's a sunny day."   ### double quote enclose single
s2 = 'It"s a sunny day.'   ### ... but legal
s3 = 'It\'s a sunny day.'  ### escape single quote
print(s1, s2, s3, sep="\n")
It's a sunny day.
It"s a sunny day.
It's a sunny day.

The enclosing quotations have to be symmetrical, otherwise you would have a syntax error.

%%expect SyntaxError

s4 = 'It's a sunny day.'   ### illegal: not closed
s5 = "It"s a sunny day."   ### illegal: not closed
print(s4, s5, sep="\n")
  Cell In[26], line 1
    s4 = 'It's a sunny day.'   ### illegal: not closed
                           ^
SyntaxError: unterminated string literal (detected at line 1)

Commonly used escape sequences are as follows.

Sequence

Meaning

\'

Single quote inside single-quoted string

\"

Double quote inside double-quoted string

\\

Backslash literal

\n

Newline

\t

Tab

Some examples of escape sequences are:

print("First line \n Second line")      ### \n: gives new line (break)
print("Name:\tDoris")                   ### \t: tab
print("He said \"Python is great!\"")   ### print " inside ""
print('I\'m learning Python')           ### show ' as a character, not quotation mark
First line 
 Second line
Name:	Doris
He said "Python is great!"
I'm learning Python

Observe and see if the print output makes sense to you.

print("PS C:\\Users\\tychen>")          ### \\ to show \
PS C:\Users\tychen>

2.3.3.2.3. Common string operations#

  1. Concatenation: "Hello" + " " + "World""Hello World"

  2. Repetition: "Ha" * 3"HaHaHa". The multiplication (*) operator also works with strings; it makes multiple copies of a string and concatenates them.

  3. Length: len("Python")6. Python provides a useful function called len that computes the length of a string. Notice that len counts the letters between the quotes, but not the quotes. In collection types, len counts the number of elements in the collection.

  4. Indexing: "Python"[0]"P"

  5. Slicing: "Python"[0:3]"Pyt"

Here we have one example for each of the string operations:

name = "Alice"
greeting = "Hello, " + name + "!"              ### 1. concatenation

print("1.", greeting)                           
print("2.", greeting * 3)                      ### 2. repetition
print(f"3. Length: {len(name)}")               ### 3. length + f-string
print(f"4. First letter: {name[0]}")           ### 4. indexing + f-string
print(f"5. First three letters: {name[0:3]}")  ### 5. slicing + f-string
1. Hello, Alice!
2. Hello, Alice!Hello, Alice!Hello, Alice!
3. Length: 5
4. First letter: A
5. First three letters: Ali

Exercise: String Manipulation

  • What will be the output of this code?

  • Guess first and then activate Live Coding and run the code in the cell below to find out.

s = "hello"
t = s
s = s.upper()
print(t)

A) HELLO
B) hello
C) Error
D) None

### Use this code cell to test.



###

Hide code cell source

### solution: comment out the print statement 
#   to see the output of t

s = "hello"
t = s
s = s.upper()
# print(t)

2.3.3.2.4. Indexing and Slicing#

Strings are sequences of characters. You can access specific elements using square bracket notation. Python indexing starts at zero. Negative indexing in slicing starts with -1 from the end element.

s = 'hello'
print(s[0])
print(s[4])
print(s[-1])
h
o
o

Slice notation allows you to grab parts of a string. Use a colon to specify the start and stop indices. The stop index is not included.

s = 'applebananacherry'
print(s[0:])     ### applebananacherry
print(s[:5])     ### apple
print(s[5:11])   ### banana
print(s[-6:])    ### cherry
print(s[-6:0])   ### 
print(s[-6:-1])  ### cherr (stop exclusive)
applebananacherry
apple
banana
cherry

cherr

2.3.3.3. Boolean Type#

The type of a Boolean value is bool. The only two Boolean values are True and False. They are built-in keywords and must be capitalized.

Booleans represent truth values and are used extensively in conditional comparisons and logical expressions, which involve comparison/relational operators and logical operators.

3 > 5
False
num1, num2, num3, num4 = 1, 2, 3, 4

if (num1 > num2):                  ### comparison operator ">"
    print("num1 is greater than num2.")
else:
    print("num1 is not greator than num2.")
num1 is not greator than num2.
is_student = True
is_graduated = False

print(f"Is student: {is_student}")      
print(f"Is graduated: {is_graduated}")   
Is student: True
Is graduated: False
if is_student:                          ### in this case, True
    print("The person is a student.")
else:
    print("The person is not a student.")
The person is a student.

2.3.3.3.1. Boolean Expressions#

A boolean expression is an expression that is either True or False. For example, the following expressions use the equals operator, ==, which compares two values and produces True if they are equal and False otherwise. Remember that there are six relational/comparison operators: == != > < >= <=.

t_f_1 = 5 == 5
t_f_2 = 5 == 7
print(t_f_1, t_f_2)
True False

A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = assigns a value to a variable and == compares two values.

x = 5       ### assignment
y = 7       ### assignment
x == y      ### comparison/relational
False

True and False are special values that belong to the type bool; they are not strings:

print(type(True))
print(type(False))
<class 'bool'>
<class 'bool'>

The == operator is one of the 6 relational operators; the others are:

x != y               # x is not equal to y
x > y                # x is greater than y
x < y                # x is less than y
x >= y               # x is greater than or equal to y
x <= y               # x is less than or equal to y
True

2.3.3.3.2. Truthy and Falsy#

In Python, every object has a truth value in Boolean contexts like if and while. “Truthy” values behave like True, while “falsy” values behave like False - empty containers and zero are falsy; most non-empty values are truthy.

  • Falsy: False, None, 0, 0.0, '', [], {}, set()

  • Truthy: most other values (e.g., '0', [0]).

  • Quick check: bool(value);

  • idiom: if not items: checks emptiness.

# Minimal truthy/falsy examples
print(bool(0)), print(bool(''))        # False
print(bool('0')), print(bool([0]))     # True
False
False
True
True
(None, None)

2.3.4. Collection Types#

Python’s data structure are the built‑in container types you use to store, organize, and operate on groups of items/values. The four main data structure types are: list, tuple, dictionary, and set.

The commonly used built-in/standard data structures can be grouped as:

  • Sequence Types

  • Set Types

  • Mapping Types

Collections (broad category)
├── Sequence Types (ordered, indexed)
│   ├── list
│   ├── tuple
│   ├── str (string)
│   ├── range
│   └── bytes/bytearray
│
├── Set Types (unordered, no duplicates)
│   ├── set
│   └── frozenset
│
└── Mapping Types (key-value pairs)
    └── dict

Among them, list, tuple, and range are sequence types . Also, strings are considered a sequence type (a kind of collection) because they behave like collections of characters, although conceptually, in many languages other than Python, strings are considered primitives, not collections.

range is debatable as a “collection”; it’s really a lazy sequence generator, not a traditional data structure. It does look like a sequence, so Python treats it as one. Note that range stores a formula, not data; it computes values on demand. Therefore, range is a sequence protocol implementer, not a data container.

A set object is an “unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference” [cite:pPython Standard Library_2026].

Mapping in Python refers to data types that store key-value pairs, where each key is associated with a corresponding value. The most common mapping type is the dictionary (dict).

2.3.4.1. Mutability#

Mutability and order are two important characteristics of Python data structures, summarized in the table below.

Type

Literal

Mutable

Ordered

Usage

Sequence Types

list

[1, 2, 3]

Yes

Yes

General purpose; dynamic arrays; index/slice access.

tuple

(1, 2, 3)

No

Yes

Fixed records; function returns; hashable if elements are.*

range

range(10)

No

Yes

Memory-efficient integer sequences; iteration.

str

"hi"

No

Yes

Text; immutable character sequences.

Set Types

set

{1, 2, 3}

Yes

No

Unique items; membership testing; set operations.

frozenset

frozenset({1,2})

No

No

Immutable set; dict keys; set elements.

Mapping Types

dict

{"a": 1, "b": 2}

Yes

Yes**

Key-value lookups; counting; grouping; configuration.

Notes:

  • * Tuples are hashable only if all elements are hashable.

  • ** Dict ordering guaranteed in Python 3.7+.

2.3.4.2. Lists (Ordered Collections)#

Lists store multiple items in order and are mutable (can be changed):

numbers = [1, 2, 3, 4, 5]
fruits = ["Apple", "Banana", "Cherry"]
mixed = [1, "hello", 3.14, True]
empty_list = []

print(f"Numbers: {numbers}")
print(f"Fruits: {fruits}")
print(f"Mixed: {mixed}")
print(f"Empty list: {empty_list}")
Numbers: [1, 2, 3, 4, 5]
Fruits: ['Apple', 'Banana', 'Cherry']
Mixed: [1, 'hello', 3.14, True]
Empty list: []

Common list operations:

  1. Append: numbers.append(6)[1, 2, 3, 4, 5, 6]

  2. Access/Indexing: numbers[0]1

  3. Slicing: numbers[1:3][2, 3]

  4. Length: len(numbers)5

  5. Update: numbers[0] = 99[99, 2, 3, 4, 5, 6]

fruits = ["apple", "banana", "cherry"]        ### create a list by assignment
print(f"\nOriginal list: {fruits}")

fruits.append("date")                         ### mutable: update/change the list
print(f"After append: {fruits}")

print(f"First fruit: {fruits[0]}")            ### indexing
print(f"Number of fruits: {len(fruits)}")     ### length
Original list: ['apple', 'banana', 'cherry']
After append: ['apple', 'banana', 'cherry', 'date']
First fruit: apple
Number of fruits: 4
fruits[2] = "cranberry"                     ### lists are mutable: modify an element
print(f"After modification: {fruits}")
After modification: ['apple', 'banana', 'cranberry', 'date']

2.3.4.3. Tuples (Immutable Sequences)#

Tuples are similar to lists, but they cannot be changed after creation (immutable). So, use tuples when you want to ensure data won’t change:

rgb_color = (255, 128, 0) single = (42,) # Note the comma for single-item tuple

fruits = tuple(fruits)
print(f"\nConverted to tuple: {fruits}")            ### ( )
print("first element in coordinate: ", fruits[0])   ### indexing
Converted to tuple: ('apple', 'banana', 'cranberry', 'date')
first element in coordinate:  apple

Since tuples are immutable, the following operation would generate an error:

%%expect TypeError

fruits[2] = "cherry"  # This would cause an error because tuples are immutable!
TypeError: 'tuple' object does not support item assignment

2.3.4.4. Dictionaries (Key-Value Pairs)#

In Python, a mapping type is a collection that stores data as key–value pairs, where each key is unique and maps to a corresponding value. The most common mapping type is the dictionary (dict), which allows flexible data organization and fast lookup, insertion, and modification of values by key rather than numerical index. An example of a Python dictionary:

student = {
    "name": "Alice",
    "age": 20,
    "major": "IST"
}
student
{'name': 'Alice', 'age': 20, 'major': 'IST'}

Common dictionary operations:

  • Access: student["name"]"Alice"

  • Add/Update: student["gpa"] = 3.8

  • Keys: student.keys()dict_keys(['name', 'age', 'major'])

  • Values: student.values()dict_values(['Alice', 20, 'Computer Science'])

print(f"Student Name: {student['name']}")   ### 1. access value by key
student["major"] = "CS"                     ### 2. update: MUTABLE
student["GPA"] = 3.5                        ### 2. add new key-value pair: MUTABLE
print(f"student: {student}")

print(student.keys())                       ### 3. keys
print(student.values())                     ### 4. values
Student Name: Alice
student: {'name': 'Alice', 'age': 20, 'major': 'CS', 'GPA': 3.5}
dict_keys(['name', 'age', 'major', 'GPA'])
dict_values(['Alice', 20, 'CS', 3.5])

2.3.4.5. Sets#

A set is an unordered collection of unique elements. Sets are useful for removing duplicates and performing mathematical set operations.

Key characteristics:

  • Unordered: Elements have no defined order

  • Unique: Automatically removes duplicate values

  • Mutable: Can add/remove elements

  • No indexing: Cannot access elements by position

# Creating sets
unique_numbers = {1, 2, 3, 4, 5}
print(f"Set: {unique_numbers}")

# Duplicates are automatically removed
data = {5, 2, 8, 2, 5, 9}
print(f"Unique values: {data}")

# Creating from a list
measurements = [23.5, 24.1, 23.5, 25.0]
unique = set(measurements)
print(f"From list: {unique}")
Set: {1, 2, 3, 4, 5}
Unique values: {8, 9, 2, 5}
From list: {24.1, 25.0, 23.5}
# Basic set operations
set_a = {1, 2, 3}
set_b = {3, 4, 5}

print(f"Union: {set_a | set_b}")          # All elements
print(f"Intersection: {set_a & set_b}")   # Common elements
print(f"Difference: {set_a - set_b}")     # In A but not B
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference: {1, 2}