DATA TYPES
IN
PYTHON
Data types
Number None Sequence Set
Dictionary
Integer Float Complex
String Tuple List
NUMBER
Number data types store numeric
values. They are immutable data types,
means that changing the value of a
number data type results in a newly
allocated object.
There are three numeric types to
present numbers-
1 Integer
2 Float
3 Complex
INTEGER
An integer is a whole number with no
decimal places. For example, 1 is an
integer, but 1.0 isn’t. The name for the
integer data type is int.
Ex- type(1) <class 'int'>
FLOATING-POINT
A floating-point number, or float for short, is
a number with a decimal place. 1.0 is a
floating-point number, as is -2.75.
Ex- type(1.0) <class 'float'>
COMPLEX
complex fare of the form a + bJ, where a and
b are floats and J (or j) represents the square
root of -1 (which is an imaginary number).
The real part of the number is a, and the
imaginary part is b. Complex numbers are
not used much in Python programming.
Ex- type(1+2j) <class 'float'>
NONE
The None keyword is used to define a null
value, or no value at all.
None is not the same as 0, False, or an
empty string. None is a data type of its own
(NoneType) and only None can be None.
Ex-If you do a boolean if test, what
will happen is None, True or False
x = None
if x:
print("Do you think None is True?")
elif x is False:
print ("Do you think None is False?")
else:
print("None is not True, or False, None is just None...")
Output-None is not True, or False, None is just None...
Sequences allow you to store multiple
values in an organized and efficient
fashion
1 STRING
SEQUENCE 2 TUPLE
3 LIST
STRING
String are arrays of bytes representing Unicode
characters. A string is a collection of one or
more characters put in a single quote, double-
quote or triple quote. In python there is no
character data type, a character is a string of
length one. It is represented by str class.
To Access characters of String
String1 = “Python" Output-
print("Initial String: ") Initial String:
print(String1) Python
print("\nFirst character of Output-
String is: ") First character
print(String1[0]) of String is: P
Tuple
A tuple is also a heterogeneous collection of
python objects seperated by commas .It means
object of different data types can consist of
tuple.
Ex- first_tuple=(2,3,4,6)
Method of Tuple
Python has two built-in methods that you can use on tuples.
Count()- Returns the number of times a specified value
occurs in a tuple.
Index()- Searches the tuple for a specified value and
retuns the position of where it was found.
LIST It is a heterogeneous collection of items of varied
data types. For Ex, a list object can store files in a
folder or the employee data in a company etc.
Ex- List=[1,2,3,4]
O/P-List=[“Annu”, ”Mini”, 33, 44]
//Heterogeneous collection of items
METHOD OF LIST
Insert() Add an element at the specified position
Remove() Removes the fist item with the specified value.
Returns the number of elements with the
Count()
specified value.
Clear() Removes all the elements from the list.
SET
A set is an unordered collection of unique and immutable
objects. Its definition starts with enclosed braces {} having
its items separated by commas inside.
Creating a set-
Set1={“BMW”, “Ferrari”, “Tesla”, “Ford”, ”Honda”}
Set2={Honda”,”Yamaha”,”Kawasaki”,20,40}
METHOD OF SET
Union()- Return a set that contatins all items from both
sets, duplicates are excluded.
Intersection()- The intersection() method returns a set that
contains the similarity between two or more sets.
Difference()- Return the difference two sets which is also
a set.
Issubset()- The issubset() method returns True if all items
in the items in the set exists in the specidied set,
otherwise it return false.
DICTIONARY
A dictionary in Python is an unordered collection of key-value pairs.
Ex- Dict1= { Accessing values
“Roll_No” : 101
Print(Dict1[Roll_No])
“Name” : “Kajal”,
// output Dict1[Roll_No]) : 101
“Caste” : “Soni”, Print(Dict1[‘Name’])
“Class” : 12 // output Dict1[‘Name’]) : Kajal
METHOD OF DICTIONARY
Get()-Returns the value of the specified key.
Update()- Updates the dictionary with the specified key-vlaue
pairs.
Pop()- Removes the element with the specified key.
Copy()- Returns a copy of the dictionary.