一.字符串——用引号括起的都是字符串
1.单引号与双引号
- 显示双(单)引号
双(单)引号位于单(双)引号中 - 撇号可以位于双引号之间;撇号不可以位于单引号之间
#√
"Python's strengthhs"
#×
'Python's strengthhs'
2.大小写字母
- title()
以首字母大写的方式显示每个单词 - upper()
全部大写 - lower()
全部小写 - eg:
name="ada lovelace"
print(name.title())
3.连接两个字符串:+
first_name="ada"
last_name="lovelace"
full_name=first_name+" "+last_name
message="Hello,"+full_name.title()+"!"
print(message)
4.\t制表符 \n换行符
print("\tPython")
5.删除空格
- 右侧rstrip() 左侧lstrip() 两端strip()
- 暂时删除
favorite_language='python '
print(favorite_language)
print(favorite_language.rstrip())
- 永久删除
favorite_language='python '
print(favorite_language)
#将结果存回原来的变量中
favorite_language=favorite_language.rstrip()
print(favorite_language)
6.单引号、双引号定义的字符串必须在一行;三引号可以分布在多行
二.数字
1.整数(integer)
- 四则运算
- 3**2表示3的2次方
- 空格不影响计算表达式
- 二进制0b开头、八进制0o开头、十六进制0x开头
2.浮点数
- 正常计算
- 存储不精确,比如1.1+2.2=3.30000…03,怎么解决?
from decimal import Decimal
print(Decimal(1.1)+Decimal(2.2))
3.str()将非字符串值→字符串
eg:
age=23
message=“happy”+str(age)+"rd birthday!"
三.布尔类型(boolean)
四.注释:#
不可以写中文
五.几条原则
The Zen of Python, by Tim Peters
- Beautiful is better than ugly.漂亮
- Explicit is better than implicit.优雅
- Simple is better than complex.简单
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.易理解
- Special cases aren’t special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- Although that way may not be obvious at first unless you’re Dutch.
- Now is better than never.
- Although never is often better than right now.
- If the implementation is hard to explain, it’s a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea – let’s do more of those!