In [ ]:
# what is a list ?
# A list is a sequential representation of data in linear form.
# A list can hold both homogenous or hetrogenous data.
# array: A collection of similar(homogenous) datatypes.(2,3, or n dimension)
# https://scipy.org/

In [2]:
# creating a list.
my_fruits = ['apple','banana','cherry','dates']
print my_fruits
print type(my_fruits)


['apple', 'banana', 'cherry', 'dates']
<type 'list'>

In [3]:
# using empty []
empty_list = []
print empty_list
print type(empty_list)


[]
<type 'list'>

In [4]:
# using list()
empty_list = list()
print empty_list
print type(empty_list)


[]
<type 'list'>

In [6]:
# Lists are indexed values.
# my_fruits = ['apple','banana','cherry','dates']
#               0        1         2        3     # +ve indexed or left to right
#               -4       -3        -2       -1    # -ve indexed or right to left
print my_fruits[0]
print my_fruits[-3]


apple
banana

In [9]:
# slicing
print my_fruits[0:2]
print my_fruits[:2]
print my_fruits[2:]


['apple', 'banana']
['apple', 'banana']
['cherry', 'dates']

In [10]:
# list elements are mutable.(you can modify them)
print my_fruits[0] # apple
my_fruits[0] = 'Apple'
print my_fruits[0]


apple
Apple

In [11]:
print my_fruits


['Apple', 'banana', 'cherry', 'dates']

In [13]:
my_string="python"
print my_string[0]
my_string[0]="T"


p
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-25dec20c4c74> in <module>()
      1 my_string="python"
      2 print my_string[0]
----> 3 my_string[0]="T"

TypeError: 'str' object does not support item assignment

In [18]:
# you can iterate over a list
for value in my_fruits:
    print value


Apple
banana
cherry
dates

In [14]:
# in operator
print 'apple' in my_fruits
print 'banana' in my_fruits


False
True

In [17]:
# gang of absentees
absentees = ['kumar','rahul','khiri']
for student in ['sravanti','thakur','kumar','ramya','khiri','rahul']:
    if student in absentees:
        continue
        #break
        #pass
    print "results of - {}".format(student)


results of - sravanti
results of - thakur
results of - ramya

In [19]:
# Functions
print my_fruits


['Apple', 'banana', 'cherry', 'dates']

In [20]:
print dir(my_fruits)


['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

In [21]:
# append
print help(my_fruits.append)


Help on built-in function append:

append(...)
    L.append(object) -- append object to end

None

In [22]:
my_fruits.append('fig')

In [23]:
print my_fruits


['Apple', 'banana', 'cherry', 'dates', 'fig']

In [24]:
# extend
print help(my_fruits.extend)


Help on built-in function extend:

extend(...)
    L.extend(iterable) -- extend list by appending elements from the iterable

None

In [25]:
my_fruits.extend(['grapes','jackfruit','kiwi'])
print my_fruits


['Apple', 'banana', 'cherry', 'dates', 'fig', 'grapes', 'jackfruit', 'kiwi']

In [26]:
# insert
print help(my_fruits.insert)


Help on built-in function insert:

insert(...)
    L.insert(index, object) -- insert object before index

None

In [28]:
my_fruits.insert(6,'gauva')

In [29]:
print my_fruits


['Apple', 'banana', 'cherry', 'dates', 'fig', 'grapes', 'gauva', 'jackfruit', 'kiwi']

In [30]:
my_fruits.insert(3,'gauva')

In [31]:
print my_fruits


['Apple', 'banana', 'cherry', 'gauva', 'dates', 'fig', 'grapes', 'gauva', 'jackfruit', 'kiwi']

In [32]:
# index
print help(my_fruits.index)


Help on built-in function index:

index(...)
    L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.

None

In [33]:
print my_fruits.index('gauva')


3

In [34]:
print my_fruits.index('gauva',4)


7

In [35]:
# count
print help(my_fruits.count)


Help on built-in function count:

count(...)
    L.count(value) -> integer -- return number of occurrences of value

None

In [37]:
print my_fruits.count('apple')
print my_fruits.count('gauva')


0
2

In [38]:
# POP
print help(my_fruits.pop)


Help on built-in function pop:

pop(...)
    L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

None

In [39]:
print my_fruits


['Apple', 'banana', 'cherry', 'gauva', 'dates', 'fig', 'grapes', 'gauva', 'jackfruit', 'kiwi']

In [40]:
print my_fruits.pop(my_fruits.index('dates'))


dates

In [41]:
print my_fruits


['Apple', 'banana', 'cherry', 'gauva', 'fig', 'grapes', 'gauva', 'jackfruit', 'kiwi']

In [42]:
print my_fruits.pop()
print my_fruits


kiwi
['Apple', 'banana', 'cherry', 'gauva', 'fig', 'grapes', 'gauva', 'jackfruit']

In [47]:
print my_fruits.pop(30)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-47-be3068b4b60a> in <module>()
----> 1 print my_fruits.pop(30)

IndexError: pop index out of range

In [43]:
# remove
print help(my_fruits.remove)


Help on built-in function remove:

remove(...)
    L.remove(value) -- remove first occurrence of value.
    Raises ValueError if the value is not present.

None

In [44]:
print my_fruits.remove('gauva')


None

In [45]:
print my_fruits


['Apple', 'banana', 'cherry', 'fig', 'grapes', 'gauva', 'jackfruit']

In [46]:
my_fruits.remove('Gauva')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-aaf9d13a2442> in <module>()
----> 1 my_fruits.remove('Gauva')

ValueError: list.remove(x): x not in list

In [48]:
# reverse
# sort

print help(my_fruits.reverse)


Help on built-in function reverse:

reverse(...)
    L.reverse() -- reverse *IN PLACE*

None

In [49]:
print my_fruits


['Apple', 'banana', 'cherry', 'fig', 'grapes', 'gauva', 'jackfruit']

In [50]:
print my_fruits.reverse()


None

In [52]:
print my_fruits


['jackfruit', 'gauva', 'grapes', 'fig', 'cherry', 'banana', 'Apple']

In [53]:
# sort
print help(my_fruits.sort)


Help on built-in function sort:

sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1

None

In [54]:
print my_fruits.sort(reverse=True)


None

In [55]:
print my_fruits


['jackfruit', 'grapes', 'gauva', 'fig', 'cherry', 'banana', 'Apple']

In [56]:
print my_fruits.sort()
print my_fruits


None
['Apple', 'banana', 'cherry', 'fig', 'gauva', 'grapes', 'jackfruit']

In [ ]:
# task
days = ['yesterday','today','tomorrow','dayafter']

#Task1:
# output
'''
yesterday 9
today     5
tomorrow  8
dayafter  8
'''

# Task2
# output
'''
Yesterday
TOday
TOMorrow
DAYAfter
'''

In [57]:
days = ['yesterday','today','tomorrow','dayafter']

for value in days:
    print value,len(value)


yesterday 9
today 5
tomorrow 8
dayafter 8

In [58]:
days = ['yesterday','today','tomorrow','dayafter']

In [59]:
for value in days:
    print value,days.index(value)


yesterday 0
today 1
tomorrow 2
dayafter 3

In [60]:
for value in days:
    print value,days.index(value),value[days.index(value)]


yesterday 0 y
today 1 o
tomorrow 2 m
dayafter 3 a

In [69]:
my_string="python"
print my_string[0:3]
print my_string[:3]
print my_string[3:]
print my_string[0:0]


pyt
pyt
hon


In [65]:
for value in days:
    print value[0:days.index(value)]


t
to
day

In [67]:
for value in days:
    print value[:days.index(value) + 1]


y
to
tom
daya

In [70]:
for value in days:
    print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]


Yesterday
TOday
TOMorrow
DAYAfter

In [ ]:
# homework
my_complete = ['apple','apple','banana','guava','guava']
# final output
# duplicate = [apple,guava]
# my_complete = [apple,banana,guava]

In [ ]:
# soft copy and deep copy

In [1]:
x = 1

In [3]:
print help(id)


Help on built-in function id in module __builtin__:

id(...)
    id(object) -> integer
    
    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)

None

In [4]:
print x,id(x),id(1)


1 31215992 31215992

In [5]:
y = 1
print y,id(y)


1 31215992

In [6]:
y = 2
print x,y,id(x),id(y)


1 2 31215992 31215968

In [7]:
z = 1
print z,id(z)


1 31215992

In [8]:
# soft copy
a = [33,44,55]
print a,id(a)


[33, 44, 55] 140520054705472

In [9]:
b = a
print b,id(b)


[33, 44, 55] 140520054705472

In [11]:
#is - memory blocks

print a is b
print z is a


True
False

In [12]:
a[0] = 66
print a # [66,44,55]
print b # [66,44,55]
print a is b


[66, 44, 55]
[66, 44, 55]
True

In [14]:
a = [77,88,99]
print a # [77,88,99]
print b # [66,44,55],[77,88,99]
print a is b


[77, 88, 99]
[66, 44, 55]
False

In [15]:
# deep copy
c = [33,44,55]
print c,id(c)


[33, 44, 55] 140519923925504

In [16]:
d = c[:]
print d,id(d)


[33, 44, 55] 140519924469984

In [17]:
print c is d


False

In [18]:
c[0] = 77
print c
print d
print c is d


[77, 44, 55]
[33, 44, 55]
False

In [19]:
# deep copy
import copy
print help(copy.deepcopy)


Help on function deepcopy in module copy:

deepcopy(x, memo=None, _nil=[])
    Deep copy operation on arbitrary Python objects.
    
    See the module's __doc__ string for more info.

None

In [20]:
e = copy.deepcopy(c)
print e,c,id(e),id(c)


[77, 44, 55] [77, 44, 55] 140519924030928 140519923925504

In [ ]:
# string to list and vice-versa.

In [21]:
my_string="python"

In [22]:
my_string[0]='T'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-a6e1b6047af7> in <module>()
----> 1 my_string[0]='T'

TypeError: 'str' object does not support item assignment

In [23]:
# convert a string to a list
Lmy_string = list(my_string)
print Lmy_string


['p', 'y', 't', 'h', 'o', 'n']

In [24]:
Lmy_string[0] = 'T'
print Lmy_string


['T', 'y', 't', 'h', 'o', 'n']

In [29]:
# conver the list to string
limiter=''  #just a variable you can give your name too.
print help(limiter.join)


Help on built-in function join:

join(...)
    S.join(iterable) -> string
    
    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.

None

In [30]:
print limiter.join(Lmy_string)


Tython

In [39]:
# task2:
my_sentence = "today is sunday"

In [32]:
Lmy_sentence = list(my_sentence)
print Lmy_sentence


['t', 'o', 'd', 'a', 'y', ' ', 'i', 's', ' ', 's', 'u', 'n', 'd', 'a', 'y']

In [33]:
print help(my_sentence.split)


Help on built-in function split:

split(...)
    S.split([sep [,maxsplit]]) -> list of strings
    
    Return a list of the words in the string S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are removed
    from the result.

None

In [41]:
# modified your string to list.
Lmy_sentence=my_sentence.split()
Lmy_sentence[2]="wednesday"
print Lmy_sentence


['today', 'is', 'wednesday']

In [42]:
# modify our list to a string
delimiter=" "
print delimiter.join(Lmy_sentence)


today is wednesday

In [ ]: