Welcome back to camp, @name! Darwin here with another lesson from our expedition.
Every good researcher must know how to classify their discoveries. In Python, data also has different "species" - we call them data types.
In the jungle we encounter different forms of life - animals, plants, insects. Similarly in Python we have different data types:
1# Whole numbers - like the number of discovered species
2species_count = 42
3expedition_day = 7
4team_members = 5
5
6# Can be negative
7temperature_drop = -5
8depth_below_sea = -100
9
10# Python 3 supports very large numbers!
11huge_number = 9999999999999999999991# Numbers with a decimal part
2temperature = 32.5
3humidity = 78.3
4distance_km = 15.7
5
6# Scientific notation
7speed_of_light = 3e8 # 3 * 10^8 = 300000000
8tiny_number = 1.5e-10 # 0.000000000151# Text in single or double quotes
2explorer_name = "Darwin"
3location = 'Amazonia'
4
5# Multi-line strings (triple quotes)
6description = """
7This is a longer description
8spanning multiple lines
9of text.
10"""
11
12# Escape characters
13message = "Darwin said: \"Let's explore!\""
14path = "C:\\Users\\Darwin\\jungle" # Windows path
15
16# Raw string (ignores escapes)
17path_raw = r"C:\Users\Darwin\jungle"1# Only two values: True or False
2is_dangerous = True
3found_water = False
4is_raining = True
5
6# The result of a comparison is a bool
7temperature = 35
8is_hot = temperature > 30 # True
9
10age = 15
11is_adult = age >= 18 # False1# The type() function shows the variable's type
2species_count = 42
3print(type(species_count)) # <class 'int'>
4
5temperature = 32.5
6print(type(temperature)) # <class 'float'>
7
8name = "Darwin"
9print(type(name)) # <class 'str'>
10
11is_safe = True
12print(type(is_safe)) # <class 'bool'>
13
14# Practical use
15data = 42
16if type(data) == int:
17 print("This is a whole number!")Sometimes we need to change the data type - like translating between languages:
1# String → Int
2age_text = "25"
3age = int(age_text)
4print(age + 5) # 30
5
6# String → Float
7temperature_text = "32.5"
8temperature = float(temperature_text)
9print(temperature + 2.5) # 35.0
10
11# WARNING! Error if the string is not a number
12# bad_conversion = int("abc") # ValueError!1# Int → String
2species_count = 42
3count_text = str(species_count)
4print("Discovered " + count_text + " species") # String concatenation
5
6# Float → String
7temperature = 32.5
8temp_text = str(temperature)
9message = f"Temperature: {temp_text}°C"1# Int → Float
2whole_number = 10
3decimal_number = float(whole_number) # 10.0
4
5# Float → Int (truncates the decimal part!)
6decimal = 15.7
7whole = int(decimal) # 15 (NOT 16!)
8
9decimal2 = 15.2
10whole2 = int(decimal2) # 151# Numbers → Bool
2# 0 = False, all others = True
3print(bool(0)) # False
4print(bool(1)) # True
5print(bool(-5)) # True
6print(bool(100)) # True
7
8# String → Bool
9# Empty string = False, non-empty = True
10print(bool("")) # False
11print(bool("Darwin")) # True
12print(bool(" ")) # True (a space is a character!)
13
14# Bool → Int
15print(int(True)) # 1
16print(int(False)) # 01# Basic operations
2a = 10
3b = 3
4
5print(a + b) # 13 (addition)
6print(a - b) # 7 (subtraction)
7print(a * b) # 30 (multiplication)
8print(a / b) # 3.333... (division - ALWAYS float!)
9print(a // b) # 3 (floor division)
10print(a % b) # 1 (remainder - modulo)
11print(a ** b) # 1000 (exponentiation: 10^3)
12
13# Operations on floats
14x = 5.5
15y = 2.5
16print(x + y) # 8.0
17print(x * y) # 13.75
18
19# Mixing int and float → result is float
20mixed = 10 + 5.5 # 15.5 (float)1# Concatenation (joining)
2first_name = "Charles"
3last_name = "Darwin"
4full_name = first_name + " " + last_name
5print(full_name) # Charles Darwin
6
7# Repetition
8jungle_cry = "Help! " * 3
9print(jungle_cry) # Help! Help! Help!
10
11# String length
12name = "Darwin"
13print(len(name)) # 6
14
15# Indexing (from 0!)
16text = "Python"
17print(text[0]) # P
18print(text[1]) # y
19print(text[-1]) # n (last character)
20
21# Slicing
22word = "Expedition"
23print(word[0:4]) # Expe
24print(word[4:]) # dition
25print(word[:4]) # Expe
26print(word[::2]) # Eeiio (every other character)`None` is a special value meaning "no value":
1# None - like an empty box
2treasure = None
3print(treasure) # None
4print(type(treasure)) # <class 'NoneType'>
5
6# Practical use
7def find_species(name):
8 if name == "Python regius":
9 return "Found!"
10 return None # Not found
11
12result = find_species("Unknown")
13if result is None:
14 print("Unknown species")1print("=== Expedition Calculator ===\n")
2
3# Collecting data
4distance_text = input("Distance (km): ")
5time_text = input("Time (hours): ")
6team_size_text = input("Team size: ")
7
8# Converting to appropriate types
9distance = float(distance_text)
10time = float(time_text)
11team_size = int(team_size_text)
12
13# Calculations
14avg_speed = distance / time
15distance_per_person = distance / team_size
16
17# Displaying results
18print("\n--- RESULTS ---")
19print(f"Average speed: {avg_speed:.2f} km/h")
20print(f"Distance per person: {distance_per_person:.2f} km")
21print(f"Type of distance: {type(distance)}")
22print(f"Type of team_size: {type(team_size)}")Create a program that:
In this lesson you learned:
Before moving on:
In the next lesson Darwin will show you operators - the explorer's mathematical and logical tools! 🔧🌴