Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Python Variables And Constants

4 min readJul 3, 2021

--

Press enter or click to view image in full size

Python Variables

A variable is a named location that is used to store data in the memory. It is basically a container that keeps data that can be used and changed later in the program.

It is okay if you still don’t get what a variable is! Just look at this example and keep moving!

myNumber = 15

Here, I have created a variable and its name is “myNumber”. Then, I assigned the value “15” to the variable using the assignment operator “=”.

Okay, let me explain it like this!

Let’s say a variable is a box that you can store a pencil in it, and that pencil can be replaced at any time!

myNumber = 15
myNumber = 17.5

In the beginning, the value I assigned to “myNumber” was “15”. Later, I changed it to “17.5”. That’s how you can use variables in Python. Piece of cake!

Now, let’s see how we can assign values to variables in Python.

As I mentioned before, we can use the “assignment operator =” to assign a value to a variable, as you can see in the previous example.

Declaring and assigning value to a variable

myName = "Scooby-Doo"
print(myName)

I have written a simple program, declaring a variable named “myName”, and assigning the value “Scooby-Doo” to it. Then, using the “print” keyword, I’m going to get the value of the variable “myName” as the output. When I execute and run this program, the output will be as follows.

Output

Scooby-Doo

“Python is a type-inferred language, so you don’t have to explicitly define the variable type. It automatically knows that “Scooby-Doo” is a string and declares the “myName” variable as a string.”

Changing the value of a variable

# initial assignment
myName = "Scooby-Doo"
print(myName)
# Cahnging the value of myName
myName = "Scrappy-Doo"
print(myName)

Here, I have initially assigned “Scooby-Doo” to the variable “myName”. But, later, I changed it and assigned “Scrappy-Doo” to the variable “myName”. Take a few seconds, and try to comprehend the output.

YES! YOU GOT IT RIGHT!

Output

Scooby-Doo
Scrappy-Doo

Assigning multiple values to multiple variables

It is a fact that “Python Is Easy!” To prove it, let me show you something interesting.

Get Nirushi Wijesiri’s stories in your inbox

Join Medium for free to get updates from this writer.

If you want to assign multiple values to multiple variables using only one line of code, you are allowed to use the following way in Python.

myName, myAge, myBestie = "Scooby-Doo", 5, "Shaggy"

Also, if you want to assign the same value to multiple variables, you can do it like this.

myLastName, myMothersLastName, myFathersLastName = "Doo"

That is all you should know about the Python variables!

Python Constants

In Python, constants are usually declared and assigned in a module.

Wait, What! A MODULE? What’s that?

Let me make it clear to you!

The module is nothing but a new file containing variables, functions, etc which is imported to the main file. Don’t worry! You will see how it is done in a while!

Declaring and assigning value to a constant

Step 1: Create a constant.py

PI = 3.14
TAU = 6.28

Step 2: Create a main.py

import constantprint(constant.PI)
print(constant.TAU)

In the above program, I have created a constant.py module file and assigned values to PI and TAU. Then I created a main.py file and imported the constant module using the “import constant” statement on top of the code.

Now, I can call the variables which I have declared in the constant.py module, inside the main.py file! Just like I have shown in the example. And the output is going to be as follows.

3.14
6.28

That is the beauty of Python!

Rules And Naming Conventions For Variables And Constants

  • The names of the constants and the variables should be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
camelCase
CapWords
snake_case
MACRO_CASE
  • It is a good practice if you can create a name that makes sense. For example, if you want to store a name, “name1” makes more sense than “n1”.
  • If you want to create a variable name having two words, it is better if you can use the “snake_case” type (use underscore to separate the words).
first_name
second_name
  • Use capital letters to declare a constant.
TEMP
SPEED_OF_LIGHT
MASS
  • Never use special symbols like !, @, #, $, %, etc.
  • Don’t start a variable name with a digit.

That is it about Python variables and constants. Hope this article helped you to learn something new! Always remember, it is never too late to start working on the things that sharpen your knowledge!

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com