Elements of Variable and Data type :

Variable is a name that refers to a value. When we create a program, we often like to store values so that it can be used later. We use variables to capture data, which then can be manipulated by computer to provide information/store value /display value.

Every Variable has 3 elements:

  1. An Identity  : It is the object’s address in memory and does not change once it has been created, can be known using id  (object).
  2. A Type (Data Type) : A data type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data and specify which type of value a variable has. It is a set of values, and the allowable operations on those values.
  3. A value: To bind value to a variable, we use assignment operator (=). This is also known as building of a variable.

Different types of data use in Python: There are various type of data use in python are basically classified into 5 types:

Elements of Variable and Data type :
  1. )Numbers: Number data type stores Numerical Values. This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:
            A) Integer & Long :

Integers are the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17. When we want a value to be treated as very long integer value append L to the value. Such values are treated as long integers by python.      Examples:

>>> a = 10
>>> b = 5192L
 >>> c= 4298114
>>> c = c * 5669
 >>> type(c) <type ‘long’>

We can know the largest integer in our version of Python by following the given set of commands:
>>> import sys
>>> print sys.maxint

Integers contain Boolean Type which is a unique data type, consisting of two constants:

  1. True
  2. False.

 A Boolean True value is Non-Zero, Non-Null and Non-empty.

Example

>>> flag = True
>>> type(flag)

Output : <type ‘bool’>

B) Floating Point :

Numbers with fractions or decimal point are called floating point numbers. A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in engineering/ scientific notation.

-2.0X 105 will be represented as -2.0e5

2.0X10-5 will be 2.0E-5

Example:
y= 12.36

A value when stored as floating point in Python will have 53 bits of precision.

C) Complex:

Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by “ j “ instead of  “i‟, so 1+0j denotes zero imaginary part.

Example :

>>> x = 1+0j
>>> print x.real,x.imag

Output :  1.0 0.0

Example

>>> y = 9-5j
>>> print y.real, y.imag

Output : 9.0 -5.0

2) None :

This is special data type with single value. It is used to signify the absence of value/false in a situation. It is represented by None.

3) Sequence :

A sequence is an ordered collection of items, indexed by positive integers. It is combination of mutable and non mutable data types.

Three types of sequence data type available in Python are Strings, Lists & Tuples.

3.1) String: It is an ordered sequence of letters/characters. They are enclosed in single quotes ( ‘ ) or double ( ‟ ). The quotes are not part of string. They only tell the computer where the string constant begins and ends. They can have any character or sign, including space in them. These are immutable data types.

Example :

>>> a = ‘Ram’

  • A string with length 1 represents a character in Python.
    • Lists: List is also a sequence of values of any type. Values in the list are called elements / items. These are mutable and indexed/ordered. List is enclosed in square brackets.

Example :

l = [“spam”, 20.5,5]

  • Tuples: Tuples are a sequence of values of any type, and are indexed by integers. They are immutable. Tuples are enclosed in ().
  • Sets  : Set is an unordered collection of values, of any type, with no duplicate entry. Sets are immutable.

Example :
s = set ([1,2,34])

  • Dictionaries: Can store any number of python objects. What they store is a key – value pairs, which are accessed using key. Dictionary is enclosed in curly brackets.

     Mapping  : This data type is unordered and mutable. Dictionaries fall under Mappings

Example :

   d = {1:’a’,2:’b’,3:’c’}

For more detail on the above topic ” Elements of Variable and Data type: ” you may also write to us @: [email protected]

Elements of Variable and Data type

Sources : www.python.org


Book Followed: http://cbse.nic.in/ePub/webcbse/webcbse/ab-cbse-book-5.html

To check about an earlier topic on Python click below:

https://www.soluversity.in/what-is-python-and-why-so-popular
” https://www.soluversity.in/frameworks-available-for-developing-various-types-of-application-in-python “

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *