Alphalearningschool
        

<<< Prev

Next >>>

Up
Python Datatypes

  • The data store in memory in many types like student id store in numeric type student address store as alphanumeric character.
  • Let us see data types of python.
    • Python has five standard data types:
      • Number
      • String
      • Tuple
      • Dictionary
      • List 

Python Number Data Types:

  • Number data type store numeric value.
  • Whenever initialize variable number object created and it will check and given appropriate data type.
    • Ex:
      • var a= 10
      • var b= 20
      • If you want to delete the reference of variable you can also delete the reference by using del statement.
    • EX:
      • del var a and del var b.
      • You can also delete the multiple variables using del
    • Python support different numerical type:
      • int
      • long
      • float
      • complex (complex number Ex:45556.87789)

Python String Data Type:

  • String is a sequence of character represented in single or double quotation mark.
  • We can do mathematical operation( + ,* ) with string in python.
  • For finding the element of string we can use ( [ ] and [ : ] ) slice operator.
  • String index starting with “0”.
  • Let see example of string using mathematical functions.

>>> str = "abc";
>>> print (str)
abc
>>> print(str*2)
abcabc
>>> print(str+"def")
abcdef
>>> print(str/2)

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    print(str/2)
TypeError: unsupported operand type(s) for /: 'str' and 'int'

>>> print(str-"a");

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    print(str-"a");
TypeError: unsupported operand type(s) for -: 'str' and 'str'

>>> print(str[0]);
	a

 

If we did ( - , / ) mathematical function then it wouldn’t work.

Python List :

  • List is most important topic in python.
  • A list contains multiple data types like homogeneous and heterogeneous.
  • The list data represent in ( [ ] ) square brackets .
  • We can do mathematical operation same as string.
  • List is similar to array but more feature provide by list as compare to array.
  • Let see example of list. 
>>> list=["abc",123 ,"def",456]
>>> print(list)
['abc', 123, 'def', 456]
>>> print(list*2)
['abc', 123, 'def', 456, 'abc', 123, 'def', 456]
>>> print(list[0])
abc
tinylist =["ghi",789]
>>> print(list+tinylist)
['abc', 123, 'def', 456, 'ghi', 789]

 

Python Tuples :

  • Tuple is sequence of the data type and this similar to the list.
  • List closed like square brackets ( [ ] ) and tuple is closed like parentheses ( ( ) ).
  • Main difference b/w tuple and list , tuple size and element cannot be change one we declare but list size and element can be change.
  • Tuple can be use for read-only
  • Same as a string we cannot change the element of tuple but it possible to add a new tuple.
  • Let see example 
>>> tuple = ("a",1,"b",2,"c",3)
>>> print(tuple)
('a', 1, 'b', 2, 'c', 3)
>>> print(tuple[0])
a
>>> tinytuple=("d",4,"f",5)
>>> print(tuple+tinytuple)
('a', 1, 'b', 2, 'c', 3, 'd', 4, 'f', 5)
>>> tuple[1]=1000
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    tuple[1]=1000
TypeError: 'tuple' object does not support item assignment
>>>

Python Dictionary:

  • In dictionary element are store like key and value pair.
  • Dictionary is work like associative array or each hashcode of key have specific value.
  • Dictionary internally working as hashtable.
  • Dictionary element separated with comma and enclosed with curly braces { }.
  • Dictionary provide a method to get key () and value ().
  • Let see example ,
dictinory ={1:'a',2:'b',3:'c'}
>>> print(dictinory)
{1: 'a', 2: 'b', 3: 'c'}
>>> print(dictinory[1])
a
>>> print(dictinory.keys())
dict_keys([1, 2, 3])
>>> print(dictinory.values())
dict_values(['a', 'b', 'c'])
>>> tinydictionary={4:'d',5:'e'}
>>> print(dictinory+tinydictionary)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    print(dictinory+tinydictionary)
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>>