In [ ]:
# A list is a linear representation of data.
# A indexed representation of your data.
# list vs array(multi-dimesion)
# http://scipy.org/
In [1]:
# creation of a list
my_fruits = ['apple','banana','custard','dates']
print my_fruits
print type(my_fruits)
empty_list = list()
print empty_list
print type(empty_list)
empty_list1 = []
print empty_list1
print type(empty_list1)
In [4]:
# in operation
print 'apple' in my_fruits
print 'Apple' in my_fruits
In [5]:
absent=['kumar','swapnil','shiva']
for student in ('shiva','srikant','sunil','kumar','tarun','swapnil','anunash'):
if student in absent:
continue # print all except kumar
#break # print shiva,srikant,sunil
#pass # print everything
print "results for the student:{}".format(student)
In [6]:
# we can modify the elements of a list.
print my_fruits
In [16]:
# lists are iterable
for value in my_fruits:
print value
In [7]:
# your lists are index values
# ['apple', 'banana', 'custard', 'dates']
# 0 1 2 3
my_fruits[0]='Apple'
In [8]:
print my_fruits
In [10]:
my_string="python"
# p y t h o n
# 0 1 2 3 4 5
print my_string[0]
my_string[0]="P"
In [11]:
# converting string to a list and vice-versa
my_string="python"
print my_string,type(my_string)
In [12]:
# converting a string to a list
Lmy_string=list(my_string)
In [13]:
print Lmy_string
In [14]:
Lmy_string[0]='J'
print Lmy_string
In [15]:
# conver the list back to a string
print help(my_string.join)
In [30]:
new_string = "".join(Lmy_string)
print my_string,new_string
In [22]:
# useradd kumar
userdetails=['kumar','x','512','512','username kumar','/home/kumar','/bin/bash']
# make a entry in /etc/passwd files
print ':'.join(userdetails)
In [23]:
# converting a string to a list.
# take a sequence of words.
my_sentence="today is saturday"
In [25]:
# list doesnot always work for words. It splits letter wise.
Lmy_sentence = list(my_sentence)
print Lmy_sentence
In [26]:
print help(my_sentence.split)
In [28]:
Lmy_sentence = my_sentence.split()
print Lmy_sentence
In [29]:
# modify the list
Lmy_sentence[0] ="Tomorrow"
Lmy_sentence[2] ="sunday."
print Lmy_sentence
In [31]:
# converting a list to a string.
new_sentence=" ".join(Lmy_sentence)
print new_sentence
In [32]:
# Function
print my_fruits
In [33]:
print dir(my_fruits)
In [34]:
# append
print help(my_fruits.append)
In [35]:
my_fruits.append('gauva')
print my_fruits
In [36]:
# extend
print help(my_fruits.extend)
In [37]:
my_fruits.extend(['jackfruit','kiwi','leechi'])
In [38]:
print my_fruits
In [40]:
# insert
print help(my_fruits.insert)
In [41]:
my_fruits.insert(4,'grapes')
In [42]:
print my_fruits
In [43]:
# index
print help(my_fruits.index)
In [44]:
print my_fruits.index('grapes') # 4
print my_fruits.index('custard') # 2
In [45]:
my_fruits.insert(4,'grapes')
print my_fruits
In [47]:
print my_fruits.index('grapes',5)
In [48]:
# pop
print help(my_fruits.pop)
In [50]:
print my_fruits.pop() # leechi
In [51]:
print my_fruits
In [52]:
print my_fruits.pop(4)
print my_fruits
In [53]:
print my_fruits.pop(10)
In [55]:
# remove
print help(my_fruits.remove)
print my_fruits
In [56]:
my_fruits.remove('grapes')
In [57]:
print my_fruits
In [58]:
my_fruits.remove('grapes')
In [61]:
# reverse
print help(my_fruits.reverse)
In [62]:
print my_fruits
print my_fruits.reverse()
print my_fruits
In [63]:
# sort
print help(my_fruits.sort)
In [64]:
my_fruits.sort() # ascending order
In [65]:
print my_fruits
In [67]:
my_fruits.sort(reverse=True) # descending order
print my_fruits
Task1 days = ['yesteday','today','tomorrow','dayafter']
yesterday 9 today 5 tomorrow 8 dayafter 8
Task2: Yesterday TOday TOMorrow DAYAafter
In [69]:
days = ['yesterday','today','tomorrow','dayafter']
for value in days:
print value,len(value)
In [70]:
days = ['yesterday','today','tomorrow','dayafter']
for value in days:
print value
In [73]:
for value in days:
print value,days.index(value),value[days.index(value)]
In [80]:
for value in days:
print value,days.index(value),value[0:days.index(value)]
In [81]:
for value in days:
print value,days.index(value),value[0:days.index(value) + 1]
In [79]:
my_string="python"
print my_string[:3]
print my_string[3:]
print my_string[:3] + my_string[3:]
print my_string[0]
print my_string[0:0] #[start:end]
In [76]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [82]:
# aliasing
a = 1
In [83]:
print help(id)
In [85]:
print id(1),id(a)
In [86]:
b = 1
In [87]:
print id(b),id(1)
In [88]:
b = 2
In [90]:
print id(a),id(b)
In [91]:
print id(1),id(2)
In [2]:
# softcopy,deepcopy,shallowcopy
In [4]:
# softcopy
a = [1,2,3]
print id(a)
print id([1,2,3])
print id(a[0])
print id(a[1])
print id(a[2])
In [5]:
# copy a list
b = a
print id(b)
print id(a)
In [8]:
# is
print a is b
print a
print b
In [9]:
# softcopy
print b[1]
b[1] = "two"
In [10]:
print b # 1,two,3
print a # 1,2,3 -> 1,two,3
In [11]:
b = ['one','two','three']
In [12]:
print a
print b
In [13]:
# deepcopy
a = [1,2,3]
print id(a)
In [14]:
# use case 1:
b = a[:]
print a,b
print id(a),id(b)
print a is b
In [16]:
b[1] ='two'
print a
print b
In [17]:
# use case 2:
import copy
print dir(copy)
In [19]:
c = copy.deepcopy(a)
print a
print c
print id(a),id(c)
print a is c
In [20]:
# list comprehension
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
In [ ]:
# input -> '1','2','3','4','5','6','7','8','9','10'
# ouput -> '2','4','6','8','10'
In [31]:
values = raw_input("please enter the numbers:")
even =[]
print values,type(values)
# parse via values one by one,convert to integers,save even in list,later convert list to string
for value in values.split(','):
if int(value) % 2 == 0: # % looks for a reminder , / looks for a quotient
even.append(value)
print even
In [32]:
print ",".join(even)
In [35]:
# list comprenesion
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
values = raw_input("please enter the numbers:")
print ",".join([ value for value in values.split(',') if int(value) % 2 == 0])
In [ ]: