8. Variables and Data Types#

8.1. Introduction#

8.2. Learning Objectives#

8.3. Variables in Python#

num_points = 120
print(num_points)
num_points

8.3.1. Variable Assignment#

# Start with a number
location_data = 42.3601

# Change to text
location_data = "Boston"

# Change to a list of coordinates
location_data = [42.3601, -71.0589]

print(location_data)

8.4. Naming Variables#

8.4.1. Good Naming Examples for Geospatial Data#

# Good variable names for geospatial data
latitude = 42.3601
longitude = -71.0589
elevation = 147.2
city_name = "Boston"
population = 685094
coordinate_system = "WGS 84"

8.4.2. Poor Naming Examples to Avoid#

# Poor variable names - avoid these
x = 42.3601  # Too generic
data = "Boston"  # Too vague
temp = 25.6  # Ambiguous - temperature or temporary?
l = [42.36, -71.06]  # Single letter variables are hard to understand

8.5. Data Types#

num_features = 500  # Represents the number of features in a geospatial dataset
latitude = 35.6895  # Represents the latitude of a point on Earth's surface
longitude = 139.6917  # Represents the longitude of a point on Earth's surface
coordinate_system = "WGS 84"  # Represents a commonly used coordinate system
is_georeferenced = True  # Represents whether a dataset is georeferenced or not
coordinates = [
    35.6895,
    139.6917,
]  # A list representing latitude and longitude of a point
feature_attributes = {
    "name": "Mount Fuji",
    "height_meters": 3776,
    "type": "Stratovolcano",
    "location": [35.3606, 138.7274],
}

8.6. Escape Characters#

print("Hello World!\nThis is a Python script.")
print("This is the first line.\n\tThis is the second line. It is indented.")
print("What's your name?")
print("What's your name?")

8.7. Comments in Python#

# This is a comment
num_points = 120  # This is an inline comment

8.8. Working with Variables and Data Types#

num_features += 20
print("Updated number of features:", num_features)
import math

latitude = 35.6895
latitude_radians = math.radians(latitude)
print("Latitude in radians:", latitude_radians)
coordinates = [35.6895, 139.6917]
coordinates.append(34.0522)  # Adding latitude of Los Angeles
coordinates.append(-118.2437)  # Adding longitude of Los Angeles
print("Updated coordinates:", coordinates)
mount_fuji_name = feature_attributes["name"]
mount_fuji_height = feature_attributes["height_meters"]
print(f"{mount_fuji_name} is {mount_fuji_height} meters high.")

8.9. Basic String Operations#

8.9.1. Changing Case#

city_name = "San Francisco"

# Convert to lowercase
city_lowercase = city_name.lower()
print("Lowercase:", city_lowercase)

# Convert to uppercase
city_uppercase = city_name.upper()
print("Uppercase:", city_uppercase)

# Convert to title case (first letter of each word capitalized)
city_title = city_name.title()
print("Title case:", city_title)

8.9.2. Replacing Text#

original_city = "San Francisco"
new_city = original_city.replace("San", "Los")
print("Original:", original_city)
print("Modified:", new_city)

8.9.3. Other Useful String Methods#

location_data = "  Mount Everest  "

# Remove whitespace from beginning and end
clean_location = location_data.strip()
print("Cleaned:", clean_location)

8.10. Key Takeaways#

8.11. Exercises#

8.11.1. Exercise 1: Variable Assignment and Basic Operations#

8.11.2. Exercise 2: Working with Strings#