Welcome to Day 2 of my Fluffy Python learning journey! Today, we're diving into variables – a fundamental concept in programming that allows you to store and manage data within your programs. Think of them as labeled containers or named storage locations in your computer's memory.
Variables hold values that can change throughout your program's execution. They allow you to:
- Store Information: Like names, ages, prices, or true/false states.
- Reuse Data: Use the same piece of information multiple times without retyping it.
- Make Code Flexible: Change values easily without modifying the core logic.
Python automatically determines the type of data you store in a variable, but it's good to know the common ones:
- String (
str): Used for text. Enclosed in single or double quotes (e.g.,"hello",'world'). - Integer (
int): Whole numbers (e.g.,10,-50,1000). - Float (
float): Numbers with decimal points (e.g.,3.14,2.5,-0.01). - Boolean (
bool): Represents truth values:TrueorFalse. (Note the capital T and F).
Here's the code we're exploring today. You'll find this in the variables_intro.py file within this directory.
# Day 2: Variables - Storing Information
# Variables are like labeled boxes where you can store different types of data.
# You give the box a name, and then you put something inside it.
# 1. Storing a String (text)
user_name = "Woliul"
greeting = 'Hello, Python Learner!'
print("User Name:", user_name)
print("Greeting:", greeting)
# 2. Storing an Integer (whole number)
age = 30
number_of_lessons = 2
print("Age:", age)
print("Lessons completed:", number_of_lessons)
# 3. Storing a Float (decimal number)
price = 19.99
pi_value = 3.14159
print("Product Price:", price)
print("Value of Pi:", pi_value)
# 4. Storing a Boolean (True/False value)
is_learning_python = True
has_finished_course = False
print("Is learning Python?", is_learning_python)
print("Has finished course?", has_finished_course)
# You can also change the value of a variable
current_mood = "Happy"
print("Current mood (before change):", current_mood)
current_mood = "Excited about Python!"
print("Current mood (after change):", current_mood)
# Variables can be used in calculations
total_cost = price * number_of_lessons
print("Total cost of lessons:", total_cost)Ready to get hands-on with variables? Try these:
- Your Profile: Create variables to store your city, your favorite hobby, and whether you own a pet (True/False). Then, print them out with descriptive labels.
- Simple Inventory: Imagine you're tracking items. Create variables for
item_name(string),item_quantity(integer), anditem_price_per_unit(float). Calculate and print thetotal_item_value. - Dynamic Greeting: Ask the user for their name (using
input(), which we'll cover more tomorrow, but you can try it!) and store it in a variable. Then, print a personalized greeting like "Hello, [Name]!". - Update a Variable: Create a variable called
scorewith an initial value. Then, change its value a couple of times (e.g.,score = score + 10) and print its value after each change.
- Naming Conventions:
- Descriptive Names: Use clear, descriptive names for your variables (e.g.,
user_nameinstead ofun). This makes your code much easier to read and understand. - Lowercase with Underscores: The common Python convention for variable names is
snake_case(all lowercase, words separated by underscores). - Avoid Keywords: Don't use Python's reserved keywords (like
print,if,for,True,False) as variable names.
- Descriptive Names: Use clear, descriptive names for your variables (e.g.,
- Dynamic Typing: Python is "dynamically typed," meaning you don't need to explicitly declare a variable's type (like
int user_age;). Python figures it out based on the value you assign. This makes coding faster but requires you to be aware of the data type when performing operations (e.g., you can't add a string and a number directly). - Type Checking (Advanced): For more complex projects, Python offers
type hints(e.g.,user_name: str = "Alice") to help tools check for potential type errors, though this doesn't change runtime behavior.
If you've followed the Setup guide, you can run this code:
- Open your terminal or command prompt.
- Navigate to the
Day_02_Variablesdirectory.cd path/to/your/fluffy-python/Day_02_Variables - Run the script using:
python variables_intro.py # Or if you installed python3: # python3 variables_intro.py
Tomorrow, we'll dive deeper into Data Types and learn how to check the type of a variable and why it matters!