3. Variables in Python

The most fundamental concept in programming is the concept of a variable.A variable is like a box in which you can store things like numbers and strings, for later use. We use variables to temporarily store data in a computer’s memory.

Here’s an example:

age = 24

when Python interpreter executes this code, it will allocate some memory, then it will store the number 24 in that memory, and finally it will attach this age label in that memory location. In other words, a variable is a name that refers to a value during program execution.

So, if I use the print function to print the value of the age variable, it will print 24, because age refers to 24.

age = 24
print(age)

So, the output of this program is 24.

The equals symbol (=) is called the assignment operator. The assignment operator means “Put this value in this variable”.

Note that the variable name is an arbitrary name that you select. A variable can have a short name (like a and b) or a more descriptive name (age, price, account_id). But there are rules for naming variables.

  • Variable names are case-sensitive
  • Variable names should not start with a number
  • Python Keywords are not allowed as variable names

Rule number 1: Python is case-sensitive

Rule number 1 says that “Variable names are case-sensitive”. Python is sensitive to capitalization of letters. For example, “Age” and “age” are two different variables because the “A” is uppercase in the first variable and lowercase in the second variable:

Age = 50
age = 24
print (Age)

So, if we run this code, the print function will print 50.

Rule number 2: variable names should not start with a number

OK, rule number two, variable names should not start with a number. For example, if you declare a variable like this:

1age = 24

And run the program, you will get an error message says:

Rule number 3: Python Keywords are not allowed

Another rule is that Python Keywords are not allowed as variable names. Keywords are a series of reserved words in Python. They have special meaning for Python.

Here is a complete list of python keywords:

anddelfromnotwhile
aselifglobalorwith
assertelseifpassyield
breakexceptimportprintNone
classexecinraisecontinue
finallyisreturndeffor
lambdatry

Updating a variable

And one last thing is about updating variables. You can update a variable after initializing it like this. For example, I can reassign the value of the age variable like this:

age = 24
age = 30
print(age)

If I run the code, the result will be 30.

2. Print() function

Let’s write our first Python program. Type print, all in lowercase, open and close parenthesis, and inside these parentheses, add double quotes or single quotes and then type whatever you want to print. In programming terms, we usually call text a string:

print("I am Beh from Dev Hive")

So, this is your first Python program. Now to run this program, just click on the green icon that is located on the upper right side.

As you see, my name and my Udemy account name, is printed here:

Now you know that we can enclose a text (or a string) in either single quotes (‘) or double quotes (“), but what’s the difference?

Note that we should use double quotes when we have single quote inside a string:

print("I'm Beh from Dev Hive")

And we should use single quotes, when we have double quote inside a string:

print('I am Beh from "Dev Hive"')

We can also use triple quotes like this:

print('''I'm Beh from "Dev Hive"''')

In this way, you can use double quote and single quote in your string at the same time.

Print a multi-line string

To print a multi-line string, we use triple quotes like this:

print('''
I'm Beh 
from "Dev Hive"
''')

The output of this program is this:

1. Best tools to run and test Python codes

You can use Visual Studio Code to run and test Python code. I will teach you further how to install and configure this tool, but before that, let me introduce you an online Python interpreter:

http://try-python.appspot.com/

In this online tool, all you need is to type your Python code and press the Enter key, so it will execute your code.

For example, in Python, there is a function named print() that prints your text. So, open this online tool and write this code:

print("Hello World!")

And then press the Enter key. It will print Hello World! in the next line.

Installing VS-Code

You can also use Visual Studio Code to run Python code. VS-Code is a free code editor that is available on your favorite platform – Windows, mac-OS, and Linux. 

Let me show you how to install Python and VS-Code on Windows. For installing Python, open python.org website. In the opened page and from downloads tab, click on [Python for Windows].

After download completed, run and install the downloaded file. To do so, click on Install Now button. After a few seconds, it will complete the Python installation.

Now we are ready to install Visual Studio Code. So, open up this https://code.visualstudio.com link.

In this page, there is a stable version of VS-Code for download.

So, download and then run the downloaded file. All you need is to click Next and accept default options. After a few seconds, your VS-Code is ready to use.

Visual Studio Code
VS-Code Editor

Create your first Python project in VS-Code

The next step is to create a new python project in Visual Studio Code.

1.  Create a new folder at any location you want and name it as you want.

2. Open this folder in VS Code from File > Open Folder.

3. Create a new file inside the folder. To do so, click on New File icon. 

Creating a new file in your folder

Name it test.py, py is the extension for Python scripts.

4. Now the VS-Code recommends this extension:

The extension makes VS-Code an excellent Python code editor

This extension is the official Python extension from Microsoft that makes VS Code an excellent Python code editor. So, click on the Install button to begin the installation.

Our VS-Code is ready to use. Let’s test it. So, type this code in your code editor:

print("Welcome to my course!")

and click on run button that is located on the upper right side.

By doing so, it will run your Python code and displays it in Terminal window:

Terminal window displays the result of your code

Beautify your VS-Code

Now let’s change font size. In Visual Studio Code, by pressing Ctrl + keys and Ctrl – keys you can change the overall font size. But you can also, click on the Gear icon , select settings, search for fonts and change 14 with for example 22:

You can also change vs code color theme by clicking on File > Preferences > Color Theme.

If you have any problem in this section, please don’t hesitate to contact me by codewithbeh@google.com.

Also, If you enjoy this post or know someone who might find it helpful, please share it with them!