(Python)
######new part: append method
######we have a list
##lis = [1,'a',8, 0]
##print(lis)
########to append "yes" to the list:
##lis.append('yes')
##print(lis)
########index method: let us find the index of 'yes':
##index_yes = lis.index('yes')
##print(index_yes) ## here you cam see that it is 4, as it must
be
########insert method: we want to put 'o, no!' in the list at the
first place:
##lis.insert(0, 'o,no!')
##print(lis)
########### sort method: we wnat to sort out list; for our regrets
sorting is
###########possible only for same kind of data: list must be all
numeric of
##########string.
##########We split our list for string part (lis_str) and numeric
part (lis_num):
##########here is our list now: lis = ['o,no!', 1, 'a', 8, 0,
'yes']
######### numeric items have indeces 1,3,4, string items:
0,2,5.
##num = [1,3,4]
##stri = [0,2,5]
#########let us use the trick:
##lis_num = [lis[i] for i in num]
##lis_stri = [lis[i] for i in stri]
#########checking:
##print(lis_num)
##print(lis_stri)
#########let us sort them:
##lis_num.sort()
##lis_stri.sort()
#########cheking:
##print(lis_num)
##print(lis_stri)
######################TASK 2. For the list liss do the
following:
##liss = ['o,no!', -1, 'why?', 8, 0, 'yes', 'not, really']
#####append "hi" to the end
#####find the index of "yes" in the new liss list
#####insert 'what?' to the third place
#####extract string part of the liss and sort it
####$$$$$$$$ If your code works - it is acceptable, if it does not
- it is not acceptable.
#########remove and del
######## let us get the list
##li = [1, 2, 6, 'yes']
##########we want to remove the item 2:
##li.remove(2)
##print(li)
###########we want to delete the element with index 2 from the new
list li:
##del li[2]
##print(li)
If you have any doubts, please give me comment...
####new part: append method
####we have a list
lis = [1,'a',8, 0]
print(lis)
######to append "yes" to the list:
lis.append('yes')
print(lis)
######index method: let us find the index of 'yes':
index_yes = lis.index('yes')
print(index_yes) ## here you cam see that it is 4, as it must be
######insert method: we want to put 'o, no!' in the list at the first place:
lis.insert(0, 'o,no!')
print(lis)
######### sort method: we wnat to sort out list; for our regrets sorting is
#########possible only for same kind of data: list must be all numeric of
########string.
########We split our list for string part (lis_str) and numeric part (lis_num):
########here is our list now: lis = ['o,no!', 1, 'a', 8, 0, 'yes']
####### numeric items have indeces 1,3,4, string items: 0,2,5.
num = [1,3,4]
stri = [0,2,5]
#######let us use the trick:
lis_num = [lis[i] for i in num]
lis_stri = [lis[i] for i in stri]
#######checking:
print(lis_num)
print(lis_stri)
#######let us sort them:
lis_num.sort()
lis_stri.sort()
#######cheking:
print(lis_num)
print(lis_stri)
####################TASK 2. For the list liss do the following:
liss = ['o,no!', -1, 'why?', 8, 0, 'yes', 'not, really']
###append "hi" to the end
liss.append("hi")
print(liss)
###find the index of "yes" in the new liss list
index_hi = liss.index("yes")
print(index_hi)
###insert 'what?' to the third place
liss.insert(2, 'what?')
print(liss)
###extract string part of the liss and sort it
liss_str = [el for el in liss if type(el) is str]
liss_str.sort()
print(liss_str)
##$$$$$$$$ If your code works - it is acceptable, if it does not - it is not acceptable.
#######remove and del
###### let us get the list
li = [1, 2, 6, 'yes']
########we want to remove the item 2:
li.remove(2)
print(li)
#########we want to delete the element with index 2 from the new list li:
del li[2]
print(li)
Get Answers For Free
Most questions answered within 1 hours.