0% found this document useful (0 votes)
13 views3 pages

Ch7 Lists

Uploaded by

ztrk.seray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Ch7 Lists

Uploaded by

ztrk.seray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# # # Ch7 Lists

# # mylist = [] # empty list


# # print(type(mylist))
# # mylist1 = [1,2,3,4,5] # declaring a list of numbers
# # mylist2 = ['hello','world','how']
# # mylist3 = [1,2,3,'a','b','c',3.4,True,False] # mixture of different variable types within a
list
# # print(mylist3)
#
# mylist4 = [1,2,3,[0,1,-1]] # nested lists
# print(mylist4)
#
# mylist4[2] = 1000 # mutable , strings are immutable
# print(mylist4)
# print(mylist4[3]) # access by the index
# print(mylist4[3][0]) # a secondary index to access elements of the nested list
# print(mylist4[3][1])
# print(mylist4[3][2])
# y = mylist4[3]
# # print(y[0],y[1],y[2],mylist4[3][0])
# cheeses = [[1],'cheddar','edam','gouda','mozeralla','Dil','hellim'] # list of strings
# print([1] in cheeses) # True
# print('dil' in cheeses) # False
# print('mozeralla' in cheeses) # True
# print('edam' in cheeses) # True
# # print(dir('string'))
# numbers = [1,2,3,4,5,[1]] # nested lists
# print(1 in numbers) # True
# print([1] in numbers) # True
# for cheese in cheeses:
# print(cheese)
# # # alternatively
# for i in range(len(cheeses)):
# print(cheeses[i]) # access by the index

# 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[1:3]) # index 1, 2 two elements


# print(t[:4]) # take index 0, 1, 2, 3 four elements
# print(t[:]) # copy all the elements
# a = t[:]
# print(t)
# a[0] = 1000

# 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"

mylist = [10,20,-10,-20,100] # define a list


print(mylist) # print the mylist
mylist.sort(reverse=True) # sorting the elements of the list
print(mylist) # sorted now

# # deleting elements in a list


t = ['a','b','c']
print(t) # before we remove the second element t
t.pop(1)
# x = t.pop(1) # returns the element we remove
# print(x)
print(t) # after we remove the second element 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)

# lists and functions


nums=[-1,3,5,7,10]
print(len(nums)) # length of a list
print(max(nums)) # the maximum value of a list
print(min(nums)) # the minimum value of a list
print(sum(nums)) # the sum of the elements of a list

#
# # 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)

You might also like