DATA TYPES IN PYTHON

In python there are different basic data types .These are used to store the  data in memory and these can of different data types.

FOR EXAMPLE:
1) Any person age can be stored as a numeric value i.e. integer type.


2) Any person name can be stored as alphabets i.e. string type and his address is stored as alphanumeric characters.

Python has the following standard data types:

  • Integer
  • String
  • List
  • Tuple
  • Dictionary

PYTHON INTEGERS

Integer store the numeric values. The number objects are created for integer datatype when you assign value for them.
for example :    a=12, b=20
Here 12 & 20 is the numeric values that are stored in the a &b number objects .

We can also delete the reference to a number object by using the del statements
for example:  del a,b
It will the delete the objects and when you try to access these values of a and b ,it will show run time error or not defined here in the syntax.

It supports four different numerical types:

  1. int (singed integers) i.e.  -2,3,7...
  2. long int                     i.e. 2200, 11987..
  3. float                          i.e. 2.2 , 4.22...
  4. complex                    i.e. 2+4 j , 6+b....
For Example:
 >>>   a=10
          print(a)
>>> 10

PYTHON STRINGS

Python strings contain many characters and these characters are delimited using double /single quotes. Subsets of strings can be taken by using the [] and [:] . Placeholder of string is %s which is used to place the value of string in %s  or  string.format() ,this is another way to use placeholders.

The (+) operator is the string concatenation and the (*) asterisk is the repetition operator.

 EXAMPLE:

str ="IAM SUBHAM"
print(str)    
print(str[0])
print(str[2:5])
print(str[1:])
print(len(str))
print(str*2)

OUTPUT:

IAM SUBHAM
I
M S
AM SUBHAM
10
IAM SUBHAMIAM SUBHAM

PYTHON LISTS

A List contains items separated by commas and enclosed within square brackets[].
It is just like arrays in c lang ,only difference between them is that  all  the items belonging to a list can be of different datatype. The values stored in a list can be accessed using the [] and [:] ,with the indexes starting at 0 in the beginning and -1 at the ending.

EXMAPLE:

list = ["abcd" , 34.8 , 44 ,"john"]
tinylist = [123, "james"]
print(list)
print(list[0])
print(list[1:3])
print (list[2:])
print(len(list))
print(list + tinylist)

OUTPUT:
['abcd' , 34.8 , 44 , 'john']
abcd
[34.8 , 44]
[44, 'john']
4
['abcd' , 34.8 , 44 ,'john' , 123, 'james']


PYTHON TUPLE 

Tuples in python are enclosed in ( ) and can't be updated.A tuple  consists of a number of values separated by commas.The elements are not deleted and can't change the size  in tuples.It can be considered as read-only lists.

EXAMPLE:
tuple=("subham",18 , 611)
print(tuple)
>>('subham' , 18 ,611)
print(len(tuple))
>>3
print(tuple*2)
>>('subham' ,18 ,611 ,'subham' ,18 ,611)
  • The another example of tuple is:
tuple[1]= 22      #invalid syntax with tuples
del tuple[0]       #invalid syntax with tuples


PYTHON DICTIONARIES

Dictionary  is an un-ordered collection of key:value pair. A dictionary key can be almost of any python type , but are usually numbers or strings. Values,on the other hand,can be any arbitary python object. It is enclosed by curly braces ({ }) and values can be assigned and accessed using square braces([ ]).

EXAMPLE 1:

student={"bob":15,"shubhu":19,"rachel":13}
print(student)
print(student["rachel"])
print(student["bob"])

OUTPUT:

{'bob':15 , 'shubhu':19 , 'rachel':13}
13
15

EXAMPLE 2:

student={"bob":15,"shubhu":19,"rachel":13}

student["rachel"]= 18
print(student)
>>{'bob':15, 'shubhu':19 ,'rachel':18}

del student["bob"]
print(student)
>>{'shubhu':19 , 'rachel':13}

print(len(student))
>>2



Comments

Post a Comment

Popular posts from this blog

India-China Bilateral relations

First living robots created from frog cells: In USA

GULF COOPERATION COUNCIL NATIONS (GCC NATIONS)