数值
- 有两种基本的数值类型:int float
- 基本的运算符号:+ - * / ** //(求除数) %(求余)
- print函数:print(value1,value2)
print(2+1,3+4,3/1)
- 变量
- abs int round函数
- 在运算时尽可能多的使用括号
字符串
- 字符串定义:由单引号’'或者双引号""包围的一个字符串序列
- 字符串的引用:[m:n],find,rfind,[-m:-n],[m]
s="hello world"
print(s[0])
print(s[1:4])
print(s.find('h'))
print(s.rfind('0'))
- input函数:
input("please enter your name:")
- eval int float strl 函数
其中eval函数是将一个表达式赋值为整型或浮点型,长与input连用
a = eval(input("please enter your age:"))
输出
- 可选参数sep:可以是空格符号或者其他字符串
print("hello","world",sep="**")
print("hello","world",sep=" ")
- 可选参数end:由于一个print函数=打印出来的结尾是换行回车,所以在结尾时用end来替换原来的\n
print("hello",end=" ")
print("world")
- \t 和 \n
- format方法
print("{}".format())
列表 元组
- 列表:一串有序的序列,用中括号括起来,元素之间用逗号隔开
[1,2,3,4,5]
["hello","world","!"]
["name","age","sex"]
列表也可以进行切片操作:list[1:4]
split join方法
- 元组
t = a, b, c
s = (a, b, c)
- 嵌套列表:既有元组又有列表
L = [("name","Bob"),("age",12),("sex","femal"),("score","99")]
print(L[0],L[0][0],L[-1][-1])
参考资料:《Python程序设计》