Variables and Data Types

Variables

Varible is a container which is used to store data.
Example
a = 8
Here a is a variable that is storing the value of 8.

Open your IDLE and write
a = 8
Hit enter and then write a. Again hit enter.
You will get 8 as output.

>>>a = 8
>>>a
output: 8

Its like simple mathematics.
Few more examples:

>>b = 12.5
>>>b
output: 12.5
>>>str = 'this contains string'
>>>str
output: 'this contains string'

Last thing to say is that while naming your variable, always use meaningful and precise names not too lengthy or short. You can use any alphanumeric characters, digits and underscore for naming your variable. Keep in mind variable names beginning with digits are not allowed. Variable names are case sensitive.

var1, Var1, VAR1 and _var are all different possible variable names while 8var, $var are not correct way of defining variables names and will give syntax errors.

Data Types

Python is a loosly typed language. Loosely typed means that you don't have to define what data kind of data you are going to store in a variable. Python decides it dynamically while executing.

In other object oriented programming languages, while defining your variable you also have to declare its data type.

Various possible data types in Python are:

  • boolean: True or False
    >>>True
    output: True
    
  • numbers: It can be integer number (eg 8), floating point number (eg - 12.5), complex number (eg - 1+2j) or fractions (eg 1/2)
    >>>a = 1
    >>>a
    output:1
    >>>b = 12.5
    >>>b
    output:12.5
    >>>c = 1 + 2j
    >>>c
    output:(1+2j)
    >>>d = 1/2
    >>>d
    output:0.5
    
  • string: List of characters forming string and are always enclosed in single or double quotes. Eg - 'hello'
    >>>str = 'hello'
    >>>str
    output:hello
    
  • list: Containers that can store sequence of data. Eg - [1, 2, 3]
    >>>mylist = [1,2,3]
    >>>mylist
    output:[1, 2, 3]
    
  • tuple: Similar to list but is immutable. Eg - (1, 2, 3)
    >>>mytuple = (1,2,3)
    >>>mytuple
    output:(1, 2, 3)
    
  • set: Similar to lists but is unordered. Eg - {1, 2, 3}
    >>>myset = {1,2,3}
    >>>myset
    output:{1, 2, 3}
    
  • dictionary: it stores data as a key/value pair. Eg - {'a':1, 'b':2}
    >>>mydict = {'a':1, 'b':2}
    >>>mydict
    output:{'a':1, 'b':2}
    
  • bytes and array of bytes: For storing image files. Eg - jpg

We will discuss these different data types in upcoming lessons. Lets talk about getting input in next lesson.

<< Displaying Output Taking Input >>