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]