Python : Data Types

Python has following datatypes

  • Numbers
    • int
    • float
    • complex
  • String
  • Collections
    • List
    • Tuple
    • Dictionary
Int
  • These are signed Integers.
  • They are positive or negative whole numbers with no decimal point.
  • Integers in Python 3 are of unlimited size.
  • Python 2 has two integer types – int and long. There is no ‘long integer’ in Python 3 anymore.
a = 22    # int
b = 3.14  # float
C = 1j   # complex
Float
  • Float represent real numbers and are written with a decimal point dividing the integer and the fractional parts.
  • Floats can be in scientific notation, with E or e indicating the power of 10
d =1.10 
e =1.0 
f =-35.59 
g = 2.5e2 = 2.5 x 100 = 250
Complex
  • Complex numbers are in the form a + bj, where a and b are floats and j (or J) represents the square root of -1 (This is an imaginary number).
  • The real part of the number is a, and the imaginary part is b.
  • Complex numbers are rarely used in Python programming.
p = 3+5j
q = 5j
r = -5j
Strings
  • Strings are the most widely used data types in Python.
  • Strings are created by enclosing characters in quotes either ‘single’ or “double”.
  • Python treats single quotes the same as double quotes.
var1 = 'hello' 
var2 = 'this is a String created @ 11:30 AM' 
#String operations
>>> 'hello'+'Jason'
'helloJason' 
>>> 'hello'*5
'hellohellohellohellohello' 
>>> 'hello'[1]
'e' 
>>> a[1:]
'ello' 
>>> a='hello'
>>> a[1]
'e' 
>>> 'e' in a
True 
>>> 'E' in a
False

Triple quotes can be used for providing multi line string character input

Python collections

Collection data can be stored in 4 different data types in python. Let us look at these one by one

lists
  • A list is a collection which is ordered and changeable.
  • Lists are written as comma-separated values (items) between square brackets.
  • Items in a list need not be of the same type
  • A list is a collection which is ordered and changeable.

Following are some of the examples of Lits (define, access, append, merge.

list1 = [1,2,3,4,5,6,7,8] 
list2 = ['one','two','three'] 
list3 = [1, 'two',3,'four'] 
#accessing list
>>> list1[0]
1 
>>> list1[-2]
7 
>>> len(list1)
9 
>>> 3 in list1
True 
>>> 1 in list1
False 
#updating list
list1[0] = 11 
print(list1[0]) 
11 
#slicing the list
>>> list1 = [0,1,2,3,4,5,6,7,8]
>>> list1[1:3]
[1, 2]
>>> list1[:3]
[0, 1, 2]
>>> list1[1:]
[1, 2, 3, 4, 5, 6, 7, 8]
# deleting list element
>>> list1
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> del list1[2]
>>> list1
[0, 0, 2, 3, 4, 5, 6, 7, 8]
# Methods for list
>>> list1
[0, 0, 2, 3, 4, 5, 6, 7, 8]
>>> list1.append(99)
>>> list1
[0, 0, 2, 3, 4, 5, 6, 7, 8, 99]
>>> list1.reverse()
>>> list1
[99, 8, 7, 6, 5, 4, 3, 2, 0, 0]
>>> list1.sort()
>>> list1
[0, 0, 2, 3, 4, 5, 6, 7, 8, 99]
# destructive read from the list
>>> list1.pop(1)
0 
>>> list1
[0, 2, 3, 4, 5, 6, 7, 8, 99]
>>> list1.pop()
99 
>>> list1
[0, 2, 3, 4, 5, 6, 7, 8]

Important List methods

Method Use
append() Adds element at the end
clear() removes all elements from list
count() Returns number of elements with specified value
pop() Removes element at specified position
reverse() Reverses the list
sort() Sorts list
Tuples
  • A tuple is a collection which is ordered and unchangeable.
  • The difference between the tuples and the lists is that the tuples cannot be changed unlike lists.
  • Tuples use parentheses, whereas lists use square brackets
thistuple = ("pune", "mumbai", "satara")
thistuple = tuple(("mango", "apple", "kivi")) # double round-brackets

 

Set
  • A set is a collection which is unordered and unindexed.
thisset = {"apple", "banana", "cherry"}
thisset = set(("apple", "banana", "cherry"))
Dictionary
  • A dictionary is a collection which is unordered, changeable and indexed.
  • Dictionary items are enclosed in curly braces.
  • Each items are separated by commas
  • Each key is separated from its value by a colon (:)
  • 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 tuple.
#Creating dictionary
firstDict = {'fname':'Jason','sname':'Bourne'} 
#Accessing dictionary
dict['fname'] 
#Updating dictionary element
dict['fname'] = 'Agent 007'; 
#deleting certain element
del firstDict['Name']
#Another way of creating 
dictthisdict = dict(apple="red", banana="yellow", cherry="pink")
#remove element
del(thisdict["banana"])
print(len(thisdict))

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.