In [ ]:
# what is a list?
# List is a collection of data.
# Lists can store both homogenous and hetrogenous data.
# list vs array
# array : http://tinyurl.com/TconSeminars
In [2]:
# lists are indexed.
my_fruits = ['apple','banana','cherry','dates']
print my_fruits,type(my_fruits)
In [3]:
my_empty = list()
print my_empty,type(my_empty)
In [4]:
my_empty = []
print my_empty,type(my_empty)
In [5]:
# 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[0]
In [6]:
# slicing
print my_fruits[0:3]
In [7]:
# lists are mutable. - you can modify the lists.
my_string = "python"
print my_string[0]
my_string[0] = "P"
In [9]:
print my_fruits
print my_fruits[0]
my_fruits[0]='Apple'
print my_fruits
In [11]:
# in
print 'Apple' in my_fruits
print 'orange' in my_fruits
In [14]:
# example on in
#!/usr/bin/python
# continue : its skips an iteration.
absent = ['sunil','raj','madhuri','kumar']
for student in ['rajni','madhuri','priya','kumar','sunil','raj','praveen']:
if student in absent:
continue
#break
#pass
print "results for the student - {}".format(student)
In [16]:
# converting string to list and vice-versa.
# case I
# converted a string to list.
my_string="python"
Lmy_string = list(my_string)
print my_string,Lmy_string
In [17]:
Lmy_string[0]='T'
print Lmy_string
In [21]:
# we have a list and need to convert it to string
# you converted a list to string.
delimiter=""
print help(delimiter.join)
print delimiter.join(Lmy_string)
In [23]:
# case II
my_sentence="Today is friday"
print list(my_sentence)
print help(my_sentence.split)
In [25]:
Lmy_string = my_sentence.split()
print Lmy_string
Lmy_string[2]='Thursday'
print Lmy_string
In [28]:
# convert a list to string
delimiter=" "
print delimiter.join(Lmy_string)
In [5]:
# Memory of lists in python.
# soft copy,deep copy,shallow copy
a = 1
print help(id)
print a,id(1),id(a)
b = a
print b,id(b)
# is
print a is b
# lets modify the value of b
b = 3
print b,id(b),id(3)
print a,id(a)
In [11]:
# lists
my_list = [1,2,3]
print id(my_list),my_list
my_list1 = my_list
print id(my_list1),my_list1
# soft copy: both the objects linked to same memory block
my_list[1] = "two"
print my_list # [1,'two',3]
print my_list1 # [1,2,3]
my_list = ["one","two","three"]
print my_list
print my_list1
print id(my_list),id(my_list1)
In [17]:
# deep copy
my_list = [1,2,3]
print my_list,id(my_list)
my_list1 = my_list[:]
print my_list1,id(my_list1)
my_list[1]="two"
print my_list
print my_list1
# deep copy
import copy
print dir(copy)
my_list2 = copy.deepcopy(my_list)
print my_list2,id(my_list2)
print my_list,id(my_list)
# is
print my_list is my_list2
In [18]:
# my_functions
my_fruits = ['apple','banana','cherry','dates']
print dir(my_fruits)
In [20]:
# append
print help(my_fruits.append)
my_fruits.append('fig')
print my_fruits
In [22]:
# extend
print help(my_fruits.extend)
my_fruits.extend(['grapes','jackfruit','kiwi'])
print my_fruits
In [26]:
# index
print help(my_fruits.index)
print my_fruits.index('grapes')
In [27]:
# insert
print help(my_fruits.insert)
my_fruits.insert(5,'gauva')
print my_fruits
In [29]:
# pop
print help(my_fruits.pop)
print my_fruits.pop()
print my_fruits
In [30]:
# remove
print help(my_fruits.remove)
print my_fruits.remove('dates')
print my_fruits
In [31]:
# count
print help(my_fruits.count)
print my_fruits.count('cherry')
print my_fruits
In [32]:
# reverse
print help(my_fruits.reverse)
print my_fruits
print my_fruits.reverse()
print my_fruits
In [36]:
# sort
print help(my_fruits.sort)
print my_fruits.sort()
print my_fruits
print my_fruits.sort(reverse=True)
print my_fruits
In [37]:
my_days = ['yesterday','today','tomorrow','dayafter']
for value in my_days:
print value,len(value)
In [42]:
# task2
for value in my_days:
print value,my_days.index(value),value[my_days.index(value)],value[:my_days.index(value) + 1]
In [44]:
my_string="python"
print my_string[:3]
print my_string[3:]
print my_string[:3] + my_string[3:]
In [43]:
for value in my_days:
print value[:my_days.index(value) + 1].upper() + value[my_days.index(value) + 1:]
In [ ]:
# List comprehensions
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
# https://github.com/tuxfux-hlp/Python-examples
In [1]:
# without list comprehnesions
# 1,2,3,4,5,6,7,8,9,10
# 2,4,6,8,10
num = raw_input("please enter a number:")
print num,type(num)
In [2]:
Lnum = num.split(',')
print Lnum,type(Lnum)
In [3]:
Leven = []
for value in Lnum:
if int(value) % 2 == 0:
Leven.append(value)
In [5]:
print Leven
print ",".join(Leven)
In [9]:
# list comprehensions
# [print;condition;statements]
# [3,1,2]
print ",".join([ value for value in num.split(',') if int(value) % 2 == 0])
In [11]:
# example 2
my_string="today is saturday"
[ [value.upper(),value.capitalize(),value.lower()] for value in my_string.split()]
Out[11]:
In [ ]: