Please enable JavaScript.
Coggle requires JavaScript to display documents.
COMPOUND DATA TYPES (LIST (Aliasing (ex:no=temp,print temp prints 10,2,3,4…
COMPOUND DATA TYPES
LIST
Basic structure
Ordered set of values called elements
Elements indexed by from 0
Creation & Accessing
accessing:print list_months[2]
<list_name>=[<val1,val2..valn>
Example:list_months=["jan","feb","mar"]
indexing:jan=[0],feb=[1],mar=[2]
Operations
"+" Eg: list_months+April appends April
"
": list_months
2 adds all items twice
List Slicing
ex: no=[1,2,3,4,5]
no[2:5] = [3,4,5]
no[3:] =[4,5]
List Methods
append(E),insert(index,E),remove(E),sort() etc
List functions
len(list),max(list),min(list),cmp(list)etc
list_loop
; for accessing list elements for loop is used
Mutability
Elements in the list can be changed
no[0]="10" prints 10,2,3,4,5
Aliasing
ex:no=temp,print temp prints 10,2,3,4,5
no and temp lists refers same memory location
change in temp affects no also
Cloning
cloned list refers different memory location
change in cloned list will not affect the original list
list deletion:
del<listname>[index]
Tuples
same as list but elements in tuple are immutable
elements accessed through index number
tuple operation
same as list,+ concatenation * repetition
tuple slicing: access the subset of elements
Elements in tuple
cannot
be deleted
Using del
entire
tuple can be
deleted
immutable element cannot be updated
list in tuple can be changed
Tuple Assignment
ex:a=10 b=20 c=30
a,b=b,a result: a=20 b=10
Functions
len(tuple),max(tuple),min(tuple)
syntax:<tuple_name>=(val1,val2,..valn)
val1=tuple_ name[0],val2=tuple_name[1]...
DICTIONARY
Immutable,{key,value}
Creation
method 1: dic1={1:'janani',2:'arun',3:'ajay'}
method 2: using dict() constructor ex: dic2=dict({1:'janani',2:'arun',3:'ajay'})
Deletion
del<dic_name>[key]
Update
Dictionary keys immutable values are mutable
Methods
: clear(),copy(),keys(),values()..
Functions
cmp(dic1,dic2),len(dic),str(dic)..
ADVANCED LIST PROCESSING
constructs list easily
create a list with the subset of elements from another list by condition
faster than for loop
Examples
a list with 0 to 9 without condition
x = [i for i in range (10)] => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a list with even nos between 0 to 20 with condition
EvenNos = [n for n in range (20) if n % 2 == 0] => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]