第一章:
1. 判断变量 var 是否已经定义
var_exists = 'var' in locals() or 'var' in globals()
2. 获取变量 var 类型 type(var)
3. 判断变量var是不是int型或者字符串型
isinstance(var,(int,str)) #注意:int,str不带引号
4. var = 123 ,将var变为字符串类型, str(a),类似的还有bool() int() float() compex()
5. num = -123,输出var的绝对值 abs(num)
6. num = 5.6,四舍五入 num, round(num)
7. a=2 b=3 求a的b次方 (1)a**b (2)pow(a,b)
8.进制转换 hex() 转换为16进制 oct()转换为8进制
9.求1 2 3的和 sum([1,2,3])
10. 拼接字符串a b a+b
11. 不知道一个函数怎么应该用 help(查看函数的帮助信息)
12. str = 'how are you',输出其中的h,are,0
a = 'how are you'print(str(a[0]))print(str(a[4:7]))print(str(a[-2]))print(str(a[-2:])) #从倒数第二个到最后print(str(a[2:])) #从第三个直接到最后
13. %s(str),%d(int),%c(单个字符),%r(repr函数表达字符串)
name = 'xuzhan'name2 = 'xiaohong'age = 18print("my name is %s i am %d years old,her name is %s" %(name,age,name2))
14. a = "abcdefg hijklmnopq",将首字母大写 a.capitalize()
15. a = "abcdefg hijklmnopq" ,检查字符串中是否有jk a.find("jk"),找不到则返回-1。index()找不到则报错
16. b = "ABCD" 将str转小写 b.lower()
17. c = "zhang san,wang wu "标题化 (遇到逗号和空格都把首字母大写) c.title()
18. c = "Zhang San,Wang Wu" 判断是否是标题化的字符串 c.istitle()
19. d = " zhangsan " 去掉d左边的空格 d.lstrip() # 仅能去掉左边的,右边的还在d.rstrip() 全部去掉 用strip()
20. e = "zhangsan is good" 把zhangsan替换为 lisi
e.replace("zhangsan","lisi")
21. f = "zhangsan lisi wangwu " 将f以空格分割为三部分
f.split(' ',2)
var = 456var_exists = 'var' in locals() or 'var' in globals()print(var_exists)if (isinstance(var,(int,str))): print(789)a = 'how are you'print(str(a[0]))print(str(a[4:7]))print(str(a[-2]))print(str(a[-2:])) #从倒数第二个到最后print(str(a[2:])) #从第三个直接到最后name = 'xuzhan'age = 18print("my name is %s iam %d" %(name,age))a = 'abcdefg hijklmnopq'print(a.capitalize()) #将a的首字符大写print(a.find('jk')) #找不到则返回-1# print(a.index('jka')) #找不到则报错b = 'ABCDEFG'print(b.lower())c = "zhang san,wang wu "print(c.title())c = "Zhang San,Wang Wu"print(c.istitle())d = " zhangsan "print(d.lstrip() + b)e = "zhangsan is good!"print(e.replace("zhangsan","lisi"))f = "zhangsan lisi wangwu "print(print(f.split(' ',1)))
22. pass 什么也不做,一般用于判断语句中