# Printing a very large integer + 1
print(123123123123123123123123123123123123123123123123321312321233 + 1)
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.
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.
# Octal, Hexadecimal, and Binary
print(0o10, 0x10, 0b10)
2. Floating-point Numbers
Floats follow IEEE 754 standards. The maximum value is approx \(1.8 \times 10^{308}\).
# 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)
# What happens if we exceed the max limit?
print("Maximum float: ", 1.79e308)
print("Too large (blow up): ", 1.8e308)
3. Strings
Text data. Can use single ' or double " quotes.
print('HKU')
print("Earth Sciences")
print('I am a string.')
4. Booleans
Represents logic: True (1) or False (0). They can be used in math equations!
print(True, type(True))
print(True + False) # 1 + 0
print(True * 2.0) # 1 * 2.0
5. Data Structure: Lists
Lists are ordered sequences that can hold mixed data types.
Creating and Accessing
# A mixed list: Strings, floats, booleans, and even another list!
mylist = ['a', 2.0, '400', True, 42, [24,2]]
print(mylist)
print(type(mylist))
Modifying Lists (Mutability)
Unlike strings, lists are mutable. You can change them in place.
mylist[1] = 26.3 # Change the 2nd element
del mylist[3] # Delete the 4th element (True)
print(mylist)
Key List Methods
# 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))
Generating Numbers with range()
# Start at 2, go up to (but not including) 20, step by 4
numlist = list(range(2, 20, 4))
print(numlist)
6. Critical Concept: The "Copy" Trap
When you say new_list = old_list, you represent binding a name to the object, not copying it.
# 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!
[:].
# 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
7. Data Structure: Tuples
Tuples are defined with parentheses (). They are almost identical to lists, but are Immutable (cannot be changed).
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
8. Data Structure: Sets
Sets use curly brackets {}. They are unordered and contain only unique elements.
# List with duplicates
raw_data = ['Earth', 'Mars', 'Earth', 'Jupiter', 'Mars']
# Convert to set to clean it
unique_planets = set(raw_data)
print(unique_planets)
Set Operations (Venn Diagram Logic)
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)