Welcome to the first step of bringing Basil, our Python assistant, to life! Today, we’re covering variables and basic Python syntax—these are the fundamental building blocks of any program.
Variables in Simple Terms:
- Variables are like labeled containers that hold information. You give each container a name, so you know what’s inside it.
- For example, if we have
ingredient = "tomato", the word"tomato"is stored in a container calledingredient.
Basic Syntax:
- Python code is pretty readable. We use
=to assign a value to a variable, and we can useprint()to show it on the screen.
Core Logic
This is our first step in turning Basil from an idea into a useful tool. By learning how to create and use variables, we can store things like Spanish words or ingredients, which is a crucial part of how Basil will work.
Memory Aid
Think of variables like sticky notes with labels. If you write “favourite_spice” on a sticky note and put it on a a jar with cinnamon, you’ll always know where to find it. If you want to change your favourite spice to nutmeg, just exchange the spices in the jar.
Operations
- Create a Variable to Store an Ingredient:
ingredient = "tomato"- Print the Ingredient:
print(ingredient) # This will output: tomatoReal-World Application:
Imagine labeling different drawers in your kitchen—one drawer for utensils, another for spices. Each label is like a variable name, and it helps you find what you need.
Built-In Functions and Methods
print(): This is a built-in function that lets us show output. It’s like asking Python to say something out loud.- the text behind the # sign is a comment. it’s ignored by Python. It’s simply there for us to remember what our code does
print("Welcome to Casa Python!") # Displays the message to the screenGuided Practice
Create a variable named spanish_word and store the word "hola". Then use print() to display it on the screen.
What’s Needed for Basil
In this episode, we’ll:
- Set up the basic files that will hold our code:
main.py,vocabulary.py, andingredients.py. - Start storing a single Spanish word and a single ingredient. Later, we’ll expand to storing more!
Basil Code Implementation
- Create Basic Files:
- Open your project folder and create three Python files:
main.py,vocabulary.py, andingredients.py.
- Store a Spanish Word:
- In
vocabulary.py, add this code:
spanish_word = "hola"
- This stores
"hola"in a container calledspanish_word.
- Store an Ingredient:
- In
ingredients.py, create a similar variable:
ingredient = "tomato"
- Print the Values:
- Let’s add a
print()inmain.pyto check what we have:
from vocabulary import spanish_word
from ingredients import ingredient
print(f"Spanish word: {spanish_word}")
print(f"Ingredient: {ingredient}")
- The
finf"..."is called an f-string. It helps us include the value of variables right inside a string. This will print:Spanish word: hola Ingredient: tomato
Optional exercises:
- Try adding another variable in
vocabulary.pyfor a different Spanish word. - In
main.py, modify the code to print out this new word.