Learn Python For Beginners

Python Variables

Learning how python variables works will be the first step in this series of learning python for beginners that will really get your brain wiggling. This is where you can start building really small programs on your own if you can put your mind to it. The sauce, if you will. There is still a lot to learn and I wouldn’t recommend trying to launch a serious application until we get to object oriented programming, but definitely get creative.

What Are Variables?

Python variables are a way to store data in memory. In previous articles in this series, we have just been printing data to the terminal using a print statement that stored our data inside of it. This isn’t a really robust way to do things in terms of actual software. Instead it would be better if we could store that data in memory so it could be reusable and its value can only change when we want it to. Let’s go back to our PyCharm IDE and write something like this:

Python Variables

Now we have taken our data and stored them in variables that we can re-use in many ways. We can see that the x variable is assigned a value of 13. The y variable stores the result of a mathematical operation using python operators, and we have a variable to store a string. If we run the program, we get the following:

Using the knowledge of our previous lessons we can see that the output is what we expect. Notice the order of operations does not apply here. This is because the variable that holds the result of 12 / 2 runs the operation before it’s used in the print statement.

Modifying Python Variables

Another great use case for variables is the fact you can change their value. Let’s add some more logic into our program.

Python Variables

There are a few things to take away from this code. Notice that the string variable is grayed out and has a hash symbol in front of it. This is a comment in python. When a line is commented out, it won’t be run through the compiler. More importantly, we can see that we have defined the x and y variables twice. Try to predict what will happen if you run the program and then click run.

We can see that we get 15 which is the result of the values we assigned to the variables the second time. Variables can only store one value at a time. Python code is interpreted from top to bottom so operations and assignment of variables goes in order.

Python Variable Naming Convention

You can name variables almost whatever you want. Naming your variables in a readable way is important when writing large software. There are a few rules and guidelines to use when naming variables in python.

  • A variable must start with a letter or underscore character
  • A variable can’t start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variables are case-sensitive

Above are all rules that must be followed when naming variables in python. The following are all just guidelines for naming variables in python. Take a look at this photo:

Python Variables

In the above picture we can see that we have three different stored values. Take note that in the third name between every word is an underscore character. The first two variables have similar names but remember the case-sensitivity rule. Let’s click run and see what happens.

As you can see, even though the string variables share the same characters they hold different values. Don’t worry about the value for the third variable for now, it’s just there to illustrate the correct way to name variables with multiple words. Instead of hard-coding in our values we can do something even cooler and take user input in python and store it.

    Leave a Comment

    Your email address will not be published. Required fields are marked *