数据集合(字符串,列表,元组,字典)
注释
块注释
“#”号后空一格,段落件用空行分开(同样需要“#”号)# 内容
行注释
至少使用两个空格和语句分开,注意不要使用无意义的注释x += 1 # 内容
文档注释
文档注释以 """ 开头和结尾, 首行不换行, 如有多行, 末行必需换行
"""Example docstrings. This module demonstrates documentation as specified by the `Google Python Style Guide`_. Docstrings may extend over multiple lines. Sections are created with a section header and a colon followed by a block of indented text. Section breaks are created by resuming unindented text. Section breaks are also implicitly created anytime a new section starts. """
简单的小程序
""" 用Python设计第一个游戏 """ import random # 导入库 counts = 3 # 3次机会 answer = random.randint(1, 10) # 设置答案是随机的 # Python里面的制表符很重要 while counts > 0: # 循环使最多只执行三次 temp = input("猜一个数字:") # 这里输出了""里面的内容,而且要求输入一个元素 guess = int(temp) # 把输入的元素转化为int类型。然后赋值到guess里面 if guess == answer: print("猜对了") print("但是没有奖励!") break else: if guess < answer: print("小啦~") else: print("大啦~") counts = counts - 1 print("The game is over!")
分支(if、else和elif)
>>> a = 3 >>> b = 5 >>> if a < b: small = a else: small = b >>> print(small) 3 >>> a = 3 >>> b = 5 >>> small = a if a < b else b >>> print(small) 3
score = input("请输入你的分数:") score = int(score) if 0 <= score < 60: print("D") elif 60 <= score < 80: print("C") elif 80 <= score < 90: print("B") elif 90 <= score < 100: print("A") elif score == 100: print("S")
两种循环结构的比较
>>> for each in "CHina!": print(each) C H i n a ! >>> i = 0 >>> while i < len("CHina!"): print("CHina!"[i]) i += 1 C H i n a !
range
range(stop) [默认开始为0]
range(start, stop)
range(start, stop, step)
>>> sum = 0 >>> for i in range(1, 100, 2): sum += i >>> print(sum) 2500
>>> for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, "=", x, "*", n // x) break else: print(n, "是一个素数") 2 是一个素数 3 是一个素数 4 = 2 * 2 5 是一个素数 6 = 2 * 3 7 是一个素数 8 = 2 * 4 9 = 3 * 3
序列
>>> zifuchuan = [1, 2, 3, 4, 5, "中国", "China"] >>> print(zifuchuan) [1, 2, 3, 4, 5, '中国', 'China'] >>> for each in zifuchuan: print(each) 1 2 3 4 5 中国 China >>> length = len(zifuchuan) >>> zifuchuan[-1] 'China' >>> zifuchuan[-2] '中国' >>> zifuchuan[0] 1 >>> zifuchuan[-7] 1 >>> zifuchuan[length - 1] 'China' >>> zifuchuan[3:6] [4, 5, '中国'] >>> zifuchuan[1::2] [2, 4, '中国'] >>> zifuchuan[::2] [1, 3, 5, 'China'] >>> # 类似于range函数
>>> heros = ["Ironman", "greenman"] >>> heros.append("black window") >>> heros ['Ironman', 'greenman', 'black window'] >>> heros.extend(["鹰眼", "灭霸", "雷神"]) >>> heros ['Ironman', 'greenman', 'black window', '鹰眼', '灭霸', '雷神'] >>> s = [1, 2, 3, 4, 5] >>> s[len(s):] = [6] >>> s [1, 2, 3, 4, 5, 6] >>> s[len(s):] = [7, 8, 9] # == s.extend([7, 8, 9]) >>> s [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> s = [1, 3, 4, 5] >>> s.insert(1, 2) >>> s [1, 2, 3, 4, 5] >>> s.insert(0, 0) >>> s [0, 1, 2, 3, 4, 5] >>> s.insert(len(s), 6) >>> s [0, 1, 2, 3, 4, 5, 6] >>> heros.remove("灭霸") >>> heros ['Ironman', 'greenman', 'black window', '鹰眼', '雷神'] # 1如果存在多个匹配的元素,只会删除第一个(下标小的) # 2如果指定元素不存在,那么程序报错 >>> heros.pop(2) 'black window' >>> heros ['Ironman', 'greenman', '鹰眼', '雷神'] >>> heros.clear() >>> heros [] >>> heros = ["spiderman", "greenman", "bwidow", "greeneye", "mieba", "leishen"] >>> heros[3:] = ["武松", "林冲", "李逵"] >>> heros ['spiderman', 'greenman', 'bwidow', '武松', '林冲', '李逵'] >>> nums = [3, 1, 9, 6, 2, 3, 5 ,7] >>> nums.sort() >>> nums [1, 2, 3, 3, 5, 6, 7, 9] >>> nums.reverse() >>> nums [9, 7, 6, 5, 3, 3, 2, 1] >>> heros.reverse <built-in method reverse of list object at 0x0000018792CFA300> >>> heros.reverse() >>> heros ['李逵', '林冲', '武松', 'bwidow', 'greenman', 'spiderman'] >>> heros.sort() >>> heros ['bwidow', 'greenman', 'spiderman', '李逵', '林冲', '武松'] >>> nums = [3, 1, 9, 6, 2, 3, 5 ,7] >>> nums.sort(reverse = True) >>> nums [9, 7, 6, 5, 3, 3, 2, 1] >>> nums.count(3) 2 >>> heros.index("greenman") 1 >>> heros[heros.index("greenman")] = "why" >>> heros ['bwidow', 'why', 'spiderman', '李逵', '林冲', '武松'] >>> nums.index(3, 1 ,7) 4 >>> num = nums.copy() >>> num [9, 7, 6, 5, 3, 3, 2, 1] >>> num.sort() >>> num [1, 2, 3, 3, 5, 6, 7, 9] >>> num2 = nums[:] >>> num2 [9, 7, 6, 5, 3, 3, 2, 1]
序列:在python当中 序列就是一组按照顺序排列的值【数据集合】
在python中 存在三种内置的序列类型:
字符串、列表、元组
优点:可以支持索引和切片的操作
特征:第一个正索引为0,指向的是左端,第一个索引为负数的时候,指向的是右端
切片:【高级特性】可以根据下表来获取序列对象的任意[部分]数据
语法结构:[start:end:step] step默认1
list:python当中非常重要的数据结构,是一种有序的数据集合
特点:
1:支持增删改查
2:列表中的数据是可以变化的【数据项可以变化,内存地址不会改变】
3:用[] 来表示列表类型,数据项之间用逗号来分割,注意:数据项可以是任何类型的数据
4:支持索引和切片来进行操作
元组:是一种不可变的序列,在创建之后不能做任何的修改
1:不可变
2:用()创建元组类型,数据项用逗号来分割
3:可以是任何的类型
4:当元组中只有一个元素时,要加上逗号,不然后解释器会当做其他类型来处理
5:同样可是支持切片操作
字典:也是python中重要的数据类型,字典是有 键值对 组成的集合,通常使用 键来访问数据,效率非常高,和list一样 支持对数据的添加、修改、删除
特点:
1:不是序列类型 没有下标的概念,是一个无序的 键值集合,是内置的高级数据类型
2:用{} 来表示字典对象,每个键值对用逗号分隔
3:键 必须是不可变的类型【元组、字符串】 值可以是任意的类型
4: 每个键必定是惟一的,如果存在重复的键,后者会覆盖前者