Variables & Data Structures

Welcome to Lecture 2. In this session, we dive deep into how Python stores data. Unlike other languages (like C or Fortran), Python is dynamically typed, making it very flexible.

1. Integers

In Python 3, integers have effectively no limit. They can be as long as your memory allows.

In [1]
# Printing a very large integer + 1
print(123123123123123123123123123123123123123123123123321312321233 + 1)
123123123123123123123123123123123123123123123123321312321234

Python also supports non-decimal numbers. Below are examples of Octal (base 8), Hexadecimal (base 16), and Binary (base 2) representations of the number 10.

In [3]
# Octal, Hexadecimal, and Binary
print(0o10, 0x10, 0b10)
8 16 2

2. Floating-point Numbers

Floats follow IEEE 754 standards. The maximum value is approx \(1.8 \times 10^{308}\).

In [5]
# Scientific Notation examples
print(4.7e2)    # 4.7 * 10^2
print(3.9e0)    # 3.9 * 10^0
print(1.6e-19)  # 1.6 * 10^-19 (Very small number)
470.0 3.9 1.6e-19
In [6]
# What happens if we exceed the max limit?
print("Maximum float: ", 1.79e308)
print("Too large (blow up): ", 1.8e308)
Maximum float: 1.79e+308 Too large (blow up): inf

3. Strings

Text data. Can use single ' or double " quotes.

In [7]
print('HKU')
print("Earth Sciences")
print('I am a string.')
HKU Earth Sciences I am a string.

4. Booleans

Represents logic: True (1) or False (0). They can be used in math equations!

In [8]
print(True, type(True))
print(True + False) # 1 + 0
print(True * 2.0)   # 1 * 2.0
True 1 2.0

5. Data Structure: Lists

Lists are ordered sequences that can hold mixed data types.

Creating and Accessing

In [14]
# A mixed list: Strings, floats, booleans, and even another list!
mylist = ['a', 2.0, '400', True, 42, [24,2]]

print(mylist)
print(type(mylist))
['a', 2.0, '400', True, 42, [24, 2]]

Modifying Lists (Mutability)

Unlike strings, lists are mutable. You can change them in place.

In [17]
mylist[1] = 26.3      # Change the 2nd element
del mylist[3]         # Delete the 4th element (True)
print(mylist)
['a', 26.3, '400', 42, [24, 2]]

Key List Methods

In [21]
# Counting how many 'False' values exist (There are none currently)
print(mylist.count(False)) 

# Find the index position of the number 42
print(mylist.index(42))
0 3

Generating Numbers with range()

In [22]
# Start at 2, go up to (but not including) 20, step by 4
numlist = list(range(2, 20, 4))
print(numlist)
[2, 6, 10, 14, 18]

6. Critical Concept: The "Copy" Trap

⚠️ WARNING: This is the most common bug for beginners!

When you say new_list = old_list, you represent binding a name to the object, not copying it.

In [18]
# THE WRONG WAY
original = [1, 2, 3]
copy_cat = original      # This creates a reference, not a copy!

copy_cat[0] = 999        # Changing copy_cat...
print(original)          # ...ALSO changes original!
[999, 2, 3]
✅ THE RIGHT WAY: To create an independent copy, use slicing [:].
In [19]
# THE RIGHT WAY
original = [1, 2, 3]
real_copy = original[:]  # Slicing creates a new object

real_copy[0] = 999
print("Copy:", real_copy)
print("Orig:", original) # Original stays safe
Copy: [999, 2, 3] Orig: [1, 2, 3]

7. Data Structure: Tuples

Tuples are defined with parentheses (). They are almost identical to lists, but are Immutable (cannot be changed).

In [23]
mytuple = (1, '28', 0.033, False, [-2, 3])
print(mytuple)

# You can slice them like lists
print(mytuple[0:2])

# But you CANNOT change them. Uncommenting the line below causes an error:
# mytuple[2] = 5  
(1, '28', 0.033, False, [-2, 3]) (1, '28')

8. Data Structure: Sets

Sets use curly brackets {}. They are unordered and contain only unique elements.

In [24]
# List with duplicates
raw_data = ['Earth', 'Mars', 'Earth', 'Jupiter', 'Mars']

# Convert to set to clean it
unique_planets = set(raw_data)
print(unique_planets)
{'Mars', 'Jupiter', 'Earth'}

Set Operations (Venn Diagram Logic)

In [25]
set_A = {'Mars', 3.9, 'Saturn'}
set_B = {'Mars', 3.9, 'Saturn', 'Uranus'}

# Difference: What is in B but NOT in A?
diff = set_B.difference(set_A)
print("Difference:", diff)

# Intersection: What is in BOTH?
inter = set_B.intersection(set_A)
print("Intersection:", inter)
Difference: {'Uranus'} Intersection: {'Mars', 3.9, 'Saturn'}