From the course: Introduction to Career Skills in Software Development

Saving data with Python - Python Tutorial

From the course: Introduction to Career Skills in Software Development

Saving data with Python

- [Instructor] We declare variables in Python by giving them a name and then setting their value. This assignment is done with the equal sign. Let's create some variables so you can see how it's done. The first variable we'll create will be a number representing a score. We'll write the word score, then a space, an equal sign, another space, and then the number 750. What did we just do? Score is the name of our variable. And because of the equal sign, we are assigning it the value of 750. This means that somewhere in the computer's memory, it has made space for our variable and stored our value. Let's see what happens when we ask the computer to print out what's in the score variable. To do that, we'll use our familiar print command, and we'll provide score as its input inside the parentheses. So we'll come underneath score, we'll type print, open parenthesis, and then score. Let's click Run. And we get 750 as the output. Perfect. Let's leave a note for ourselves to know what this variable is used for. To do that, we're going to add a comment. We will go above where we've defined the score variable. I'm going to put a pound sign and then the words The user's score. This is how you create a comment in Python. Comments are ignored by the computer, and they don't appear in the output. Let's rerun the code so you can see that. Still the same output, 750. Comments are helpful to remind you and other programmers what your code does. Let's create another variable. This variable is going to hold the value of my favorite snack. I'll put the pound sign, and then I'll put My favorite snack. Underneath, I'll create a variable. We'll start by giving the variable a name. It will be favorite_snack. Now notice that I use an underscore here instead of a space. That's because the rules of the Python language dictate that variable names can't contain spaces. To get around that, it's common to use an underscore instead. Next I'll add a space, an equal sign, another space, and then the string Cookies. Let's print this out. We'll use the print command again. So on this new line, I'm going to type print, open parenthesis, and then favorite_snack. Let's run our program. And we get 750, followed by Cookies right underneath. Are you starting to get the hang of creating variables in Python? I hope so. Why not try to create a few more variables of your own and print them out? Practice makes perfect.

Contents