repl.it
@yskov/

python chapter 8

Python

No description

fork
loading
Files
  • main.py
main.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
#lists have square brackets. can have lists that include lists. 
#can iterate through a list
for i in [1,2,3,4,5]:
  print('Happy new year', i)

z=[1,3,5,7,9]

friends = ['joe', 'sally', 'greg']

print (z[0])

#the option below gives a traceback because strings are not mutable so the 0 item is a capital B and not a lower case one. 
#fruit = 'Banana'
#fruit[0] = 'b'


#you can create a counted loop this way: 
for i in range (len(friends)):
  friend = friends[i]
  print ('happy new year', friend)


#you can slice lists with the up to but not including denotation. 
print (z[1:3])


#you can append a list
z.append(7)

print (z)


#you can treat a string as a list with split
empty = list()
x= 'with three words;hello'

#using split for a string automatically makes the string a list
stuff= x.split()

print (stuff)

for i in stuff:
  empty.append(i)

#can sort a list with sort method
empty.sort()
print (empty)


#you can split based on a certain parameter
y = x.split(';')
print(y)