Different datatypes in Python| string, list, tuple, dictionary, numeric

 DATA TYPES IN PYTHON 



Data types in python

 

There are many data types are in python but in proper way there is just 5 data types and these are also divided in sub data types. 

1) numeric datatype

2) string datatype

3) list datatype

4) tuple datatype

5) dictionary datatype

 

These are I think you studied, but here today we will learn about these datatype in full detail. 

Let us start

First i.e

I.Numeric datatype -it contains number like integer, octal, hexadecimal and complex numbers. 

So now understand one by one 

1.integer means the numbers between 1 to infinity postivie numbers. 

2.octal number which is denoted by 0o10 here first digit is zero and second is O represent octal and then your number. 

It's base is 8.

If you convert any number into octal number then understand by giving examples:

 

a=10

b=oct.(a) 

print(b) 

 

Similarly you can input any number and use oct() function to convert into octal number. 

 

3.hexadecimal number -its base is 16

It is represented by Oxa

Here First digit is zero then second digit is x represent hexadecimal and then your number and here a is for 10 (Ascii value). 

 

A=10

B=hex.(A) 

print(B) 

 

4.complex number-which contains real and imaginary number. Example

A=4+5j

Here j represent imaginary. 

 To print real and imaginary part separately. 

 

 

Example:

A=5+6j

print(A.real(), A. imag()) 

 

 

5.float-the number which contains decimal part also. 

Example:

A=35.0

B=56.7

These are float numbers, if you use float() function in your program then float() function convert into that number in also decimal. 

 

7.eval=the number which contain fraction or positive or negative number. 

You can use eval() function in program if the number would be negative or positive. 

 

Now we will talk about 

II.String datatype:

String is a sequential datatype which is contain inside the single or double or triple quotes. 

It is immutable datatype it mean it is not changeable. 

Let us understand, 

A="This is python "

B='it is food

C=''' They are good'''

 

All these are string

Operations on string using function

·       str.join() it joins two string

Example:

A="ABC"

B="Sumit" 

print(A.join(B)) 

Output:

SABC u ABC m ABC i ABC t ABC

·      replace() function

It helps to replace any word by given your word

Example:

A="This is python coding"

print(A.replace("coding", " language ")) 

Output:

This is python language

·       len() function it is used to find length of given string.  Which start from 1.

Example:

A=" Python"

print(len(A)) 

Output:

6

·      index() function

It helps to find index number of any number which start from 0.

Example:

A="Coding"

print(index('C')) 

Output:

0

print(index('i)) 

Output:

3

·       split() -split function helps in to convert string to list. 

Let understand by example:

S="this is good"

print(S.split()) 

Output:

['this','is', 'good']

 

·       partition() function -it is used to divide the string in three parts, therefore it's length always 3.and it will just contain 3 elements. 

And it convert into tuple datatype. 

It means if you want to any string into list then you use split() and to convert into tuple datatype then you can use partition() function but it is not fully correct way. 

As sometime same thing you have to convert into list tuple without changing their index value. So for that you have to use

String to list use list() function

String to tuple use tuple() 

Note:Be aware  about that function always should be in small letters. 

S="Python is computer language "

print(S.partition('is')) 

Output:

('Python's, 'is', 'computer language') 

 

 

III.List datatype

List datatype is mutuable datatype. In which any change we can do in the list. We can change the the elements and we can access the elements to do more work. 

Method To write a elements (number or name) in the list i.e 

[] in square brackets. 

L=[1, 3,46,7,100]

This is called list. 

Function in list are:

·       max() function -it  is used to find max number from the list. 

Example:

L=[4, 7,16,68,53]

print(max(L)) 

Output:

68

If you want to make a program to find max and minimum from the without using function you click on below link:

Program to find maximum and minimum from the list

 

·       min() function-helps to find minimum value from the list. 

L=[4, 8,0,14,31,6,]

print(min(L)) 

Output:

0

type() function-to find given statement is which type. 

A="aghj"

print(type(A)) 

Output:

<string>

·       index() function-this function use in string, list, tuple and dictionary datatype, but in dictionary the element of dictionary we cannot access by index value. 

It helps to index number of any element in list in string, in tuple etc. 

L=[6, 79,15,10,86]

print(L.index(6)) 

Output:

0

print(L.index(86)) 

Output:

4

L=["ram", "shyam", " Mohan"]

print(L.index("ram")) 

Output:

0

I think now you have understood the concept of index. 

·       clear() function-this function only use in list datatype. 

It is used to clear the whole list and the output will be an empty list ([ ]). 

 

4.Tuple datatype-

Tuple datatype is also sequential datatype and it immutuable. 

It means we can just find the length of the tuple, only find the index value of the tuple that's it. 

So similarly we can find here also length and index value element. 

Tuple convert into list

Use list() function. 

Example:

T=(4, 6,7) 

print(list(T)) 

Output:

[4, 6,7]

Tuple convert into string use str(). 

T=(6, 5) 

print(str(T)) 

Output:

(6, 5) 

 

V.Dictionary datatype-Dictionary datatype is unsequential datatype and it contains key and value both separate by colon sign (:). 

Dictionary is  also called mapping datatype. 

In dictionary keys are immutable (not changeable) and it's value are changeable. 

How to change value in dictionary. 

Let us understand, 

Example:

D={1:56, 2:58, 3:100}

D[2]=90

print(D) 

 

Output:

{1:56, 2:90, 3:100}

·       del () function is used to delete key and it's value using the index number. 

Finally, you have understood all these data type in detail. 

 

 Question Related:

Q.What are immutuable datatype?
Ans.Dictionary,tuple,string.


Q.Which function is used to find the length of any datatype?
Ans.len() function


Q.Which function is used to remove any element using index number from the list?
Ans.remove() function.

Q.How to convert string to tuple?
Ans.tuple() function

Q.Name the function to find id of any element?
Ans.id() function

 

 Q.How to square root of any number?
Ans. import math
        print(math.sqrt(4))
Output:
2

 

 


No comments:

Powered by Blogger.