Data types

0
50

Data can be of many types e.g., character, integer, real, string etc. Anything enclosed in quotes represents string data in Python. Numbers without fractions represents integer data.

Before you learn how you faan process different types of data in Python, let us discuss various data-types supported in Python.

Built- in Core Data Types

  • Numbers (int, float, complex)
  • string
  • List
  • Tuple
  • Dictionary

Numbers  

As it is clear by the name the Number data types are used to store numeric values in Python.

(i) Integers                                                                                                                                – Integers (signed)                                                                                                        -Booleans

(ii) Floating-Point Numbers                                                                                            (iii) Complex Numbers

Integers 

Integers are whole numbers such as 5, 39, 1917, 0 etc.

Types of Integers in Python                                                                                          (i) Integers (signed)                                                                                                    (ii) Booleans

(i) Integers (signed). It is the normal integer representation of whole numbers.

(ii) Booleans. These represent the truth values False and true. The Boolean type is a subtype of plain integers, and Boolean values False and True behave like the values 0 and 1, respectively. To get the Boolean equivalent of 0 or 1, you can type bool(0) or bool(1), Python will return False or True respectively.

Floating Point Numbers

A number having fractional part is a floating-point number. The decimal point signals that it is a floating-point number, not an integer. The number 12 is an integer, but 12.0 is a floating-point number.

Floating-point numbers have two advantages over integers :

  • They can represent values between the integers.
  • They can represent a much greater range of values.

But floating-point numbers suffer from one disadvantage also :

  • Floating-point operations are usually slower than integer operations.

Complex Numbers

Python is a versatile language that offers you a numeric type to represent complex Numbers also.

A complex number is in the form A + Bi where i is the imaginary number, equal to the square root of -1 i.e., √-1, that is i² =-1.

Strings

The string data type lets you hold string data, i.e., any number of valid characters into a set of quotation marks.

A string can hold any type of known characters i.e., letters, numbers, and special characters, of any known scripted language.

Following are all legal strings in Python :

“abcd” , “1234”, ‘$%^&’ , ‘????’ ,  “ŠÆËá” , “??????” , ‘????’ , “????” , ‘???’ , “??”

List, Tuples and Dictionary

The lists and tuples are Python’s compound data types. We have taken them together in one section because they are basically the same types with one difference. List can be changed / modified (i.e., mutable) but types cannot be changed or modified (i.e., immutable). Let us talk about these two Python types one by one.

Lists

A List in Python represents a list of comma-separated values of any datatype between square brackets e.g., following are some lists :

[1, 2, 3, 4, 5]
['a', 'e', 'i', 'o', 'u']
['Neha', 102, 79.5]

Like any other value, ytou can assign a list to a variable e.g.,

>>> a = [1, 2, 3, 4, 5]          #statement 1 
>>> a
[1, 2, 3, 4, 5]
>>> print (a)
[1, 2, 3, 4, 5]

To change first value in a list namely a ( given above), you many wrirte

>>> a [0] = 10   #change 1st item - consider statement 1 above
>>> a 
[10, 2, 3, 4, 5]

To change 3rd item, you may write

>>> a [2] = 30                 #change 3rd item
>>> a 
[10, 2, 30, 4, 5]

You guessed it right ; the values internally are numbered from 0 (zero) onwards i.e., first item of the list in internally numbered as 0, second item of the list as 1, 3rd item as 2 and so on.

We are not going further in list discussion here. List shall be discussed in details in a later chapter.

Tuples

You can think of Tuples (pronounced as tu-pp-le, rhyming with couple) as those lists which cannot be changed i.e., are not modifiable. Tuples are representedas group of comma-separated values of any type within parentheses, e.g., following are some tuples :

p = (1, 2, 3, 4, 5)
q = (2, 4, 6, 8)
r = ('a', 'e', 'i', 'o', 'u',)
h = (7, 8, 9, 'A', 'B', 'C',)

Tuples shall be discussed in details in a later chapter.

Dictionary

Dictionary data type is another feature in Python’s hat. The dictionary is an unordered set of comma-separated key : value pairs, within {}, with the requirement that within a dictionary , no two keys can be the same (i.e., there are unique keys within a dictionary). For instance, following are some dictionaries :

{'a' : 1, 'e' : 2, 'i' : 3, 'o' : 4, 'u' : 5}
>>> vowels['a'] = {'a' : 1, 'e' : 2, 'i' : 3, 'o' : 4, 'u' : 5}
>>> vowels['a']               Here 'a','e','i','o' and 'u' are the keys of 
1                              dictionary vowels:
                              1, 2, 3, 4, 5 are values for these keys respectively.
>>> vowels['u']               Specifying key inside [] after dictionary name gives 
5                               the corresponding value from the key : value pair 
                              inside dictionary.

Dictionaries shall be covered in details in a later chapter.

MUTABLE AND IMMUTABLE TYPES

The Python data objects can be broadly categorized into two- mutable and immutale types, in simple words changeable or modifiable and-modifiable types.

Immutable types

The immutable types are those that can never change their value in place. In Python, the following type are immutable :

  1. integers
  2. floating point numbers
  3. booleans
  4. strings
  5. tuples
sample code 7.1
  p = 5 
  q = P
  r = 5
         :       #will give 5, 5, 5, 
 P = 10
 r = 7 
 q = r

After reading the above code, you can say that values of integer variables p, q, r could be changed effortlessly. Since p, q, r are integer types, you may think that integer types can change values.

But hold : It is not the case. Let’s see how.

You already know that in Python, variable-names are just the references to value-objects i.e., data values. The variable-names do not store values themselves i.e., they are not storage containers.

So although it appears that the value of variable p / q / r / is changing; values are not changing “in place” the fact is that the variable-names are instead made to refer to new immutable integer object. (Changing in place means modifying the same value in same memory location).

Mutable types

The mutable types are those whose values can be changed in place. Only three types are mutable in Python. These are:

  1. lists
  2. dictionaries
  3. sets

To change a member of a list, you may write :

Chk = [2, 4, 6]
chk[1] = 40

See, even after changing a value in the list chk, its reference memory address has remained same. That means the change has taken in place the lists are mutable.

Note

Mutable objects are :

  1. lists
  2. dictionary
  3. set

immutable objects:

  1. int
  2. float 
  3. complex
  4. string 
  5. tuple.

LEAVE A REPLY

Please enter your comment!
Please enter your name here