print 打印,计算结果,打印变量,列表,结果。打印变量或列表时候需要使用括号,print(变量列表)
变量与字符串
将字符串的放到变量 nemo中,
>>> nemo= 'qiche,zixingche,huoche,diandongche'
>>> print (nemo)
qiche,zixingche,huoche ,diandongche
调用变量中的指定位置方式
>>> print (nemo[2])
c
调用变量中 起始到结束阶段
>>> print (nemo[2:11])
che,zixin
占位符
使用%s将值嵌入到字符串里面,%s在message中一个标记。主要是在打印的时候,加上要嵌入的值或者是变量。
>>> myscore=1000
>>> message='i scored %s points'
>>> print(message %myscore)
i scored 1000 points
可以在一个字符串中使用多个占位符,值排放的顺序就是被引用的顺序。
>>> 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!!
占位符也可以引用变量
>>> nums='what did the number %s say to the number %s? nice belt!!'
>>> nama1='gougou'
>>> name2='maomi'
>>> print(nums % (nama1,name2))
what did the number gougou say to the number maomi? nice belt!!
列表
创建一个列表,列表内容需要使用方括号,内容使用英文半角符区分,中间加以逗号。
>>> wizard=['spider legs','toe of frog','eye of newt']
>>> print (wizard)
['spider legs', 'toe of frog', 'eye of newt']
调用列表1位置的元素
>>> print (wizard[1])
toe of frog
调用列表起始0位置到结束3位置的内容,列表索引的 终止位置不包含在内
>>> print (wizard[0:3])
['spider legs', 'toe of frog', 'eye of newt']
替换,列表中索引位为2的元素
>>> wizard[2]='snail'
>>> print (wizard)
['spider legs', 'toe of frog', 'snail']
使用 变量名.append('元素内容') 往列表中添加元素,
>>> wizard.append('list')
>>> print (wizard)
['spider legs', 'toe of frog', 'snail', 'list']
使用del命令,从列表中删除指定位置的元素
>>> del wizard[3]
>>> print (wizard)
['spider legs', 'toe of frog', 'snail']
删除列表中多个元素,结束索引位置不会被包含在内。
>>> del wizard[2:3]
>>> print (wizard)
['spider legs', 'toe of frog', 'list']
列表相加可以把他们连起来,但只能使用*(乘法)和+加法
>>> list=['i','tripped','over','and','hit','the']
>>> list1=[1,2,3,4]
>>> print(list1+list)
[1, 2, 3, 4, 'i', 'tripped', 'over', 'and', 'hit', 'the']
列表乘以一个数字,将会打印出重复的列表元素。
>>> list2=[1,2]
>>> print(list2*5)
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
下面将list2列表中的1号元素位,乘以5,元素以数字相乘以,将会用于计算
>>> print (list2[1]*5)
10
将list2列表中的2号元素位e,乘以5,得到重复的5个字符串
>>> list3=['q','w','e','r']
>>> print (list3[2]*5)
eeeee
元组
就是一个使用括号的列表,但是元组的元素是不能修改的,一旦创建就不能做改动
>>> fibs=(0,1,2,3,4,5)
>>> print(fibs[3])
3
字典
创建一个字典,每个逗号间隔的为一组,第一个为键,第二个为值,可以直接查找第一个值,快速找到对应的值。(只能使用健去查找,不能使用值查找键)
>>> facorite_sports={'ralph':'footb','michael':'basketball','edward':'basketball','rebecca':'netball'}
>>> print(facorite_sports['edward'])
basketball
删除字典中的值,需要使用它的键。
>>> del facorite_sports['ralph']
>>> print (facorite_sports)
{'michael': 'basketball', 'edward': 'basketball', 'rebecca': 'netball'}
替换字典中的值,也需要使用到它的健,修改了edward键对应的值。
>>> facorite_sports['edward']='ice hockey'
>>> print (facorite_sports)
{'michael': 'basketball', 'edward': 'ice hockey', 'rebecca': 'netball'}
小测验:
#1:最爱
把爱好列出来,并把这个列表起个变量名 games。 把喜欢的食物列出来,起个变量名为foods。把这两个列表连在一起并把结果命名为 favorites,最后把变量favorites打印出来。
>>> games=['play games','play computer games','sing','listen to the music','tourism']
>>> foode=['spareribs','eggplant','potato','shrimp','Soybean']
>>> favorites=games+foode
>>> print(favorites)
['play games', 'play computer games', 'sing', 'listen to the music', 'tourism', 'spareribs', 'eggplant', 'potato', 'shrimp', 'Soybean']
#2:战士计数
有三座建筑,每座房顶藏有25个忍者,还有2个地道,每个地道藏有40个武士,那么一共有多少个忍者和武士准备投入战斗(你可以在PthonShell 程序里用一个算式做出来。)
>>> roofs = 3
>>> ninjas_per_roof = 25
>>> tunnels = 2
>>> samurai_per_tunnel = 40
>>> print((roofs * ninjas_per_roof) + (tunnels * samurai_per_tunnel))
155
#3:打招呼
创建两个变量:一个指向你的姓,一个指向你的名。创建一个字符串,用占位符使用这两个变量来打印带有你的名字的信息,比如“你好,某某某!”
>>> surname='lin'
>>> name='xiang'
>>> summary='"hello,%s%s!"'
>>> print (summary % (surname,name))
"hello,linxiang!"
领取专属 10元无门槛券
私享最新 技术干货