Python For Kids学习笔记03:Strings, Lists, Tuples, and Maps

本文介绍了Python中字符串的创建及处理技巧,包括单行和多行字符串的使用方法、特殊字符的转义方式,以及如何在字符串中插入变量值。此外,还详细讲解了列表(List)、元组(Tuple)和字典(Dictionary)等数据结构的基本操作,如元素的增删改查、切片应用、列表拼接和重复等。

创造一个单行字符串

fred = "Why do gorillas have big nostrils? Big fingers!!"
>>> print(fred)
Why do gorillas have big nostrils? Big fingers!!


如果忘记写一对双引号,会出现这个错误

SyntaxError: EOL while scanning string literal


我当时觉得这本书好棒啊!教我们面对错误不要紧张,我们后面遇到问题也都觉得可以迎刃而解


多行字符串,用两对三个单引号表示,把内容放在中间

>>> fred = '''How do dinosaurs pay their bills?
With tyrannosaurus checks!'''
>>> print(fred)
How do dinosaurs pay their bills?
With tyrannosaurus checks!

如果我要输入的内容里面有引号怎么办?

>>> silly_string = "He said, "Aren't can't shouldn't wouldn't.""
SyntaxError: invalid syntax

方法一:多行字符串

silly_string = '''He said, "Aren't can't shouldn't wouldn't."'''


方法二:用“\”来避免歧义

>>> single_quote_str = 'He said, "Aren\'t can\'t shouldn\'t wouldn\'t."'
>>> double_quote_str = "He said, \"Aren't can't shouldn't wouldn't.\""
>>> print(single_quote_str)
He said, "Aren't can't shouldn't wouldn't."
>>> print(double_quote_str)
He said, "Aren't can't shouldn't wouldn't."


Embedding Values in Strings 在字符串中插入数值

>>> myscore = 1000
>>> message = 'I scored %s points'
>>> print(message % myscore)
I scored 1000 points

再看一个例子

>>> joke_text = '%s: a device for finding furniture in the dark'
>>> bodypart1 = 'Knee'
>>> bodypart2 = 'Shin'
>>> print(joke_text % bodypart1)
Knee: a device for finding furniture in the dark
>>> print(joke_text % bodypart2)
Shin: a device for finding furniture in the dark

不单只能使用1个占位符哦,可以使用多个

>>> nums = 'What did the number %s say to the number %s? Nice belt!!'
>>> print(nums % (0, 8))
What did the number 0 say to the number 8? Nice belt!!


多次输出字符串

>>> print(10 * 'a')
aaaaaaaaaa


spaces = ' ' * 25
print('%s 12 Butts Wynd' % spaces)
print('%s Twinklebottom Heath' % spaces)
print('%s West Snoring' % spaces)
print()
print()
print('Dear Sir')
print()
print('I wish to report that tiles are missing from the')
print('outside toilet roof.')
print('I think it was bad wind the other night that blew them away.')
print()
print('Regards')
print('Malcolm Dithering')


List列表的使用方法

>>> wizard_list = 'spider legs, toe of frog, eye of newt, bat wing,
slug butter, snake dandruff'
>>> print(wizard_list)
spider legs, toe of frog, eye of newt, bat wing, slug butter, snake
dandruff

>>> wizard_list = ['spider legs', 'toe of frog', 'eye of newt',
'bat wing', 'slug butter', 'snake dandruff']
>>> print(wizard_list)
['spider legs', 'toe of frog', 'eye of newt', 'bat wing', 'slug
butter', 'snake dandruff']

两种对比

输出列表中的某一位,记住哦 列表的第一位是从0开始计算的

>>> print(wizard_list[2])
eye of newt

>>> wizard_list[2] = 'snail tongue'
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'snake dandruff']

用冒号(X:Y)表示从列表的X位到Y位

>>> print(wizard_list[2:5])
['snail tongue', 'bat wing', 'slug butter']


列表的内容可以是数值和字符串的混合

>>> numbers_and_strings = ['Why', 'was', 6, 'afraid', 'of', 7,
'because', 7, 8, 9]
>>> print(numbers_and_strings)
['Why', 'was', 6, 'afraid', 'of', 7, 'because', 7, 8, 9]

组合两个列表

>>> numbers = [1, 2, 3, 4]
>>> strings = ['I', 'kicked', 'my', 'toe', 'and', 'it', 'is', 'sore']
>>> mylist = [numbers, strings]
>>> print(mylist)
[[1, 2, 3, 4], ['I', 'kicked', 'my', 'toe', 'and', 'it', 'is', 'sore']]


用append 往列表中插入一项

>>> wizard_list.append('bear burp')
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'snake dandruff', 'bear burp']

>>> wizard_list.append('mandrake')
>>> wizard_list.append('hemlock')
>>> wizard_list.append('swamp gas')
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'snake dandruff', 'bear burp', 'mandrake', 'hemlock', 'swamp
gas']


用del 从列表中删除一项

>>> del wizard_list[5]
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'bear burp', 'mandrake', 'hemlock', 'swamp gas']
>>> del wizard_list[8]
>>> del wizard_list[7]
>>> del wizard_list[6]
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'bear burp']


用+ 组合两个list

>>> list1 = [1, 2, 3, 4]
>>> list2 = ['I', 'ate', 'chocolate', 'and', 'I', 'want', 'more']
>>> list3 = list1 + list2
>>> print(list3)
[1, 2, 3, 4, 'I', 'ate', 'chocolate', 'and', 'I', 'want', 'more']


用数字*list 可以重复这个list几次

>>> list1 = [1, 2]
>>> print(list1 * 5)
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]


list可以和其他运算符号一起用吗?来试试

>>> list1 / 20
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
list1 / 20
TypeError: unsupported operand type(s) for /: 'list' and 'int'
>>> list1 - 20
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
list1 - 20
TypeError: unsupported operand type(s) for -: 'list' and 'int'


这样加有歧义,所以报错

>>> list1 + 50
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
list1 + 50
TypeError: can only concatenate list (not "int") to list


用圆括号的list,我们叫它什么?Tuples元组!

>>> fibs = (0, 1, 1, 2, 3)
>>> print(fibs[3])
2


元组是创建后不能随便修改的

>>> fibs[0] = 4
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
fibs[0] = 4
TypeError: 'tuple' object does not support item assignment


map也叫dict(dictionary)就是一组东西,什么东西呢?类似list或者tuples,我们看看下面就知道了

>>> favorite_sports = ['Ralph Williams, Football',
'Michael Tippett, Basketball',
'Edward Elgar, Baseball',
'Rebecca Clarke, Netball',
'Ethel Smyth, Badminton',
'Frank Bridge, Rugby']

这样我们可以很快的找到对应的值

>>> print(favorite_sports['Rebecca Clarke'])
Netball

删除的方法也和list类似

>>> del favorite_sports['Ethel Smyth']
>>> print(favorite_sports)
{'Rebecca Clarke': 'Netball', 'Michael Tippett': 'Basketball', 'Ralph
Williams': 'Football', 'Edward Elgar': 'Baseball', 'Frank Bridge':
'Rugby'}

这样,我们可以替换map里面的值

>>> favorite_sports['Ralph Williams'] = 'Ice Hockey'
>>> print(favorite_sports)
{'Rebecca Clarke': 'Netball', 'Michael Tippett': 'Basketball', 'Ralph
Williams': 'Ice Hockey', 'Edward Elgar': 'Baseball', 'Frank Bridge':
'Rugby'}


map和list、tuples最大的不同,就是不能把两个map组合在一起

>>> favorite_sports = {'Rebecca Clarke': 'Netball',
		'Michael Tippett': 'Basketball',
		'Ralph Williams': 'Ice Hockey',
		'Edward Elgar': 'Baseball',
		'Frank Bridge': 'Rugby'}
>>> favorite_colors = {'Malcolm Warner' : 'Pink polka dots',
		'James Baxter' : 'Orange stripes',
		'Sue Lee' : 'Purple paisley'}
>>> favorite_sports + favorite_colors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值