Ch7 Lists
Ch7 Lists
# list operations
# a = [1,2,3]
# b = [4,5,6]
# print(a+b) # merge two lists together
# print(a*10) # creates repeated elements in a list
# print([0]*100)
# list slices
# t = ['a','b','c','d','e','f','g']
# print(t)
# # print(t[2:5]) # index 5 is not included, we have 2, 3, 4
# # a = t[:] # create a copy of t
# a = t
# print(a)
# t[0] = 10000
# print(a) # a is also updated the index 0 if we use a = t
# # input('hold on ')
# print(t)
#
# b = t
# b[0] = 1000
# print(t)
# # t[1:3] = ['x','y']
# # print(t)
#
# # # list methods
import math
t = [25.5,'a','b','c',3,math.sin(10)]
print(t)
# print(dir(t)) # all list methods displayed
t.append('d') # adding elements to the existing list
print(t)
t2 = ['d','e','f','g']
print( t + t2 )
# t = t + t2
t.extend(t2) # use the + or extend to merge these two lists
print(t)
# # alternatively
print(dir(t)) # show you the list of methods available to use with the list "t"
x = [1,2,3,4,5,6]
print(x)
del x[3:]
print(x)
del x[0], x[1] # delete the list element in index 0 and 1
print(x)
t = ['a','b','c']
del t[0]
print(t)
#
# # total = 0
# # count = 0
# # while (True):
# # inp = input('Enter a number: ')
# # if inp == 'done':
# # break
# # value = float(inp)
# # total = total + value
# # count = count + 1
# #
# # average = total/count
# # print('Average of numbers entered =',average)
#
#
# numlist = []
# while (True):
# inp = input('Enter a number: ')
# if inp == 'done':
# break
# value = float(inp)
# numlist.append(value)
# average = sum(numlist)/len(numlist)
# print(numlist)
# print('Average of numbers entered =',average)
#
#
# # lists and strings
# s = 'banana'
# x = list(s)
# print(x)
#
# mystring = "Hello Jack, how are you today?, today is sunny, 120"
# # t = mystring.split()
# # print(t)
# t = mystring.split(',')
# print(t,type(t))
#
# for words in t:
# x = words.strip()
# print(x)
#
# # a = [1,2,3]
# # print(a)
# # b = a[:] # a copy of a
# # b[0]=100
# # print(a , b)
#
#
# # t1 = [1,2]
# # t2 = t1.append(3)
# # print(t1)
# # print(t2)
# #
# # t3 = t1 + [3]
# # print(t3)
# # print(t2 is t3)
#
#
# input('hold on--------')
# print(t)
# def bad_delete_head(t):
# t = t[1:]
# print(t)
#
#
# def tail(t):
# return t[1:]
#
# t = [1,2,3,4,5,6]
# print(t)
# x = bad_delete_head(t)
# print(x)
# x = tail(t)
# print(x)
#
# #
# # mytext = "When we read and parse files, \n there are many opportunities to encounter input
that can crash our program so it is a good idea \n to revisit the guardian pattern when it
comes writing programs that read through a file an"
# # mytext2 = mytext.split('\n')
# # print(mytext2)
# # for line in mytext2:
# # words = line.split()
# # print(words)