.2 Sequence Data Types

[1]:
123
var = [1,2]
print(type(var))
print(var)
stdout
<class 'list'> [1, 2]
[4]:
12
var = [1,2,'a',1.2,['a','b']] # notice the last element is list type 
print(var)
stdout
[1, 2, 'a', 1.2, ['a', 'b']]
[5]:
12
var = [1,2,'a',1.2,['a','b']] 
print("First Index ",var[0]) # Accessing the first index of a list  
stdout
First Index 1
[6]:
1234
var = [1,2,'a','b'] # we will replace 'a' with 'hello'
print("Before Changing ",var)
var[2] = 'hello'
print("After Changing",var)
stdout
Before Changing [1, 2, 'a', 'b'] After Changing [1, 2, 'hello', 'b']
[7]:
123
var = (1,2,3)
print("Type is ",type(var))
print(var)
stdout
Type is <class 'tuple'> (1, 2, 3)
[8]:
12
var = (1,2,'a',1.2,('a','b')) # notice the last element is tuple type 
print(var)
stdout
(1, 2, 'a', 1.2, ('a', 'b'))
[9]:
12
var = (1,2,'a',1.2,('a','b')) 
print("First Index ",var[0]) # Accessing the first index of a tuple  
stdout
First Index 1
[10]:
12
var = (1,2,'a',1.2,('a','b')) # we will try to change the first index of this tuple
var[0] = 2 # notice that this throws an error
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-13e899f2c1fb> in <module> 1 var = (1,2,'a',1.2,('a','b')) # we will try to change the first index of this tuple ----> 2 var[0] = 2 TypeError: 'tuple' object does not support item assignment
[11]:
1234
var = (1,2,'a',1.2,('a','b')) # to change the first element of a tuple we have declared a new tuple altogether
print("old tuple -->",var)
var = (1.2,2,'a',1.2,('a','b'))
print("new tuple -->",var)
stdout
old tuple --> (1, 2, 'a', 1.2, ('a', 'b')) new tuple --> (1.2, 2, 'a', 1.2, ('a', 'b'))
[13]:
123
var = {1,2,3}
print(type(var))
print(var)
stdout
<class 'set'> {1, 2, 3}
[18]:
12
var = {1,2,'a'}
print(var)
stdout
{1, 2, 'a'}
[21]:
12
var = {1,2,1} # Declaring a set with duplicates 
print(var) # notice that only 1 is present only once
stdout
{1, 2}
[22]:
12
var = {'a':1}
print(type(var))
stdout
<class 'dict'>
[23]:
1
print(var['a']) # accessing the first element of the dict
stdout
1
[28]:
12
var = {'a':1,'b':2,4:'a'}
print(var)
stdout
{'a': 1, 'b': 2, 4: 'a'}
[29]:
12
var = {'a':1,'b':2,4:'a',[1]:'a'} #adding a list as a key will throw an error 
print(var)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-29-8e81735dba73> in <module> ----> 1 var = {'a':1,'b':2,4:'a',[1]:'a'} #adding a list as a key will throw an error 2 print(var) TypeError: unhashable type: 'list'
[30]:
12
var = {'a':1,'b':(1),4:'a',1:['a']} #but list and sets can be added as values
print(var)
stdout
{'a': 1, 'b': 1, 4: 'a', 1: ['a']}
[32]:
1234
var = {'a':1,'b':(1),4:'a',1:['a']}
print("Before Changing",var)
var['a']=2
print("After Changing",var)
stdout
Before Changing {'a': 1, 'b': 1, 4: 'a', 1: ['a']} After Changing {'a': 2, 'b': 1, 4: 'a', 1: ['a']}
[34]:
12
var = {'a':1,'a':2} # value for key a will be 2 here
var
Out[34]:
{'a': 2}
[37]:
123
var = {'a':1,'b':(1),4:'a',1:['a']}
print("Keys -->",var.keys())
print("Values -->",var.values())
stdout
Keys --> dict_keys(['a', 'b', 4, 1]) Values --> dict_values([1, 1, 'a', ['a']])
[42]:
1234
# Int to float
var_int = 1
print("Before float conversion ",var_int)
print("After float conversion ",float(var_int))
stdout
Before float conversion 1 After float conversion 1.0
[43]:
1234
# float to int
var_float = 1.9
print("Before int conversion ",var_float)
print("After int conversion ",int(var_float)) # notice that only integer part will be extracted
stdout
Before int conversion 1.9 After int conversion 1
[48]:
123
# String to Int
var_str = '1' # non numerci values here will throw an error
print(int(var_str))
stdout
1
[49]:
123
# str to float
var_str = '1'
print(float(var_str))
stdout
1.0
[51]:
12345
# Int to string & float to string
var_int = 1
print("integer to string -->",str(var_int))
var_float = 1.1
print("float to string -->",str(var_float))
stdout
integer to string --> 1 float to string --> 1.1
[52]:
12345
# Int or Float to boolean
var_int = 1
print("integer to Boolean -->",bool(var_int)) 
var_float = 1.1
print("float to Boolean -->",bool(var_float))
stdout
integer to Boolean --> True float to Boolean --> True
[60]:
12345
# tuple to list & list to tuple
var_list = [1,2,3,4,5]
print(tuple(var_list))
var_tuple = (1,2,3,4,5)
print(list(var_list))
stdout
(1, 2, 3, 4, 5) [1, 2, 3, 4, 5]
[64]:
12345
# String to list or tuple
var_str = "Hello World"
print("String to list",list(var_str))
print("String to tuple",tuple(var_str))
stdout
String to list ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] String to tuple ('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')
[ ]:
1
Previous
Start of course
Next
.1 Python Basic Data Types