Python Variables

By now, you should have installed Python on your computer. Follow these steps if you still need to do so.

A variable is like a labeled box that holds information. You give it a name, assign it a value, and the program, in this case, Python, holds onto that value for you while it runs your code.

Example:

name = "Alice"  # Assigning a string to the variable 'name'
age = 25        # Assigning an integer to the variable 'age'
height = 5.7    # Assigning a float to the variable 'height'

Here, name stores a string ("Alice"), age stores a number (25), and height stores a float (5.7).

Variable Naming Rules

You can name your variables almost anything, but there are a few rules to follow in Python. The variable:

  • Must start with a letter or an underscore _
  • Can include letters, numbers, and underscores
  • Is case-sensitive (Age and age are different variables!)
  • Has no spaces or special symbols like @, $, !

Changing Variables

Variables can change over time. Here, age was first set to 25. Then we changed it to 26. Now, age holds the new value.

age = 25  # Initial value
age = 26  # Now age equals 26 - Reassigned value

Quick Practice

Try running this code. You should see a simple sentence printed out.

city = "New York"  # Assigning a city name
temperature = 75  # Assigning a temperature value
print("I live in", city, "where the temperature is", temperature, "degrees.")

Try changing the values and running it again.

In the next post, we’ll introduce the fictional dataset we’ll be working with throughout the series. I’ve found that I learn best when working with meaningful data, so I created a sample dataset that feels similar to something you might actually come across in health or demographic analyses.

We will check the dataset out in the next post. See you there.

Recommended Python Books

Plan your day with Sunsama! Get started with a 30-day trial.

It pays to have a financial ally. $100 to be exact. Open a new Ally Bank account to earn money for you and your family.

Recent Posts