Variables are a vital part of any program as they store data to be processed in an application. For example, you might need variables to store the value of two numbers in an addition function and a separate variable for the result.
Variables are seen by the computer as memory chunks who are assigned differently to different data types. The data type of a variable tells the Python interpreter how much memory to assign to that variable.
In this tutorial, we will see different variable data types available in python and how to store values in these variables.
Storing Values In Variables In Python
Since Python doesn’t require explicitly mentioning data type of a variable to declare (allocate memory space to) a variable, the same syntax is used to declare all data types. The declaration is done automatically when you assign value to a variable.
“=” operator is used to assign value to a variable. The operand on the left of this operator is the name of the variable and to the right is its value. Following is the basic syntax for declaring and instantiating variables:
[variable name] = [value to be stored]
This creates a space with variable name in the memory and stores the value in that location. The following code stores the values Bob and 220 in the variables name and score.
name = “Bob” score = 220
You can do the following to print these variables on the screen:
The output will be:
Basic Data Types In Python
Following are three data types that you must know when starting with python:
Let’s study these in detail.
Number data type in Python is used to store numerical values in a program. Depending on the type of data you are storing, Python supports four different numerical types:
- int – Signed integers or simple numbers up to ±263
- long – long integers or numbers greater than ±263 (can also be hexadecimal or octal)
- float – floating-point or decimal numbers
- complex – complex or imaginary numbers
Some examples of these numbers are as follows:
a = 220 #int b = 26423783L #long c = 56.7 #float d = 2+4j #complex
If you want to store English sentences or words in Python, the string data type is used for that. Strings are treated as a set of characters in Python represented by quotation marks.
city = “New York”
Strings can be sliced using the slice operator  or [:] to access different parts of them. Indexes start from 0 at the beginning while the last digit can also be accessed by -1 and incrementing backwards.
print(city) #the complete string print(city) #first character of the string print(city[2:4]) #characters from 3rd to 5th print(city[2:]) #characters from 3rd to last print(city + “state”) #variable city and string state concatenated
The out of these will be as follows:
New York N w Y w York New Yorkstate
Lists are used to store related or unrelated sequences of data. A list is represented in python as items separated by commas within square brackets. An interesting thing about lists in python is that they can store values of different data types. Following is the declaration of a list variable:
myList = [2, 567, “hello”, 78.9, “Joe”]
Lists in Python can be sliced in the same manner as strings using the slice operator and indices.
print myList #complete list print myList #first element of the list print myList[1:3] #elements from 2nd till 4th print myList[2:] #elements from 3rd element to end print myList + [“world”, 45] #concatenated lists
The output will be as follows:
[2, 567, “hello”, 78.9, “Joe”] 2 [567, “hello”, 78.9] [“hello”, 78.9, “Joe”] [2, 567, “hello”, 78.9, “Joe”, “world”, 45]
This was all about declaring variables and their different data types in Python. If you have any questions, feel free to ask in the comments below!