In [ ]:
# Lists
# A collection of elements - homongenous/hetrogenous collection of data.
# list and array are different - python
# list is a linear collection of data.
# Array is a n dimension representation of data. ( numpy)
# http://scipy.org/
# TODO : ADD THE LINK FOR THE SEMINARS
In [3]:
my_fruits = ['apple','banana','cherry','dates']
print my_fruits,type(my_fruits)
my_empty = []
print my_empty,type(my_empty)
my_emtpy = list()
print my_empty,type(my_empty)
In [4]:
# indexing
my_fruits = ['apple','banana','cherry','dates']
# 0 1 2 3 # +ve indexing or left to right
# -4 -3 -2 -1 # -ve indexing or right to left
print my_fruits[1]
In [7]:
# slicing
print my_fruits[0:3]
# for(i=0;i<=4;i+2)
print my_fruits[::2]
print my_fruits[1::2]
In [9]:
# in operator
print 'apple' in my_fruits
print 'gauva' in my_fruits
In [12]:
# task
absent = ['kumar','shalini','sandy']
for student in ['naren','sandy','kumar','shalini','sushma']:
if student in absent:
continue
#break
#pass
print "results of student - {}".format(student)
In [ ]:
# lists are mutable
# strings are immutable
In [14]:
print my_fruits
my_fruits[0]='Apple'
print my_fruits
In [15]:
# string
my_string = "python"
my_string[0]="P"
In [16]:
# lists are iterable.
for value in my_fruits:
print value
In [18]:
# converting a list to string and vice-versa
## convert a string to a list.
my_string="python"
Lmy_string = list(my_string)
print Lmy_string,type(Lmy_string)
In [19]:
# list to string
Lmy_string[0]='T'
print Lmy_string
In [23]:
delimiter=""
print help(delimiter.join)
print delimiter.join(Lmy_string)
In [25]:
# case - II
my_string = "Today is sunday"
Lmy_string=list(my_string)
print Lmy_string
In [26]:
print help(my_string.split)
In [29]:
Lmy_string = my_string.split()
print Lmy_string
Lmy_string[0]='Tomorrow'
print Lmy_string
In [30]:
# convert the list back to string
limiter=" "
print limiter.join(Lmy_string)
In [33]:
# Function
print my_fruits
print dir(my_fruits)
print help(dir)
In [34]:
# append
print help(my_fruits.append)
In [35]:
print my_fruits.append('figs')
print my_fruits
In [36]:
# extend
print help(my_fruits.extend)
In [38]:
my_fruits.extend(['gauva','jackfruit','kiwi'])
print my_fruits
In [39]:
# index
print help(my_fruits.index)
In [40]:
my_fruits.index('gauva')
Out[40]:
In [41]:
# insert
print help(my_fruits.insert)
In [42]:
my_fruits.insert(5,'grapes')
print my_fruits
In [43]:
# count
print help(my_fruits.count)
In [44]:
print my_fruits.count('gauva')
In [45]:
my_fruits.append('gauva')
print my_fruits
print my_fruits.count('gauva')
In [46]:
# pop
print help(my_fruits.pop)
In [47]:
print my_fruits.pop(my_fruits.index('jackfruit'))
In [48]:
print my_fruits
In [49]:
print my_fruits.pop()
In [50]:
print my_fruits
In [51]:
print my_fruits.pop(20)
In [52]:
# remove
print help(my_fruits.remove)
In [53]:
print my_fruits.remove('grapes')
print my_fruits
In [54]:
print my_fruits.remove('grapes')
In [55]:
# reverse
print help(my_fruits.reverse)
print my_fruits.reverse()
print my_fruits
In [56]:
# sort
print help(my_fruits.sort)
In [57]:
print my_fruits.sort(reverse=True)
print my_fruits
In [58]:
print my_fruits.sort()
print my_fruits
In [18]:
# task1
days = ['yesterday','today','tomorrow','dayafter']
# part1
# len
for value in days:
print value,len(value)
In [25]:
# part 2
for value in days:
print value,days.index(value),value[days.index(value)],value[:days.index(value) + 1]
In [1]:
my_string="python"
print my_string[:3]
print my_string[3:]
print my_string[:3] + my_string[3:]
In [28]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [22]:
# task2
my_fruits = ['apple','banana','apple','banana','cherry']
duplicate=[]
for value in my_fruits:
if my_fruits.count(value) > 1:
if value in duplicate:
pass
else:
duplicate.append(value)
my_fruits.remove(value)
print my_fruits,duplicate
In [6]:
# task3
my_even = []
num = raw_input("please enter a number")
print num,type(num)
Lmy_num = num.split(',')
print Lmy_num
for value in Lmy_num:
if int(value) % 2 == 0:
my_even.append(value)
print ",".join(my_even)
In [10]:
# list comprehnesions
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
# [ print ; condition ; statement ]
num = raw_input("please enter a number:")
print ",".join([ value for value in num.split(',') if int(value) % 2 == 0])
In [12]:
#
my_string = "today is sunday"
# output
# capitalize,upper,lower
print [[value.capitalize(),value.upper(),value.lower()] for value in my_string.split()]
In [1]:
# memory allocation in list
a = 10
print help(id)
In [2]:
print id(a)
print id(10)
In [3]:
b = a
print a,b
print id(a),id(b),id(10)
In [4]:
# is
print b is a
In [5]:
b = 20
print a,b
print id(a),id(b),id(10),id(20)
In [7]:
# lists
# soft and deepcopy
# softcopy : both your labels(La,Lb) are refering to same memory block.
La = ["one","two","three"]
print La,id(La)
Lb = La
print Lb,id(Lb)
print La is Lb
In [8]:
La[1] = "2"
print La
print Lb
In [9]:
La = [5,6,7]
print La
print Lb
In [10]:
# deep copy
C = [1,2,3]
import copy
print help(copy.deepcopy)
In [11]:
D = copy.deepcopy(C)
print D,C,id(D),id(C)
print C is D
In [12]:
E = C[:]
print E,C,id(E),id(C)
print E is C
In [ ]: