In [ ]:
# lists
# A list is a collection of elements.
# List is a collection of hetrogenous elements.
# List - Linear collection of data.
# arrays - Multidimension collection of data.
# ex: latitude and logitude.
# http://scipy.org
In [1]:
# lists
my_fruits = ['apple','banana','cherry','dates','fig'] # homogenous
In [2]:
print my_fruits,type(my_fruits)
In [3]:
my_empty = []
print my_empty,type(my_empty)
In [4]:
my_empty = list()
print my_empty,type(my_empty)
In [5]:
# in operation
In [6]:
print 'apple' in my_fruits
In [7]:
print 'Apple' in my_fruits
In [11]:
# example
absent = ['kumar','anu','varun','deepti']
for student in ['shravya','anu','deepti','kumar','naimisha','varun','mani']:
if student in absent:
continue
#break
#pass
print "results of student - {}".format(student)
In [12]:
# lists support indendation
# lists support slicing
In [14]:
print my_fruits[0]
# lists are mutable - you can modify.
my_fruits[0] = 'Apple'
print my_fruits
In [15]:
my_string = "apple"
print my_string[0]
In [16]:
# string is immutable - you cannot modify.
my_string[0]='A'
In [17]:
# Iteration
In [18]:
for fruit in my_fruits:
print fruit
In [19]:
# functions
In [20]:
print dir(my_fruits)
In [21]:
# my_fruits.append
print help(my_fruits.append)
In [22]:
print my_fruits.append('grapes')
In [23]:
my_fruits.append('guava')
In [24]:
print my_fruits
In [25]:
# my_fruits.extend
print help(my_fruits.extend)
In [26]:
my_fruits.extend(['jackfruit','kiwi','leechi'])
In [27]:
print my_fruits
In [28]:
# my_fruits.insert
print help(my_fruits.insert)
In [29]:
my_fruits.insert(1,'apricot')
In [30]:
print my_fruits
In [31]:
my_fruits.insert(3,'grapes')
In [34]:
print my_fruits
In [33]:
# my_fruits.index
print help(my_fruits.index)
In [35]:
print my_fruits.index('grapes')
In [37]:
print my_fruits.index('grapes',4)
print my_fruits.index('grapes',4,8)
In [38]:
# my_fruits.count
print help(my_fruits.count)
In [39]:
print my_fruits.count('grapes') #2
print my_fruits.count('apple') #0
In [40]:
# my_fruits.pop
print help(my_fruits.pop)
In [42]:
# task : 1 want to remove the first grapes
my_fruits.pop(my_fruits.index('grapes')) # removes only one time
print my_fruits
In [43]:
my_fruits.pop(20)
In [44]:
# my_fruits.remove
print help(my_fruits.remove)
In [45]:
my_fruits.remove('Apple')
In [46]:
print my_fruits
In [47]:
my_fruits.remove('Apple')
In [48]:
# my_fruits.reverse
print help(my_fruits.reverse)
In [49]:
my_fruits.reverse()
In [50]:
print my_fruits
In [51]:
# my_fruits.sort
print help(my_fruits.sort)
In [52]:
print my_fruits.sort()
In [53]:
print my_fruits
In [56]:
# tasks
# 5
# a
days = ['yesterday','today','tomorrow','dayafter']
print len(days)
In [57]:
for day in days:
print day,len(day)
In [58]:
# 5b
# task
for value in days:
print value,days.index(value)
In [59]:
for value in days:
print value,value[days.index(value)]
In [61]:
for value in days:
print value,value[0:days.index(value) + 1]
In [62]:
for value in days:
print value[:days.index(value) + 1].upper()
In [66]:
my_string="python"
print my_string[0:3]
print my_string[:3]
print my_string[3:]
print my_string[:3] + my_string[3:]
In [67]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [68]:
# task 6
my_fruits = ['apple','banana','apple','banana','cherry']
In [70]:
my_duplicate = []
for value in my_fruits:
if my_fruits.count(value) > 1:
my_duplicate.append(value)
my_fruits.remove(value)
print my_duplicate
print my_fruits
In [1]:
# memory - lists
# soft copy and deep copy
In [2]:
a = 1
In [3]:
print a
In [4]:
print help(id)
In [5]:
print id(1)
print id(a)
In [6]:
b = 1
In [12]:
print b,id(b)
In [8]:
b = 2
In [9]:
print b,id(b)
In [10]:
# is
In [11]:
print 'apple' in ['apple','banana','cherry','dates']
In [13]:
print a is b
In [14]:
c = 1
In [15]:
print id(1),id(a),id(c)
In [16]:
print c is a
In [17]:
# lists
In [ ]:
# soft copy
In [18]:
a = [1,2,3]
print a,id(a)
In [19]:
b = a
print b,id(b)
In [20]:
print a is b
In [21]:
print b[1]
In [22]:
b[1]="two"
In [23]:
print a,id(a) # [1,two,3]
print b,id(b) # [1,2,3] or [1,two,3]
In [24]:
a = [1,2,3]
print a,id(a) # [1,2,3]
print b,id(b) # [1,2,3] or [1,two,3]
In [25]:
# deep copy
In [26]:
c = ['one','two','three']
# deepcopy
d = c[:]
print c,id(c)
print d,id(d)
In [27]:
print c is d
In [28]:
# copy
import copy
In [29]:
e = copy.deepcopy(c)
print c,id(c)
print e,id(e)
In [30]:
print e is c
In [31]:
# sys.argv
In [33]:
# look at the example. -
# look at add.py
In [ ]:
# convert from list to string
# string to list and vice-versa
In [39]:
# converting string to list.
my_string = "python"
Lmy_string = list(my_string)
print Lmy_string
In [40]:
Lmy_string[0]='T'
print Lmy_string
In [41]:
# converting a list to string
In [46]:
limiter = ''
print help(limiter.join)
In [47]:
print limiter.join(Lmy_string)
In [48]:
# ex: useradd kumar
# tail /etc/passwd
my_user = ['kumar','x','512','512','user kumar','/home/kumar','/bin/bash']
limiter = ':'
print limiter.join(my_user)
In [49]:
# converting a sentence to a list
my_sentence = "today is sunday"
In [50]:
Lmy_sentence = list(my_sentence)
print Lmy_sentence
In [52]:
print help(my_sentence.split)
Lmy_sentence = my_sentence.split()
print Lmy_sentence
In [53]:
Lmy_sentence[0] = 'yesterday'
print Lmy_sentence
In [54]:
delimiter = " "
print delimiter.join(Lmy_sentence)
In [34]:
# list comprehensions
# building list on fly.
In [35]:
num = raw_input("please enter a number:")
print num
In [55]:
print type(num),num
In [56]:
# output - 2,4,6,8,10
In [59]:
my_output = []
for value in num.split(','):
if int(value) % 2 == 0:
my_output.append(value)
print ','.join(my_output)
In [63]:
# list comprehension
#num = raw_input("please enter a number:")
# [output;loop;condtion]
print ','.join([ value for value in num.split(',') if int(value) % 2 == 0])
In [64]:
# few more example on list comprehesion.
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
In [ ]: