Python Variables and Datatypes

Code Computer Skills Python Web Development

Like it is essential to learn the alphabets and grammar before learning any language, it is important to have an overview of variables and data types before getting into any programming language.

Variables

A unique feature of variables in Python is that they do not need any explicit declaration. It means there is no need to specify what type of data you are going to store. Just declare the name and assign whatever you want to store. The variables will recognize the data type automatically.

Note that Python has five standard datatypes:

  • Numbers
  • String
  • List
  • Tuples
  • Dictionary

You should know that a variable is actually created first when they are assigned and they must not be referenced anywhere in the program before their assigning statement.

Once you give the necessary data, the interpreter allocates the required memory depending on the datatype.

Some naming rules to be followed while naming of variables are:

  • Choose a meaningful name instead of a short name.
    Example
    : name is preferable to nm.
  • Keep the length of the variable as short and precise as possible.
  • Be consistent with whatever name you give to your variable.
  • Begin a variable name with an underscore ‘_’ character for a special name.
  • The assignment procedure is:

<variable name> = <value of variable>

  • You can also follow multiple assignment of variables as:

<var1> = <var2> = <var3> = <value of variables>

  • You can reuse and shift from one datatype to another one just by reassigning them.
    #variable type is integer
    randomvar= 10
    print(randomvar) 
    
    #variable type changes to string with updated variable.                   
    randomvar= “Mario"           
    print(randomvar)

Datatypes

Datatype represents the kind of value. All datatypes in Python are encapsulated in their own classes. Remember that everything in Python is an object and every object has an identity, a type, and a value. Like any other object-oriented programming language, many different types of data are allowed to be stored here in Python.

Apart from that, you can also define modules like a function-oriented programming language to define a datatype which shows some different characteristics from the given one. Some common datatypes which we are going to use are:

1. Numeric Types:

Python supports three different types of numbers as input:

  1. Integer: support both positive and negative numbers
  2. Floating Point: supports positive and negative along with decimal values of the number
  3. Complex Numbers: allows us to give input in the real and imaginary form.

To check for complex number, you can run the following code:

cmplxnum = 5+7j
print(type(cmplxnum))

When you run this program, it will show you the type as a complex number.

NOTE: type() is an inbuilt function that allows us to determine the datatype of a variable at any instant.

2. Boolean Types:

Like any other language, Boolean represents two values: True and False. You can verify a variable to be Boolean by following the exact steps mentioned above.

3. String Types:

Strings in Python contain some of the most advanced features which we will study in further modules. But these are some basics which you should be accustomed to.

Few points on Strings are:

  • These are basically a sequence of single individual characters stored in the format of an array in memory.
  • Python strings are immutable.
  • We can represent a string by using either single quotes or double quotes. Make sure not to jumble them up i.e. starting with single quotes and ending with double quotes and vice versa.

You can declare a string in this way:

strvar = "My name is Mario"
print(strvar)

Apart from that, there are some common special characters in Strings which are:

  • \n : called as ‘newline’. Shift the lines following this symbol to the next line.
  • \t :  called as ‘horizontal tab’. Give a tab space to lines following this symbol.
  • \\ : called as ‘Backslash’. Prints the backslash symbol in the given position.
  • \’ : prints a single quote at the given position.
  • \” : prints double quote at the given position.

4. Lists:

A list is considered as a compound datatype in Python. By compound we mean, it can have multiple values allotted to a variable at a time. But note that, the values are accessed one by one only.

You can consider a Python list as an array, but it is different in terms of the fact that it can store continuous data of different datatype at the same time, while an array accepts only one type of data.

A list is written by enclosing the values in square brackets while storing the different values separated by comma.

We will study lists in more detail as the course advances.

list = [25, 36.7, 'India', 'a',true]
print(list)          #prints the entire list
print(list[2])       #prints the 3rd element of the list
print(list[0:2])     #prints list from beginning to 3rd position
print(list*3)        #prints a list thrice

5. Tuples:

Tuples are much similar to Lists. The only difference between them is that Lists are enclosed by square brackets while Tuples are enclosed in parenthesis.

Another major difference is, that Lists can be upgraded in terms of their size, but a Tuple is read only once it is created along with its values.

We will study Tuples in more detail as the course advances. Meanwhile, try and create a basic Tuple and execute the above operations we did in Lists.

6. Dictionaries:

Python Dictionaries are a type of two-dimensional table. The inputs are given to them in order of keys and values. It means that we define a generalized area for giving the range of inputs, and then we provide it with a particular input.

The generalized area is called the key, while the particular input becomes the value.

For example:

sampledict = {}
sampledict[1] = "I belong to India"
sampledict[2] = "I belong to Odisha"
print(sampledict[1])
print(sampledict[2])
sampledict = {'name': 'Ramesh', 'city': 'Bhubaneswar', 'Work': 'TCS'}
print(sampledict)
print(sampledict.keys())
print(sampledict.values())

Dictionaries play a very important role in any application of python. We will be unfolding this topic in stages as we proceed through this course.

Meanwhile here is an assignment for you to think:

Try and create a dictionary which has 5 instances and stores the name, roll number, class, section, and school as info. Print them in serial order. Then create another dictionary which takes all the values at a time and print the keys and values separately. What is the benefit of having a single instance dictionary and ordered dictionaries?