In [1]:
# lists
# list is a sequence of values - homongenous list or hetrogenous list
# list vs arrays
# list is linear representation of data.
# array is n dimensional representation of data.
# https://scipy.org/
In [1]:
# how to create a list
my_fruits = ['apple','banana','cherry','dates']
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 support indexing
# my_fruits = ['apple','banana','cherry','dates']
# 0 1 2 3 # +ve index or left to right
# -4 -3 -2 -1 # -ve index or right to left
In [6]:
# indexing
print my_fruits[0]
In [7]:
print my_fruits[-4]
In [8]:
# slicing
print my_fruits[1:3]
In [9]:
# we can modify the values.
print my_fruits
my_fruits[0]='Apple'
print my_fruits
In [10]:
my_string="apple"
print my_string[0]
my_string[0]='A'
In [12]:
# in operator
print my_fruits
print 'apple' in my_fruits
print 'Apple' in my_fruits
In [13]:
absentee = ['bibha','kumar','ashish']
for student in ['bibha','ansh','kumar','sri','ashish','ashok']:
if student in absentee:
continue
#break
#pass
print "result for the - {}".format(student)
In [14]:
# converting a list to string and a string to a list
my_string="python"
In [15]:
# convert a string to a list
Lmy_string = list(my_string)
print Lmy_string
In [20]:
# modify p to t
Lmy_string[0]='T'
print Lmy_string
# list to a string
new_string=''
print help(new_string.join)
print new_string.join(Lmy_string)
In [21]:
# example2:
my_sentence="today is saturday"
In [22]:
Lmy_sentence=list(my_sentence)
print Lmy_sentence
In [23]:
# split
print help(my_sentence.split)
In [25]:
# convert a string to a list
Lmy_sentence=my_sentence.split()
print Lmy_sentence
Lmy_sentence[-1]='Friday'
print Lmy_sentence
In [26]:
# convert a list to string
delimiter=" "
print delimiter.join(Lmy_sentence)
In [27]:
# khyaathi:x:1000:1000:KHYAATHI,,,:/home/khyaathi:/bin/bash
# useradd khyaathi
my_user=['khyaathi','x','1000','1000','khyaathi home','/home/khyaathi','/bin/bash']
In [30]:
print my_user
print ':'.join(my_user)
print ','.join(my_user)
In [2]:
# Function in a list
print my_fruits
In [3]:
print dir(my_fruits)
In [4]:
# append
print help(my_fruits.append)
In [5]:
print my_fruits.append('gauva')
In [6]:
print my_fruits
In [8]:
# extend
print help(my_fruits.extend)
print my_fruits.extend(['grapes','jackfruit','kiwi'])
print my_fruits
In [10]:
# insert
print help(my_fruits.insert)
my_fruits.insert(0,'apricot')
print my_fruits
In [11]:
# index
print help(my_fruits.index)
In [12]:
# use case
my_fruits.insert(0,'grapes')
print my_fruits
In [15]:
print my_fruits.index('grapes')
print my_fruits.index('grapes',1)
In [16]:
# count
print help(my_fruits.count)
In [17]:
print my_fruits.count('grapes')
In [21]:
# reverse
print help(my_fruits.reverse)
my_fruits.reverse()
print my_fruits
In [20]:
# sort
# ascending or descending
In [22]:
print help(my_fruits.sort)
In [23]:
# ascending
my_fruits.sort()
print my_fruits
In [24]:
# descending
my_fruits.sort(reverse=True)
print my_fruits
In [25]:
# pop
print help(my_fruits.pop)
In [26]:
# i want to remove grapes
my_fruits.pop(2)
Out[26]:
In [27]:
print my_fruits
In [28]:
print my_fruits.pop(20)
In [29]:
# remove
print help(my_fruits.remove)
In [30]:
my_fruits.remove('grapes')
print my_fruits
In [31]:
my_fruits.remove('grapes')
In [52]:
# example1:
my_trainings = ['python','django','python','django','python','devops']
# my_trainings = ['python','django','devops']
# my_duplicates = ['python','django']
In [53]:
# soft copy and deep copy.
my_duplicates=[]
for value in my_trainings[:]:
print "value- {}".format(value)
if my_trainings.count(value) > 1:
print "value rm {}".format(value)
my_trainings.remove(value)
if value not in my_duplicates:
print "value add {}".format(value)
my_duplicates.append(value)
print my_trainings
print my_duplicates
In [54]:
# example2
my_days = ['yesterday','today','tomorrow','dayafter']
# task1:
# output
# yesterday' 9
# today 5
# tomorrow 8
# dayafter 8
# task2:
# output
# Yesterday
# TOday
# TOMorrow
# DAYAfter
In [55]:
# task1
for value in my_days:
print value,len(value)
In [61]:
# task2
print my_days
for value in my_days:
print value,my_days.index(value),value[my_days.index(value)],value[0:my_days.index(value)],value[0:my_days.index(value) + 1]
In [63]:
print my_days
for value in my_days:
print value[0:my_days.index(value)]
In [66]:
print my_days
for value in my_days:
print value[0:my_days.index(value) + 1]
In [65]:
my_string="python"
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 [68]:
print my_days
for value in my_days:
print value[:my_days.index(value) + 1].upper() + value[my_days.index(value) + 1:]
In [69]:
# soft and deep copy
# memory allocaiton happend for lists in python
In [70]:
# variable
a = 1
In [71]:
print help(id)
In [72]:
print id(a)
print id(1)
In [73]:
b = 1
In [74]:
print id(b)
In [75]:
b = 2
In [76]:
print id(a)
print id(1)
print id(2)
print id(b)
In [77]:
# lists
In [85]:
a = [1,2,3]
print a,id(a)
b = a
print b,id(b)
In [86]:
# is
print a is b
In [87]:
# softcopy - object(memory) can have multiple labels.
# case1: modify one value within my list
a[1] = 30
print a # [1,2,3] or [1,30,3]
print b # [1,2,3] or [1,30,3]
print a is b
In [89]:
# case2 : my modify my whole list
a = [20,30,40]
print a # [1,30,3] or [20,30,40]
print b # [1,30,3] or [20,30,40]
print a is b
In [90]:
# deep copy - each object has its own memory allocation.
In [91]:
c = [20,30,40]
d = c[:] # one way to create a deep copy.
print c,d
print id(c),id(d)
print c is d
In [92]:
# case1:
c[1]=33
print c
print d
In [93]:
# another way to create deepcopy.
import copy
print dir(copy)
print help(copy.deepcopy)
e = copy.deepcopy(c)
print e
print c
print e is c
In [1]:
# list comprehension
# https://github.com/tuxfux-hlp/Python-examples/blob/master/lists/list_comprehension.txt
In [ ]:
# input of number - string - 1,2,3,4,5,6,7,8,9,10
# 2,4,6,8,10
In [6]:
# example1
even=[]
number = raw_input("please enter the number: ")
for value in number.split(','):
if int(value) % 2 == 0:
even.append(value)
print ",".join(even)
In [9]:
# list comprehnesion on example1
number = raw_input("please enter your number:")
# [ print expression condition]
print ",".join([ value for value in number.split(',') if int(value) % 2 == 0])
In [10]:
# example2:
my_string="today is monday"
for value in my_string.split():
print value
In [12]:
[ [value,value.capitalize(),value.upper()] for value in my_string.split()]
Out[12]:
In [ ]: