Chapter 3Chapter 4
Chapter 3
names = [
'Tom',
'Jerry',
'Jack']
for name
in names:
print(name, end=
', ')
Tom, Jerry, Jack,
persons = [
'Tom',
'Jerry',
'Jack']
persons.insert(
0,
'QQ')
persons.insert(len(persons)//
2,
'HH')
persons.append(
'PP')
for person
in persons:
print(
'Welcome,' + person +
'!')
Welcome,QQ!
Welcome,Tom!
Welcome,HH!
Welcome,Jerry!
Welcome,Jack!
Welcome,PP!
print(
'Just 2.')
while(len(persons)>
2):
print(
"Sorry,", persons[-
1])
persons.pop()
for person
in persons:
print(
'Welcome again,', person)
del persons[:]
print(persons)
Just 2.
Sorry, PP
Sorry, Jack
Sorry, Jerry
Sorry, HH
Welcome again, QQ
Welcome again, Tom
[]
print(len(persons))
0
Chapter 4
animals = [
'dog',
'cat',
'bird']
for animal
in animals:
print(
"A " + animal +
" would make a great pet")
print(
'Any of these animals would make a great pet!')
A dog would make a great pet
A cat would make a great pet
A bird would make a great pet
Any of these animals would make a great pet!
for i
in range(
1,
21):
print(i, end=
" ")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[x**
3 for x
in range(
1,
11)]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]