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)
In [3]:
# using empty []
empty_list = []
print empty_list
print type(empty_list)
In [4]:
# using list()
empty_list = list()
print empty_list
print type(empty_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]
In [9]:
# slicing
print my_fruits[0:2]
print my_fruits[:2]
print my_fruits[2:]
In [10]:
# list elements are mutable.(you can modify them)
print my_fruits[0] # apple
my_fruits[0] = 'Apple'
print my_fruits[0]
In [11]:
print my_fruits
In [13]:
my_string="python"
print my_string[0]
my_string[0]="T"
In [18]:
# you can iterate over a list
for value in my_fruits:
print value
In [14]:
# in operator
print 'apple' in my_fruits
print 'banana' in my_fruits
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)
In [19]:
# Functions
print my_fruits
In [20]:
print dir(my_fruits)
In [21]:
# append
print help(my_fruits.append)
In [22]:
my_fruits.append('fig')
In [23]:
print my_fruits
In [24]:
# extend
print help(my_fruits.extend)
In [25]:
my_fruits.extend(['grapes','jackfruit','kiwi'])
print my_fruits
In [26]:
# insert
print help(my_fruits.insert)
In [28]:
my_fruits.insert(6,'gauva')
In [29]:
print my_fruits
In [30]:
my_fruits.insert(3,'gauva')
In [31]:
print my_fruits
In [32]:
# index
print help(my_fruits.index)
In [33]:
print my_fruits.index('gauva')
In [34]:
print my_fruits.index('gauva',4)
In [35]:
# count
print help(my_fruits.count)
In [37]:
print my_fruits.count('apple')
print my_fruits.count('gauva')
In [38]:
# POP
print help(my_fruits.pop)
In [39]:
print my_fruits
In [40]:
print my_fruits.pop(my_fruits.index('dates'))
In [41]:
print my_fruits
In [42]:
print my_fruits.pop()
print my_fruits
In [47]:
print my_fruits.pop(30)
In [43]:
# remove
print help(my_fruits.remove)
In [44]:
print my_fruits.remove('gauva')
In [45]:
print my_fruits
In [46]:
my_fruits.remove('Gauva')
In [48]:
# reverse
# sort
print help(my_fruits.reverse)
In [49]:
print my_fruits
In [50]:
print my_fruits.reverse()
In [52]:
print my_fruits
In [53]:
# sort
print help(my_fruits.sort)
In [54]:
print my_fruits.sort(reverse=True)
In [55]:
print my_fruits
In [56]:
print my_fruits.sort()
print my_fruits
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)
In [58]:
days = ['yesterday','today','tomorrow','dayafter']
In [59]:
for value in days:
print value,days.index(value)
In [60]:
for value in days:
print value,days.index(value),value[days.index(value)]
In [69]:
my_string="python"
print my_string[0:3]
print my_string[:3]
print my_string[3:]
print my_string[0:0]
In [65]:
for value in days:
print value[0:days.index(value)]
In [67]:
for value in days:
print value[:days.index(value) + 1]
In [70]:
for value in days:
print value[:days.index(value) + 1].upper() + value[days.index(value) + 1:]
In [ ]:
# homework
my_complete = ['apple','apple','banana','guava','guava']
# final output
# duplicate = [apple,guava]
# my_complete = [apple,banana,guava]