Introduction
What are actually variables in python and why do they call variables? Before going to the concepts of variables let's take an example, if you are doing a Google search for "Python" you'll get millions of results instantly, but have you ever wondered where is your search query actually stored temporarily for processing and getting you the results. Your search query must be stored somewhere so it can be processed. Here comes the concept of variables. Variables we can say are the containers for storing data. They stores data that needs to be processed throughout the program. Here we are going to look at variables and data types in python with examples.
What are variables in python?
Variables are temporary storage spaces that allocate a space in the memory for storing different types of data. In python, it is really simple to create a variable
>>> name = "PyCodeMates">>> print(name)output:PyCodeMates
Since python is dynamically typed we don't need to bother about declaring datatypes for variables. The type of variables are declared in the runtime and python will take care of it.
Here we created a simple variable called "name" which stores the data "PyCodeMates". Every variable holds a value which is the information associated with that variable. If we use "name" inside the print function in python, it will print the data stored in the variable to the console.
Python is actually doing a lot of stuff even you are just creating a variable that holds a single value. The python interpreter checks if there is any variable created in the program, if any it checks the data stored in the variable and initializes it in the memory. Again it checks the type of data stored in the variable for getting the memory space for storing the particular data. When we execute the print function, the python interpreter retrieves the data stored in the memory and prints it to the console.
Memory representation |
Rules for naming a variable
While giving a name for a variable there are certain conditions you must follow, which includes:
- A variable name must start with a letter or underscore, you can't user numbers at the starting of a variable name
- There cannot be any whitespaces between characters when creating a variable, for instance, the first name is not allowed while first_name is allowed.
- A variable name doesn't contain any symbols, which means it must be alpha-numeric
- Variables are case-sensitive, which means Name and NAME are two different variables even they are the same in meaning.
Data types available in python
Here are the basic data types available in python:
- Integer - Represent all integer values, eg: 1, 2, 3, 4, ...
- String - Represent all characters, eg: "Apple", "Orange", "Banana"
- Float - Represents real numbers written with a decimal point, eg: 1.5, 3.5, 945.25
- Boolean - Represent True or False statement.
# Variable storing a string data typename = "Jack"# integer data typeage = 19# Float data typeheight = 177.5# Boolean data typeis_married = Falseprint(name)print(age)print(height)print(is_married)output:Jack19177.5False
Things you can do with python variables
1. You can assign a single value to multiple variables:
>>> name1 = name2 = name3 = "Jack">>> print(name1)>>> print(name2)>>> print(name3)output:JackJackJack
2. You can re-declare a variable:
>>> a = 10>>> a = 50>>> print(a)output:50
3. You can use the same variable name for different data types:
>>> a = 100>>> a = "Welcome to PyCodeMates">>> print(a)output:Welcome to PyCodeMates
4. You can assign different values to multiple variables:
a, b, c = 100, 1.2, "PyCodeMates"print(a)print(b)print(c)output:1001.2PyCodeMates
Types of variables
There are two types of variables in python, The Local variable and the Global variable. Let's find out what these terms are:
Local variables: Variables that are mainly used inside a function are known as Local Variables. Its scope is restricted inside the function itself and cannot be used outside the function.
def greeting():# Declaring a local variablename = "Julie"print("Welcome", name)greeting()output:Welcome Julie
Here we defined a simple function that greets the user. Inside the function, the "name" is a local variable If you are not sure about functions, don't worry we will surely give tutorials about python basics on our website. But for now, you only need to concentrate on variables.
Global variables: Some variables that can be used everywhere throughout the program are known as Global variables. Their scope is in the whole program. In python, the global variable can be accessed by the global keyword.
