Python Variables and Data Types

PythonPythonBeginner
Practice Now

Introduction

Welcome to the wild and untamed West! In this lab, you will take on the role of a courageous frontier explorer venturing into the uncharted territories of Python programming. Your mission, should you choose to accept it, is to master the use of variables and data types to track your supplies and navigate through Python's dynamic landscape.

The wind howls and your trusty computer is your compass. Along your journey, you will learn to declare variables, understand the nuanced differences between data types, and use them effectively to make crucial decisions.

Prepare your horse, pack your gear, and get ready to blaze a trail through the wilderness of code!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") subgraph Lab Skills python/variables_data_types -.-> lab-271605{{"`Python Variables and Data Types`"}} end

Setting up Your Camp

In this step, you will establish a base camp by setting up your first Python variables. Think of these as containers to store your supplies. We need to be organized before we embark on our adventure!

In /home/labex/project/base_camp.py, begin your Python trail by declaring three variables: water, food, and ammunition. We'll start by simply storing the quantities of each.

## base_camp.py

water = 100      ## Gallons of water
food = 50        ## Pounds of food
ammunition = 40  ## Rounds of ammunition

Execute your script to ensure it runs without errors:

python3 /home/labex/project/base_camp.py

You won't see any output yet, but that's okay. You're setting the stage for survival!

Understanding Data Types

Your supplies are stored, but not everything in the wilderness is so simple. Python has several data types, each with a purpose. In this step, you will explore the common data types: integers, floats, strings, and booleans.

Enhance your /home/labex/project/base_camp.py script by now assigning appropriate data types to your variables. Let's specify exactly what type of values we're dealing with for clarity.

## base_camp.py

water = int(100)            ## Gallons of water
food = float(50.5)          ## Pounds of food, and we're carrying halves
ammunition = 40             ## Rounds of ammunition are always whole numbers
camp_name = "Fort Python"   ## The name of your camp
enemy_visible = False       ## There's no enemy in sight right now

Now, let the script tell us what we have by printing each variable and its type:

print('Water:', water, type(water))
print('Food:', food, type(food))
print('Ammunition:', ammunition, type(ammunition))
print('Camp Name:', camp_name, type(camp_name))
print('Is the enemy visible?', enemy_visible, type(enemy_visible))

Run your enhanced script again:

python3 /home/labex/project/base_camp.py

Check for your supplies and their types in the output. Your terminal should show:

Water: 100 <class 'int'>
Food: 50.5 <class 'float'>
Ammunition: 40 <class 'int'>
Camp Name: Fort Python <class 'str'>
Is the enemy visible? False <class 'bool'>

Summary

In this lab, we began our adventure by setting up our base camp with Python variables.

Remember, the trail you blaze through Python is as adventurous as you make it. Happy coding, explorer!

Other Python Tutorials you may like