🎉 Welcome to PyVerse! Start Learning Today

PYTHONDATA SCIENCE

Understanding Data — Types, Values, and Formats

PyVerse.io Data Science Foundations

PyVerse Lesson: Understanding Data — Types, Values, and Formats

Introduction

  • Every app, game, and website uses data. Your age, a username, a high score, even a weather report — all are data.
  • In Python, we organize data using types so the computer knows how to store it and what we can do with it.
  • In this lesson, we’ll explore:
    • What data is
    • Common Python data types
    • How to convert between types
    • How data is stored in formats like CSV and JSON

Step-by-Step Explanation

1) What is data?

  • Data is information. Examples:
    • 15 (a number)
    • "Hello" (text)
    • True (a yes/no value)
    • ["apple", "banana", "pear"] (a list of things)
  • Python needs to know the type of data so it can handle it correctly.

2) Types vs. Values

  • Value: the actual piece of data (like 15 or "Hello").
  • Type: what kind of value it is (number, text, etc.).
  • The same value written differently can be a different type:
    • "15" is text (string)
    • 15 is a number (integer)

3) Meet the main Python data types

  • int (integer): whole numbers like 0, 5, -12
  • float: numbers with decimals like 3.14, 0.5
  • str (string): text inside quotes like "Python", "123"
  • bool (boolean): True or False
  • NoneType: the special value None meaning “nothing yet”
  • list: an ordered collection like [1, 2, 3] or ["red", "blue"]
  • dict (dictionary): labeled data using key-value pairs like {"name": "Ari", "age": 14}

4) Check the type of a value

Use the type() function to ask Python what type a value or variable is.

Code Example 1: Seeing Types and Values

# Understanding types and values age = 14 # int: a whole number height_m = 1.63 # float: decimal number (meters) name = "Ari" # str: text is_student = True # bool: True or False nothing_yet = None # NoneType: means "no value yet" print("age:", age, "=>", type(age)) print("height_m:", height_m, "=>", type(height_m)) print("name:", name, "=>", type(name)) print("is_student:", is_student, "=>", type(is_student)) print("nothing_yet:", nothing_yet, "=>", type(nothing_yet)) # Lists hold many values (in order) colors = ["red", "green", "blue"] # list print("colors:", colors, "=>", type(colors)) # Dictionaries hold labeled values (like a mini form) student = {"name": "Ari", "age": 14, "likes_python": True} # dict print("student:", student, "=>", type(student))

5) Changing between types (type conversion)

  • Sometimes data comes in as text, but you need a number to do math.
  • Use:
    • int("42") -> 42
    • float("7.5") -> 7.5
    • str(42) -> "42"
    • bool(value) -> True/False (empty things are usually False)

Code Example 2: Type Conversion (Casting)

# Converting between types text_number = "42" # this is text, not a number print(int(text_number) + 8) # 50, now it's a number so we can add price_text = "7.50" price = float(price_text) # convert to a decimal number print("Rounded price:", int(price)) # int() drops the decimal (7) # Converting numbers and booleans to strings for printing or messages age = 14 message = "I am " + str(age) + " years old." print(message) # bool() rules of thumb: print(bool(0)) # False (zero is "empty") print(bool(3)) # True (non-zero number) print(bool("")) # False (empty string) print(bool("hi")) # True (non-empty string)

6) Collections: lists and dictionaries

  • Use a list when order matters: [88, 95, 72]
  • Use a dictionary when you want labels: {"math": 88, "science": 95}
  • You can combine them: a list of dictionaries (like a tiny table of rows)

7) Data formats: how data is stored and shared

  • Plain text (.txt): simple text
  • CSV (.csv): Comma-Separated Values; like a spreadsheet in text form
    name,score Luna,88 Kai,95
  • JSON (.json): JavaScript Object Notation; looks like Python dictionaries and lists
    {"city": "Nairobi", "temp_c": 27, "raining": false}

Code Example 3: Reading CSV and JSON (No Files Needed)

# CSV: read table-like data from a text string import csv import io csv_text = """name,score Luna,88 Kai,95 Sam,72 """ # StringIO lets us treat a string like a file f = io.StringIO(csv_text) reader = csv.DictReader(f) # each row becomes a dict using the headers for row in reader: name = row["name"] # string score = int(row["score"]) # convert to int for math if score >= 90: print(name, "is a high scorer with", score) # JSON: read structured data from a text string import json json_text = '{"city": "Nairobi", "temp_c": 27, "raining": false}' data = json.loads(json_text) # parse JSON string to a Python dict print("City:", data["city"]) print("Temperature (C):", data["temp_c"]) print("Raining?:", data["raining"])

Practical Exercise: Student Scores

Goal: Practice types, conversion, and reading CSV.

Your task:

  • You are given CSV text with students, ages, and scores.
  • Parse it, convert numbers, and print some simple results.

Starter code:

import csv import io csv_text = """name,age,score Ava,14,91 Ben,13,84 Mira,15,96 Omar,14,78 Zoe,13,88 """ # 1) Turn the csv_text into rows you can loop over f = io.StringIO(csv_text) reader = csv.DictReader(f) # 2) For each row: # - Get the name (string) # - Convert age to int # - Convert score to int # - If score >= 90, print: " did great with !" # 3) Challenge (optional): # - Calculate the average score and print it at the end Hints: # - Use int(row["age"]) and int(row["score"]) # - Keep a running total of scores and a count of students for the average # - Average = total / count (make sure total and count are numbers)

Recap

  • Data is information. Python uses types (int, float, str, bool, None, list, dict) to understand how to store and use that information.
  • Values are the actual pieces of data; types describe what they are.
  • You can convert between types using int(), float(), str(), and bool().
  • Data formats like CSV and JSON help us store and share data. Python can read them easily.
  • Practice by parsing small CSV or JSON snippets and converting text to numbers when needed.
  • You’re now ready to work with real-world data in Python—one type at a time!

Loading quizzes...