In [ ]:
# Lists
# List is a linear representation of hetrogeneous data .
# Arrays(numpy) and Lists are not synonymous.
# https://scipy.org/
In [1]:
my_fruits = ["Apple","banana","cherry","dates"]
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]:
# list - iterator
for value in my_fruits:
print value
In [ ]:
# list support - indexing,slicing.
In [6]:
print my_fruits[3]
print my_fruits[-1]
In [7]:
# cherry
print my_fruits[2][2]
In [8]:
# slicing
print my_fruits[1:3]
In [ ]:
# in
In [10]:
print 'apple' in my_fruits
print 'Apple' in my_fruits
In [11]:
# list are mutable.
print my_fruits
In [12]:
my_fruits[1]="Banana"
print my_fruits
In [13]:
# string cannot be modified - immutable.
my_string="banana"
my_string[0]="B"
In [ ]:
# converting a list to string and vice-versa
In [14]:
# string
my_string="python"
# convering a string to a list
Lmy_string = list(my_string)
print Lmy_string
In [15]:
# modified list.
Lmy_string[0]="T"
print Lmy_string
In [26]:
# convert the list back to string
limiter=""
print help(limiter.join)
In [28]:
print limiter.join(Lmy_string)
print ",".join(Lmy_string)
In [31]:
# my_sentence
my_sentence = "Today is thursday"
print my_sentence
In [33]:
Lmy_sentence = list(my_sentence)
print Lmy_sentence
In [34]:
# split
print help(my_sentence.split)
In [35]:
Lmy_sentence = my_sentence.split()
print Lmy_sentence
In [37]:
Lmy_sentence[2]="Friday"
print Lmy_sentence
In [38]:
# revert it back to a string
limiter=" "
print limiter.join(Lmy_sentence)
In [40]:
#!/usr/bin/python
# continue: you can skip an iteration
absent = ['chitu','ravi','law','akshit','kumar']
for student in ['chitu','soumi','ravi','kumar','law','aditya','akshit','krishna']:
if student in absent:
continue
#break
#pass
print "results of student - {} ".format(student)
In [43]:
# i want to find out the students who are absent and also there in studnet list.
absent = ['chitu','ravi','law','akshit','kumar']
student = ['chitu','soumi','ravi','law','aditya','akshit','krishna']
for stu in absent:
if stu not in student:
continue
print "name of the student who is absent - {}".format(stu)
In [ ]:
# Assignements
# https://github.com/sambapython/python/blob/master/assignments.txt
# reverse a list -wihout a function.
l=[1,2,3,5,7,8,9,10,11,12,13,20,22,23,24,25,26,27,20,21,22,4] output = [[1, 2, 3], [5], [7, 8, 9, 10, 11, 12, 13], [20], [22, 23, 24, 25, 26, 27], [20, 21, 22], [4]]
In [ ]:
# functions
In [1]:
my_fruits = ['apple','banana','cherry','dates']
In [2]:
print dir(my_fruits)
In [3]:
# append
print help(my_fruits.append)
In [4]:
my_fruits.append('fig')
In [5]:
print my_fruits
In [6]:
# extend
print help(my_fruits.extend)
In [7]:
my_fruits.extend(['grapes','jackfruit','kiwi'])
In [8]:
print my_fruits
In [10]:
# insert
print help(my_fruits.insert)
In [11]:
my_fruits.insert(6,'guava')
In [12]:
print my_fruits
In [13]:
# index
print help(my_fruits.index)
In [14]:
print my_fruits.index('guava')
In [17]:
my_fruits.insert(0,'guava')
print my_fruits
print my_fruits.index('guava') # first guava
print my_fruits.index('guava',1)
In [19]:
# count
print help(my_fruits.count)
In [20]:
print my_fruits.count('guava')
In [21]:
# pop
print help(my_fruits.pop)
In [22]:
print my_fruits
In [23]:
my_fruits.pop(0)
Out[23]:
In [24]:
print my_fruits
In [25]:
print my_fruits.pop(10)
In [26]:
print my_fruits.pop(10)
In [27]:
# remove
print help(my_fruits.remove)
In [28]:
print my_fruits.remove('guava')
In [29]:
print my_fruits
In [31]:
# sort
print help(my_fruits.sort)
my_fruits.sort()
In [32]:
print my_fruits
In [33]:
print my_fruits.sort(reverse=True)
In [34]:
print my_fruits
In [35]:
# reverse
print help(my_fruits.reverse)
print my_fruits.reverse()
In [36]:
print my_fruits
In [38]:
# task
days = ['yesterday','today','tomorrow','dayafter']
# task1
#
# yesterday 9
# today 5
# tomorrow 8
# dayafter 8
# task2
# Yesterday
# TOday
# TOMorrow
# DAYAfter
In [39]:
for value in days:
print value,len(value)
In [42]:
for value in days:
print value,days.index(value),value[days.index(value)]
In [44]:
for value in days:
print value,days.index(value),value[0:days.index(value) + 1]
In [48]:
for value in days:
print value[:days.index(value) + 1].upper()
In [47]:
my_string="python"
print my_string[0:3] # pyt
print my_string[:3] # pyt
print my_string[3:6] # hon
print my_string[3:]
print my_string[:3] + my_string[3:]
In [49]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [ ]:
# sys.argv
In [ ]:
# List Comprehensions
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
# example1
In [53]:
square=[]
multiples = []
for value in range(1,100):
square.append(value*value)
for value in square:
if value % 2 == 0 and value % 3 == 0:
multiples.append(value)
print multiples
In [57]:
# list comprehension
# [value looping condition]
print [ value for value in [ value*value for value in range(1,100) ] if value % 2 == 0 and value % 3 == 0 ]
In [63]:
# example2
# 2,4,6,8,10
num = raw_input("please enter a number:")
print num,type(num)
print ",".join([value for value in num.split(',') if int(value) % 2 == 0])
In [ ]:
# softcopy and deepcopy
In [1]:
a = 1
In [3]:
print help(id)
In [2]:
print id(1),id(a)
In [4]:
b = 1
In [5]:
print id(b),id(1)
In [6]:
# is
print a is b
In [9]:
# in
print b in [1,2]
In [10]:
b = 2
In [11]:
print b
In [12]:
print id(1),id(2),id(a),id(b)
In [13]:
# lists
In [ ]:
# two lists sharing similar memory blocks and both get modified when we try to modify one element
# this features is called as soft copy.
In [14]:
weeks = ['sun','mon','tue']
In [15]:
print id(weeks),id(weeks[0]),id(weeks[1]),id(weeks[2])
In [16]:
weeks1 = weeks
In [19]:
print weeks1
print id(weeks1),id(weeks1[0]),id(weeks1[1]),id(weeks1[2])
In [20]:
print weeks is weeks1
In [22]:
print weeks[2]
weeks[2] = "wed"
print weeks1 #["sun","mon","tue"]
print weeks #["sun","mon","wed"]
In [23]:
# use case
weeks = ['thu','fri','sat']
print weeks
print weeks1
In [26]:
# deep copy
a = [1,2,3]
print a,id(a),id(a[0]),id(a[1]),id(a[2])
In [25]:
# case i
b = a[:]
print b,id(b),id(b[0]),id(b[1]),id(b[2])
In [28]:
b[1]="two"
print b
print a
In [30]:
# copy
import copy
print dir(copy)
In [31]:
c = copy.deepcopy(a)
print c,a
print id(c),id(a)
In [ ]:
#
In [ ]: