Python Basics Tutorial 2 – Python Variables

(Interested in learning more about Python? Learn Python in 3 weeks, under 10 minutes each day today by clicking here.)

Notes from the video Python Course Singapore | Learn Python Programming |Python Tutorial 2 – Python Variables’:

Now, before we start explaining what variables are, let me first share a bit about myself.

  • My age is 23 years old
  • My interest is programming.

Now, take a moment, to memorise this.

Now, if I ask you what my age and what my interest is, you would probably say it’s 23 years old and programming respectively.

In this example, the terms my age and my interest work as placeholder for 23 years old and programming respectively.

This is what variables are in Python. They are just placeholder for data.

 

Now, let’s try to write my interest as a variable in Python.

My_interest = “programming”

Let’s look at this line of code.

The name of the variable myInterest is keyed in first followed by the equal sign and the data it refers to.

In this case, the variable myInterest refers to the string ‘programming’.

So, now when Python encounters the variable myInterest, it knows that I am referring to the string ‘programming’.

In the previous video, we learnt about the print function.

If we were to print the variable myInterest, python would display programming.

Notice that the variable name myInterest has no double or single quotes around it unlike the string ‘programming’. That’s because only strings are enclosed in quotes in python.

You might have also noticed that each statement is on a separate line.

 

Variable names have no inherent meaning in Python and can be called just about whatever you want, for example, we could change the variable name to Interest or ABC or just about anything we want.

 

Restrictions on naming variables
You can’t start with a number though you can include numbers in the subsequent characters after the first character or have spaces between words in the variable name.

You also shouldn’t have two variables with the same name or variables with the same names as functions or else you would face problems later on.
We would cover more about naming variables in a later video.

 

Variables can also be used to refer to a number.

For example, we can write my age as a variable in Python.

my_age = 23

In this code, the variable my_age refers to the number 23.

So, now when Python encounters the variable my_age, it knows that I am referring to the number 23. You may have also noticed that unlike strings, numbers are not enclosed in single or double quotes.
You might be wondering how Python knows that the number 23 is not a variable in this case. Remember earlier we briefly mentioned that variable names cannot start with numbers. Since it’s a number, Python rejects 23 as a variable and recognises it as a number.

If you tried to print the variable my_age, python would display the number 23.

Get FREE LESSONS and TIPS on how YOU can excel at programming and business

Awesome! Thank you for subscribing!