《笨办法学python3》的学习笔记(5-9)节
2016-03-17 11:52
507 查看
5.更多的变量和打印
在输入更多的变量并且想要将他打印出来的时候。需要使用一个叫做“格式化字符串”的东西每一次使用双引号将一些文本引用起来,就是创建了一个字符串ex5.py
my_name="Zed A.Shaw" my_age=35 # not a lie my_height=74#inches my_weight=180#lbs my_eyes='blue' my_teeth='White' my_hair='Brown' print"Let's talk about %s."%my_name print"He's %d inches tall."%my_height print"He's %d pounds heavy."%my_weight print"Actually that's not too heavy." print"He's got %s eyes and %s hair."%(my_eyes,my_hair) print"His teeth are usually %s depending on the office."%my_teeth #this line is tricky,try to get it exactly right print"If I add %d,%d,and %d I get %d."%( my_age,my_height,my_weight,my_age+my_height+my_weight)
变量名要以字母开头,所以不能直接使用数字
%s,%r和%d都是一种格式控制工具:表示将右边的变量依次放到左边相应的位置上。 当有多个时,用,隔开。
-
6.字符串和文本
字符串可以包含之前看见过的简单的字符串。只需要将格式化的变量放到字符串中,紧跟着一个百分号就可以。需要注意的是如果想要在字符串中通过格式化字符串放入多个变量,需要将变量放到圆括号(()),而且变量之间是用逗号,隔开。程序 ex6.py:
x="There are %d types of people."%10 binary="binary" do_not="don't" y="Those who know %s and those who %s."%(binary,do_not) print x print y print"I said:%r."% x print"I also said:'%s.'"% y hilarious=False joke_evaluation="Isn't that joke so funny?!%r" print joke_evaluation%hilarious w="This is the left side of..." e="a string with a right side." print w+e
%r和%s的不同在于:%r会显示原始的数据(raw data),%s和其他符号则是用来向用户显示输出的,显示变量了
7.更多打印
注意在这个里面的‘snow’其实是字符串而不是变量的名字,变量的名字是不会带引号的程序ex7.py:
print"Mary had a little lamb." print"Its fleece was white as %s."%'snow' print"And everywhere that Mary went." print"% and #"*10#what'd that do? end1="C" end2="h" end3="e" end4="e" end5="s" end6="e" end7="B" end8="u" end9="r" end10="g" end11="e" end12="r" #watch that comma at the end .try removing it to see what happens print end1+end2+end3+end4+end5+end6 print end7+end8+end9+end10+end11+end12
单引号和双引号的区别其实也不是很明显,就是单引号一般被用来创建简短的字符串.
-
8.打印,打印
依然还是定义变量和打印输出。程序ex8.py:
formatter="%r %r %r %r" print formatter%(1,2,3,4) print formatter%("one","two","three","four") print formatter%(True,False,False,True) print formatter%(formatter,formatter,formatter,formatter) print formatter%( "I had this thing.", "That you could type up right.", "But it didn't sing.", "So I said good night." )
需要注意的一点是在打印语句中。”one”,”two”,”three”,”four”和True,False,False,True这样的区别。ture和false是关键字,用来表示真和假的概念,如果加上了引号就便成了字符串,就没有逻辑平判定的功能了。
9.打印,打印,打印
照例还是定义变量和利用print语句输出一些东西。程序ex9.py:
#here's some new stange stuff,remember type it exactly days="Mon Tue Wed Thu Fri Sat Sun" months="Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print"Here are the days:",days print"Here are the months:",months print""" There 's somthing going on here." With the treee double-quotes." We'll be able to type as much as we like." Even 4 lines if we want,or5,or6. """
“”“三引号可以将多行的字符串连起来进行输出
\n的意思是进行换行操作,需要指出的是:\n需要放在print语句之中才能够生效。
相关文章推荐
- Python-Argparse
- 基于bandersnatch搭建pip本地源
- Python+Selenium+Eclipse 进行web测试 的环境配置(1)-粗略版
- python判断是文件还是目录的注意事项
- fedora python pillow pil
- Hadoop之使用python实现数据集合间join操作
- python ——使字典按添加顺序输出
- 抓取高速公路摄像照片
- python 类中的某个函数作为装饰器
- Python实现计算最小编辑距离
- python os模块 常用命令
- python如何一次性取出多个随机数
- python员工查询表
- python os模块详细介绍
- Python 数据分析:pandas 操作基础篇
- python的XML及json解析
- android 使用 ase执行python脚本
- java getBytes() | python bytearray()
- Python单元测试——深入理解unittest
- python子进程模块subprocess详解与应用实例 之三