-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch5.py
113 lines (87 loc) · 2.27 KB
/
ch5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 17:15:57 2021
@author: HMFR
"""
# Lists
#made by [] and commas,
animals = ['cock', 'hen', 'chicken']
print(animals[0])
animals[0]='rooster'
print(animals[0])
# indices are circular i.e 0 1 2 -1 -2 -3
print(animals[-1])
# append function
animals.append('fox')
print(animals[-1])
#extend function
animals.extend(['snake','rabbit'])
print(animals)
animals.extend(animals)
print(animals)
#insert function
animals = ['rooster', 'hen', 'chicken', 'fox', 'snake', 'rabbit']
animals.insert(0,'bear')
print(animals)
#slices
some = animals[1:4]#from 1 to before 4
print('animals are {}'.format(animals))
print('1 to 4 are {}'.format(some))
slice = animals[4:]#4 to the LAST
print('4: is {}'.format(slice))
slice = animals[-2:]
print('-2: is {}'.format(slice))
# slice from strings
slice = 'Hashem Moradmand'[0:6] #from index 0 to 5
print(slice)
# index function returns the index of occurance of sth
print('chicken is occured at {}'.format(animals.index('chicken')))
# if index func cannot find the value in the list => exception
#indexat = animals.index('chick')
#print('chick is occured at {}'.format(indexat))
# exception handling: try & except
try:
indexat = animals.index('chick')
except:
indexat = 'NA'
print('chick is occured at {}'.format(indexat))
# for loop
# for item_variable in list_name:
for anim in animals:
print(anim.upper())
# while loop
# while condition:
print ('---WHILE LOOP---')
index = 0
while index < len(animals):
print(animals[index])
index +=1;
print('end')
#sort() and sorted() functions
animSorted = sorted(animals)
print(animSorted)
print(animals)
animals.sort() #changes the list itself (list will be sorted)
print(animals)
# concatenating two lists
moreanimals = ['dog','cat']
allanimals = animals + moreanimals
print(allanimals)
# append function
allanimals.append('wolf')
#allanimals.append('wolf','tiger') #append takes just one arg
print(allanimals)
#range function
#range(N) produces indices 0 to N-1 totaly N items
for num in range(5):
print(num)
for num in range(len(animals)):
print(animals[num])
for num in range(2,5):
print(num)
print('------')
for num in range(10,25,2):
print(num)
for num in range(0,len(animals),3):
print(animals[num])