In [ ]:
# lists
# A linear representation of data is called list.
# a list could be homogenous or hetrogenous.
# array vs list
# array is multi dimension representation of data - numpy
# http://scipy.org/
In [5]:
my_fruits = ['apple','banana','custard','dates']
my_empty = []
my_empty1 = list()
In [6]:
print my_fruits,type(my_fruits)
print my_empty,type(my_empty)
print my_empty1,type(my_empty1)
In [ ]:
# lists are indexed values
# my_fruits = ['apple','banana','custard','dates']
# 0 1 2 3 # +ve index or left to right.
# -4 -3 -2 -1 # -ve index or right to left
In [7]:
print my_fruits[0]
In [8]:
print my_fruits[-1]
In [ ]:
# slicing
In [10]:
#for(i=0;i<=3;i+2)
print my_fruits[0:3] # zero till three
print my_fruits[0:3:2]
In [12]:
# you can modify the list
my_string="python"
print my_string[0]
my_string[0]="P"
In [14]:
print my_fruits[0]
my_fruits[0]='Apple'
In [15]:
print my_fruits
In [32]:
# list is an iterable
for value in my_fruits:
print value
In [19]:
# in operator
print my_fruits
print 'Apple' in my_fruits
print 'apple' in my_fruits
print 'p' in 'python'
In [21]:
# converting a list to a string and vice-versa.
# convert a string to a list.
my_string="python"
Lmy_string=list(my_string)
print Lmy_string
Lmy_string[0]='T'
print Lmy_string
In [30]:
# convert a list to a string.
delimiter=""
print help(delimiter.join)
In [31]:
print delimiter.join(Lmy_string)
new_string = delimiter.join(Lmy_string)
print new_string
In [33]:
# sentence
# convert a string to a list.
my_sentence = "today is friday"
Lmy_sentence = list(my_sentence)
print Lmy_sentence
In [41]:
my_sentence = "today is friday"
print help(my_string.split)
my_email="tuxfux"
print my_email.split()
In [35]:
Lmy_sentence = my_sentence.split()
In [38]:
print Lmy_sentence
Lmy_sentence[2]='Thursday'
print Lmy_sentence
In [48]:
# converting a list to a string.
#azhar=" "
#azhar.join(Lmy_sentence)
new_sentence=" ".join(Lmy_sentence)
print Lmy_sentence
print my_sentence
print new_sentence
In [49]:
# functions
print my_fruits
In [50]:
print dir(my_fruits)
In [51]:
# append
print help(my_fruits.append)
In [52]:
my_fruits.append('grapes')
print my_fruits
In [53]:
# extend
print help(my_fruits.extend)
In [54]:
my_fruits.extend(['jackfruits','kiwi','leechi'])
print my_fruits
In [55]:
# insert
print help(my_fruits.insert)
In [56]:
my_fruits.insert(3,'mango')
print my_fruits
In [57]:
# index
print help(my_fruits.index)
In [58]:
print my_fruits.index('mango')
In [61]:
my_fruits = ['Apple', 'banana', 'custard', 'mango', 'dates', 'grapes', 'mango','jackfruits', 'kiwi', 'leechi']
print my_fruits.index('mango')
print my_fruits.index('mango',4)
In [62]:
#count
print help(my_fruits.count)
In [64]:
print my_fruits.count('mango')
print my_fruits.count('gauva')
In [68]:
# pop
print help(my_fruits.pop)
print my_fruits.pop()
print my_fruits
print my_fruits.pop(1)
print my_fruits
In [69]:
print my_fruits.pop(10)
In [71]:
# remove
print help(my_fruits.remove)
print my_fruits.remove('mango')
print my_fruits
In [72]:
print my_fruits.remove('gauva')
In [76]:
# reverse
print help(my_fruits.reverse)
print my_fruits.reverse()
print my_fruits
In [77]:
# sort
print help(my_fruits.sort)
In [80]:
# reverse=False =>ascending
# reverse=True =>descending
print my_fruits.sort()
In [81]:
print my_fruits
In [2]:
# task1
days = ['yesterday','today','tomorrow','dayafter']
In [ ]:
# output 1:
# yesterday 9
# today 5
# tomorrow 8
# dayafter 8
# output 2:
# Yesterday
# TOday
# TOMorrow
# DAYAfter
In [3]:
for value in days:
print value,len(value)
In [6]:
days = ['yesterday','today','tomorrow','dayafter']
for value in days:
print value,days.index(value),value[days.index(value)]
In [7]:
for value in days:
print value,days.index(value),value[0:days.index(value)]
In [8]:
for value in days:
print value,days.index(value),value[0:days.index(value) + 1]
In [13]:
my_string = "python"
# pyt
print my_string[0:3]
print my_string[:3]
print my_string[3:6]
print my_string[3:]
print my_string[:3] + my_string[3:]
In [14]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [17]:
# soft copy and deepcopy
print help(id)
In [19]:
a = 10
print id(a)
print id(10)
b = a
print id(b)
print id(10)
In [21]:
b = 20
print id(b)
print id(20)
print a
print id(a)
In [ ]:
# lists
In [22]:
# soft copy
my_lista = [1,2,3]
my_listb = my_lista
print my_lista,id(my_lista)
print my_listb,id(my_listb)
In [23]:
# is
print my_lista is my_listb
In [24]:
my_listb[1] = "two"
print my_listb # [1,"two",3]
print my_lista # [1,2,3]
In [25]:
my_listb = ["one","two","three"]
print my_listb
print my_lista
In [28]:
# Deep copy
my_listc = ["a","b","c"]
# way 1
my_listd = my_listc[:]
print my_listd,id(my_listd)
print my_listc,id(my_listc)
print my_listc is my_listd
In [29]:
# way 2
import copy
print help(copy.deepcopy)
In [30]:
my_liste = copy.deepcopy(my_listc)
print my_listc,id(my_listc)
print my_liste,id(my_liste)
print my_listc is my_liste
In [31]:
my_listc[1] ="one"
print my_listc
print my_liste
In [ ]:
# list comprehension
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
In [39]:
# input -> 1,2,3,4,5,6,7,8,9,10
# output -> 2,4,6,8,10
output=[]
enter_numbers = raw_input("please enter the numbers:")
print enter_numbers,type(enter_numbers)
for num in enter_numbers.split(','):
if int(num) % 2 == 0:
output.append(num)
In [41]:
print output
print ",".join(output)
In [47]:
print 9 / 2 # quotient
print 9 % 2 # reminder
In [46]:
# usign list comprehensions
enter_numbers = raw_input("please enter the numbers:")
print ",".join([ num for num in enter_numbers.split(',') if int(num) % 2 == 0])
In [48]:
# example 2:
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
my_sentence = "today is friday"
print [ [value.upper(),value.lower(),value.capitalize()] for value in my_sentence.split()]
In [ ]: