Data Types In python

Data types

  • Number (Integer,boolean, Floating point; Complex)
  • None
  • Sequence (String, Tuples, List )
  • Sets
  • Mapping

Number

Number data type stores numerical values. This data type is immutable i.e. value of its object cannot be changed. Numbers are of three different types:
  • Integer & Long (to store whole numbers i.e. decimal digits without fraction part)
  • Float/floating point (to store numbers including fraction part)
  • Complex (to store real and imaginary part)

None

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

Sequence

  • A sequence is an ordered collection of items
  • Indexed by positive integers
  • Index starts with 0
It is a combination of mutable (a mutable variable is one, whose value may change) and immutable (an immutable variable is one, whose value may not change) data types. There are three types of sequence data type available in Python, they are Mutable.
  • Strings
  • Lists
  • Tuples

String

String-is an ordered sequence of letters/characters. They are enclosed in single quotes (‘ ‘) ordouble quotes (‘’ “). The quotes are not part of string. They only tell the computer about where the string constant begins and ends. They can have any character or sign, including space in them. String is immutable Declaring String Strvar = “Hello world!” Strvar =’Hello world’

Character

A string of with length one represents a character in Python. Declaring characters char =’a’

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 ([]).
Creating list
#Creating a empty list list =[] #Creating a list of numbers numlist=[1,2,3,4,5] #Creating character list charlist =[‘a’,’s’,’h’,’i’,’s’,’h’] #Creating list with integer, string & boolean mixlist =[1,”str”,True]

Tuples

  • Tuples are a sequence of values of any type and are indexed by integers.
  • They are immutable.
  • Tuples are enclosed in ().
Creating tuple
#The tuple contains any type of data tuple1 =(‘Delhi’,’jaipur’,1997,2000) #The tuple contains integers tuple2 =(1,2,3,4,5) #The tuple containing characters tuple3 =(”a”,”b”,”c”,”d”) #An empty tuple is created with empty parentheses () tupleempty =() # tuple with single element tupsingle =(50,)
To write a tuple containing a single value you have to include a comma, even though there is only one value

Set

Set is unordered collection of values of any type of unique elements.
  • It is immutable.
  • Set does not contains the duplicate values
  • Indexed by integer
  • The index value of tuple starts from 0.

Defining sets Syntax:

set ([sequence]) mport set # Create sets set(range(10)) #output # set([0,1,2,3,4,5,6,7,8,9]) # create an empty set set =()

Mapping

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

Dictionaries

  • It can store any number of python objects.
  • They are immutable.
  • What they store is a key -value pairs, which are accessed using key. Dictionary is enclosed in curly brackets ({}).
Defining dictionary Syntax:
Dictionary ={key:value,key1:value} # Creating the phone book with 2 key and values phonebook ={‘Ram’:’2341’,’Shyam’:’9102’,’Joseph’:’3258’} # An empty dictionary phonebook={}
  • Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
  • An empty dictionary without any items is written with just two curly braces, like this: {}.
  • Keys are unique within a dictionary while values may not be.
  • The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
Keys are unique within a dictionary. Values do not need to be unique within a dictionary.