2.0 Python Data Types
Python has five standard data types –
Numeric types: Sequences: Sets: Mappings: Boolean:
Int str set dict
Long byte frozen set
Float list
Complex tuple
1) Numeric types:
Integers:
Integers are any numbers without decimals
#Int assignment exmaple
a=1
print(a)
o/p:
1
Long:
no long in python 3
Float
# float
a = 1.0
type(a)
Complex:
Complex literals can be created by using the notation x + yj
x =real component
y = imaginary component.
2) Sequences
2.1 Strings
strings are sequence.
Strings are immutable.
[] are used for Indexing. Indexing starts at 0.
String literals can be defined with any of single quotes ('), double quotes (") or triple quotes (''' or """)
2.2 List4 built-in data types in Python are List, Tuple Set and Dictionary.List is a sequence, it can hold different objects. (no fixed type constraint )
Lists are mutable.Lists are constructed with Square brackets0 - "[]"We can use " : " to index string.
list1= [1, 'str', 1.2]
indexing:
2.3 Tuple: Tuples are constructed with Parentheses - '()' ImmutableExample usage of tuples, in days of week, dates in calendar.For Data integrity, tuples are used.
Tuples are faster compared to list. tup1 = (1,2,3) # tuple creationprint (tup1[:2]) # indexing tupleO/p:(1, 2)tuplebyte (binary type)
3) Sets:setfrozen set
Sets are unordered collection of unique elements.Built in data type which stores collections of data.Items in set are unordered, unchangeable, unique.Set items can be of any data type.s = set() # set creation
s.add(1) # adding elemt to set
print(s)
Output:{1}
set1= {'apple', 1, 2.3} # set creaiton
print(set1)Output:{2.3, 'apple', 1}
4 built-in data types in Python are List, Tuple Set and Dictionary.
List is a sequence, it can hold different objects. (no fixed type constraint )
Lists are mutable.
Lists are constructed with Square brackets0 - "[]"
We can use " : " to index string.
list1= [1, 'str', 1.2]
indexing:
tup1 = (1,2,3) # tuple creation
print (tup1[:2]) # indexing tuple
Sets are unordered collection of unique elements.
Built in data type which stores collections of data.
Items in set are unordered, unchangeable, unique.
Set items can be of any data type.
s = set() # set creation
s.add(1) # adding elemt to set
print(s)
Output:
{1}
set1= {'apple', 1, 2.3} # set creaiton
print(set1)Output:
{2.3, 'apple', 1}
set creation from list:
list1 = ['1', '2', '1','2','1','2']
set2 = set(list1)
print(set2)Output:{'2', '1'}
Access set items:We, can't access items in a set by referring index or key. But, we can loop over. set1= {'apple', 1, 2.3} # set creaiton
set1[0]O/p:TypeError: 'set' object is not subscriptable
set1= {'apple', 1, 2.3} # set creaitonfor i in set1: print(i)O/p:12.3apple
Add new item to set:set1= {1, 2.3, 'Bng'} # set creaiton
set1.add('new-item') #adding item to setprint(set1)O/p:{1, 2.3, 'new-item', 'Bng'}
Joining Sets:set1 = {'a', 'b', 'c'}set2 = {1, 2, 3}
unioned_set = set1.union(set2)print (unioned_set)O/p:{1, 2, 'a', 3, 'c', 'b'}
4)Mappings:Dictionary 2.5 Dictionaries:Dictionary is a mapping.Mappings are a collection of objects that are stored by a "key".Mapping store objects by their relative position.
Dictionary consists of a key with an associated value. Value can be any python objectdict1 = {1:'key1', 'key2': 'value2', 'key3': 'value3'}dict1O/p:{1: 'key1', 'key2': 'value2', 'key3': 'value3'}
Dictionary comprehension:dict_comprehension = {x:x**2 for x in range(5)}dict_comprehensionO/p:{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
list1 = ['1', '2', '1','2','1','2']
set2 = set(list1)
print(set2)Output:{'2', '1'}
Access set items:
We, can't access items in a set by referring index or key. But, we can loop over.
set1= {'apple', 1, 2.3} # set creaiton
set1[0]
O/p:
TypeError: 'set' object is not subscriptable
set1= {'apple', 1, 2.3} # set creaiton
for i in set1:
print(i)
O/p:
1
2.3
apple
Add new item to set:
set1= {1, 2.3, 'Bng'} # set creaiton
set1.add('new-item') #adding item to set
print(set1)
O/p:
{1, 2.3, 'new-item', 'Bng'}
Joining Sets:
set1 = {'a', 'b', 'c'}
set2 = {1, 2, 3}
unioned_set = set1.union(set2)
print (unioned_set)
O/p:
{1, 2, 'a', 3, 'c', 'b'}
4)Mappings:
Dictionary
2.5 Dictionaries:
Dictionary is a mapping.
Mappings are a collection of objects that are stored by a "key".
Mapping store objects by their relative position.
Dictionary consists of a key with an associated value. Value can be any python object
dict1 = {1:'key1', 'key2': 'value2', 'key3': 'value3'}
dict1
O/p:
{1: 'key1', 'key2': 'value2', 'key3': 'value3'}
dict_comprehension = {x:x**2 for x in range(5)}
dict_comprehension
O/p:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
5) Boolean:
Boolean can be defined by typing True/False without quotes
# Boolean Example
bool1 = True
bool2 = False
print (type(bool1))
O/p:
<class 'bool'>
Note: Mutability:
int, float, bool, str, tuple, unicode are immutable.
list, set, dict are mutable.
No comments:
Post a Comment