Python
sun_apollo
这个作者很懒,什么都没留下…
展开
-
【08】函数
调用函数 >>> int('123') 123 >>> int(12.34) 12 >>> float('12.34') 12.34 >>> str(1.23) '1.23' >>> str(100) '100' >>> bool(1) True >>> bool('') False >>> abs(-1) 1 >>> max(1,2,3) 3原创 2021-06-21 14:14:38 · 128 阅读 · 0 评论 -
【07】dict和set
dict 相当于map,key-value对的存储容器,对于查找非常迅速 # 格式是大括号,key:value >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} # 通过key访问元素 >>> d['Bob'] 75 # 通过key增加元素 >>> d['Jhon'] = 99 >>> d {'Michael': 95, 'Bob': 75, 'Tracy': 85, 'Jhon': 9原创 2021-06-21 14:13:05 · 100 阅读 · 0 评论 -
【06】循环语句
for in 语句 names = ['Michael', 'Bob', 'Tracy'] for name in names: print(name) # 打印1-100数之和 # 利用range函数产生[0,100]的整数 sum = 0 for i in range(101): sum += i; print(sum) while 语句 def function: count = 10 while count > 0: pri原创 2021-06-21 14:12:10 · 52 阅读 · 0 评论 -
【05】条件判断
if语句 # if后注意有冒号 age = 22 if age > 18: print("age is", age) print("adult") if-else语句 # if 和 else 后有冒号 age = 22 if age > 18: print("age is", age) print("adult") else: print("age is", age) print("teenager") if-elif-else语句 age原创 2021-06-21 14:10:33 · 86 阅读 · 0 评论 -
【04】list和tuple
List python内置数据类型,一种有序集合容器,可以有重复元素, 有点像STL中的vector # 普通的list >>> classmate = ['Bob', 'Mike', 'Kate'] >>> classmate ['Bob', 'Mike', 'Kate'] # len()查看元素个数 >>> len(classmate) 3 # 下标访问内部元素,下标可以为负数 >>> classmate[0] 'Bob' &g原创 2021-06-21 14:08:01 · 48 阅读 · 0 评论 -
【03】字符串和编码
Unicode编码 内存中统一使用Unicode编码 但是为了解决空间,在存储文件、网络传输、网页展示时使用UTF-8编码 字符 ASCII Unicode UTF-8 A 01000001 01000001 01000001 01000001 中 无 01001110 00101101 11100100 10111000 10101101 python字符串 在最新的Python 3版本中,字符串是以Unicode编码的 字符串格式化输出 # 方式1,%分隔符 # %x 表示用原创 2021-06-21 14:06:17 · 80 阅读 · 0 评论 -
【02】数据类型和变量
整数 >>> a = 1 >>> a 1 >>> 1 1 >>> -1 -1 # 二进制 >>> 0b0011 3 # 八进制 >>> 0o127 87 # 十六进制 >>> 0xff0 4080 # 整数允许加下划线用于分隔 >>> 10_0000_0000 1000000000 >>> 0xff_446a 16729194 浮点数 # 普通原创 2021-06-21 14:05:23 · 50 阅读 · 0 评论 -
【01】输入输出
实例代码 # print函数,默认是带换行符的 >>> print("hello world") hello world # 默认添加空格 >>> print("hello", "world") hello world # +代表一个字符串 >>> print("hello"+"world") helloworld >>> print(10086) 10086 >>> name = input("Please inpu原创 2021-06-21 14:04:25 · 63 阅读 · 0 评论