Ep. 2 Exercise: Data Types and Conversion

Today, we’re leveling up Basil’s skills by storing multiple items and working with different data types. We’re keeping it simple—storing more Spanish words and ingredient quantities, and making sure our assistant can handle different types of information like numbers and text.

Data Types in Simple Terms:

  • Strings for text, like "hola" or "onion".
  • Integers for whole numbers, like 5 tomatoes.
  • Floats for decimal numbers, like 1.5 kilos of onions.

Type Conversion:

  • When users give us input, we might need to change its type—turning a string like "5" into an integer so we can do math with it.

Quick Recap

In Episode 1, we created basic Python files and started storing a single Spanish word and ingredient. We used variables for storage, which is like keeping one piece of data in a single box.

Core Logic

Today, instead of storing one piece of data, we’re storing more of them. Imagine needing to track multiple Spanish words or multiple ingredients—it makes Basil a lot more useful!

Memory Aid

Think of data types as different types of storage—a notebook for your words, a bag for your five oranges, a bottle for your water. You’ve got to use the right storage for the right things.


Operations

  • Storing Multiple Words with Variables: We can use separate variables for each word we want to store, like:
  spanish_word1 = "hola"
  spanish_word2 = "adiós"
  spanish_word3 = "gracias"

This is how we can manage more than one piece of information without diving into more advanced concepts like lists just yet.

  • Storing Ingredient Quantities with Variables:
  ingredient_name1 = "tomato"
  ingredient_quantity1 = 5  # whole number, integer type

  ingredient_name2 = "onion"
  ingredient_quantity2 = 1.5  # decimal number, float type
  • Type Conversion Example:
  quantity_string = "3"
  quantity = int(quantity_string)  # Converts string "3" into integer 3

Real-World Application:
Imagine writing down your grocery list on paper. You’d have “tomato: 5” and “onion: 1.5 kg.” Here, “5” and “1.5” are numbers that help us quantify things, while the names are strings.


Built-In Functions and Methods

  • int(), float(), str(): These built-in functions let us change the type of data we’re working with.
  • Convert text to numbers when needed.
  • For example, turning "10" into 10 to use it as a quantity.

Guided Practice

  • Take the string "7" and convert it into an integer.
  • Store two new Spanish words using separate variables and quantify two ingredients.

What’s Needed for Basil

For our Basil assistant, we need to:

  1. Expand the ability to store more than one Spanish word or ingredient.
  2. Manage different data types (e.g., text and numbers) properly.

We won’t jump into lists yet, but we’ll work on storing multiple items by naming separate variables. This lays a solid foundation before we move on to more advanced ways of storing many items at once.


Basil Code Implementation

  1. Store More Spanish Words
  • In vocabulary.py, we’ll create multiple variables to store new words:
   spanish_word1 = "hola"
   spanish_word2 = "adiós"
   spanish_word3 = "gracias"
  1. Store Ingredient Names and Quantities
  • In ingredients.py, keep track of ingredients and their amounts separately:
   ingredient_name1 = "tomato"
   ingredient_quantity1 = 5  # in units

   ingredient_name2 = "onion"
   ingredient_quantity2 = 1.5  # in kilograms
  1. Convert User Inputs for Quantities
  • When taking input, convert strings to integers or floats:
   quantity_string = "4"
   quantity = int(quantity_string)  # Convert to integer for use


Episode 3: introduces lists in Python.

Scroll to Top