Python Fundamentals for Everybody — Variables

This is the second of many articles on Python Fundamentals for Everyone, a python tutorial series which focuses on python fundamentals.

You can refer to previous article written in this series below.

At the end of this article you will have knowledge on what are variables in python, how are they assigned and the basic operations the can be performed by various operators in python.

Without much ado, let’s just dive into it!

So, what is a variable?

Just like how we have a store room in our house where we store things, in any programming language (Python, C, C++), variable is a storage location where some value is stored.

The value stored inside a variable can be accessed or updated later.

A value may contain similar or different type of values ie., it may contain numeric, alphabetic, alphanumeric or even special symbols.

For example:

>>> str = “Hello World”

So here, ‘str’ is a variable which contains alphabetic values. In programming terms, ‘str’ is called as the variable name which holds string value. Therefore, we say that, ‘str’ is of datatype string.

>>> num = 2

Similarly, ‘num’ is a variable name which holds numeric value, hence, ‘num’ is of datatype integer.

>>> pi = 3.14

And ‘pi’ is a variable which contains floating point (decimal values) so, ‘pi’ is of datatype float.

Python supports built-in function ‘type()’ which takes values as arguments and returns it’s data type.

Image by author

Built-in functions are basically already existing chunk of code that are present inside the python interpreter and is always available.

For instance, when you want to print something, you just write >>>print(‘hello world’) and hello world is printed.

But what actually happens is that, a chunk of code present in the interpreter, is executed when ever print() is called.

Can I give any name to the variable as I wish? Well, python variables have some name rules.

  • Python is a case sensitive language which means that a variable Student and student are treated as different
  • A python variable must begin with either letters (a-z or A-Z) or underscore(_)
  • Reserved names cannot be used as a variable name, for example ‘if’ cannot be used as a variable name because ‘if’ is a reserved word which is used to check conditions.

The following code snippet shows some examples of valid and invalid variable names in python.

Image by author

Python Assignment

By now, from the above examples you must be familiar with how the variables are assigned values, ie., <variable name> =<expression>

For example, str = ‘hello’

Python also supports multiple assignments these are also called as chain assignments. In case multiple variables hold a single value then it can be assigned like this: x=y=z=1

Here, x, y and z are all assigned a single value 1.

Image by author

Well, well done on sticking till the end of this article! ;)

In the next article we will look at Operators and Operands.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Analytics Vidhya

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

Sneha Thomas Mathew

Written by

Decision Scientist. Aiming to share my knowledge on python and data science right from the basics.

Analytics Vidhya

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

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store