From the course: Introduction to Career Skills in Software Development

Exploring variables - Python Tutorial

From the course: Introduction to Career Skills in Software Development

Exploring variables

- In algebra class, you were introduced to variables. Remember seeing formulas like this where both x and y represented variables? A variable was a placeholder for an unknown value. Similarly, in programming, we use variables as a container for a value. When we run our programs, the computer gives us space in its memory where we can put data that we want to use as a reference for later. This data is called a variable. Variables are an essential part of programming, because we need a way to store information. Think about your favorite online game. A programmer will need to keep track of dozens of variables. For example, I like to play word games online. Besides the score for each player, there's the total number of tiles left to play, the value of each tile, bonus points, and more. Each of these values are variables stored in the computer's memory. To visualize this, imagine each variable is a jar on a shelf. One jar holds your score, another, the total number of tiles, and yet another, the number of tiles left. As the game proceeds, the values of the variables may change. For example, if you score, the value of their score variable changes, and the previous score is gone from memory. Also, when you make a new word, the number of tiles left will decrease. But then, when it comes to the total number of tiles, that's a variable that doesn't change during gameplay. These are special types of variables that we call constants, because their values don't change. Variables are vital in any programming language as they allow you to write flexible programs. Can you imagine playing your favorite game and the moves are always the same, the score, always the same, you would lost interest quickly. Rather than assuming how the user will interact with your program, variables allow you to leave it up to them, a more engaging experience all around.

Contents