北京品牌网站定制公司,济南网站APP,百度推广怎么赚钱,2022年最新血糖标准权威发布1.编程基础
1.1标识符
标识符是变量、函数、模块和其他对象的名称。Python中标识符的命名不是随意的#xff0c;而是要遵守一定的命名规则#xff0c;比如说: 1、标识符是由字母 (A~Z 和 a~z) 、下划线和数字组成#xff0c;但第一个字符不 能是数字。 2、标识符不…1.编程基础
1.1标识符
标识符是变量、函数、模块和其他对象的名称。Python中标识符的命名不是随意的而是要遵守一定的命名规则比如说: 1、标识符是由字母 (A~Z 和 a~z) 、下划线和数字组成但第一个字符不 能是数字。 2、标识符不能和 Python 中的保留字关键字相同。 3、Python 中的标识符中不能包含空格、、% 以及 $等特殊字符。 4、在 Python 中标识符中的字母是严格区分大小写的。 5、Python 语言中以下划线开头的标识符有特殊含义例如:_width、__add、__init__
1.2 关键字
False class from def if raise in True try
assert else with await finally or None and del
is while nonlocal continue global pass import as
elif return lambda async except yield break for not
1.3变量 greethahahagreet
hahahascore95.5score
95.5x10x
10x30x
30xTruex
True1.4 语句
Python代码是由关键字、标识符、表达式和语句等构成的语句是代码的重要组成部分。语句结尾的逗号加不加都可以。 greetHolle worldscore95.5abc10b
10score
95.51.5代码注释
#这是一个整形变量
score95
print(score)#打印score变量zzz
bbfdf
hahahahgesgs
....
sdvss#表示单行注释 表示多行注释 表示多行注释
1.6模块
模块就是一个包含了Python定义和声明的“.py”文件。 import模块名 from模块名 import代码元素 from模块名 import 代码元素 as代码元素别名 2.数字类型的数据
2.1 数据类型
Python中有6种主要的内置数据类型:数字、字符串、列表、元组、集合和字典 Python中有4种数字类型整数类型、浮点类型、复数类型和布尔类型
2.2 整数类型
Python中整数类型为int类 10
100o10 #八进制
80x0A #十六进制
100b1010 #二进制
10type(0x0a)
class int2.3 浮点类型
Python中浮点类型为float类 type(1.11)
class float2.4 复数类型
复数在数学中被表示为: abi其中a为实部b为虚部i为虚数单位。 在Python中虚数单位为j或者J。 c32jc
(32j)type(c)
class complexacomplex(3,2)a
(32j)2.5 布尔类型
Python中的布尔类型为bool类它只有两个值: True和False。 bool(None)
Falsebool(0)
Falsebool([])
Falsebool(2)
Truebool()
Falsebool( )
True
2.6 数字类型相互转换
2.6.1 隐式类型的转换
数字之间可以进行数学计算在进行数学计算时若数字类型不同则会发 生隐式类型的转换。 a1Truea
2a1.01a
2.0a1.0Falsea
1.0
2.6.2 显式类型的转换强制转换 aint(1.0)1a
2int(False)
0int(True)
1int(0.3)
0float(3)
3.0float(True)
1.03.运算符
3.1 算术运算符 3.2 比较运算符 3.3 逻辑运算符 3.4 位运算符 3.5 赋值运算符 4.程序流程控制
4.1 分支语句
4.1.1 if语句
if 条件表达式: 语句块
age5
if age3:print(hahaha)
4.1.2 if-else语句
if 条件表达式: 语句块1 else: 语句块2
u_nameinput(请输入用户名:)
pwdinput(请输入密码:)
if u_nameadmin and pwd123:print(登入成功!即将进入主界面...)
else:print(您输入的用户名或密码错误请重新输入...)4.1.3 if-elif-else语句
if 条件表达式1: 语句块1 elif条件表达式2: 语句块2 elif条件表达式n: 语句块n else: 语句块n1
scoreint(input(请输入您的会员积分:))
if score0:print(注册会员)
elif 0score200:print(铜牌会员)
elif 200score1000:print(银牌会员)
elif 1000score3000:print(金牌会员)
else:print(钻石会员)4.1.4 嵌套分支结构
if 条件表达式1: if条件表达式2: 语句块1 else: 语句块2 else: 语句块3
id_code input(请输入身份证号码:)
if len(id_code) 18:number int(id_code[-2])if number%2 0:print(女性)else:print(男性)
else:print(输入不合法)
4.2 循环语句
4.2.1 while循环
while 判断条件: 语句
# 输出1-100的所有数的和
count 0
num 0
while count 100:count count 1num num count
print(1-100的所有数的总和为: 和,num)#无限循环
while True:numint(input(请输入一个数字:))print(您输入的数字是:,num)
4.2.2 for循环
for 临时变量 in 可迭代对象: 执行语句1 执行语句2
#数组遍历
languages [c,Python,Java,C#]
for x in languages:print(当前语言是:,x)#输出[0~6),左开右闭包含0不包含6
for i in range(6):print(当前的数列为:,i)#输出[1~3),左开右闭包含1不包含3
for i in range(1,3): #范围从1开始不包含最后一个数字print(i)#输出九九乘法表
for i in range(1, 10):#遍历9次打印9行for j in range(1,10): #遍历9次打印9列的数据if j i: # 当列数行数的时候就可以打印乘法公式print(f{j}*{i}{j*i}.format(j, i),end\t)print(\n)
4.3 跳转语句
4.3.1 break语句
break跳出循环
#遇到l跳出循环
for s in Hello:if s l:breakprint(当前字母为 :, s)#判断质数
for n in range(2,10):for x in range(2,n):if n%x0:print(n,等于,x,*,n//x)breakelse: #当从内循环break跳出后不会执行else内的语句否则会执行print(n,是质数)
4.3.2 continue语句
continue跳过本次循环
#当x0跳过本次循环
for x in [0,-2,5,7,-10]:if x0:continueprint(x)
4.4 pass语句
pass作用是占位保持语句结构的完整相当于{}
x0
if x0:pass#作用是占位保持if语句结构的完整
5.容器类型数据
5.1 序列
在 Python 中序列类型包括字符串、列表、元组、集合和字典这些序列支持以下几种通用的操作但比较特殊的是集合和字典不支持索引、切片、相加和相乘操作。
5.1.1 序列索引操作 aHelloa[0]
Ha[-5]
Ha[5] #越界访问
Traceback (most recent call last):File pyshell, line 1, in module
IndexError: string index out of rangemax(a)
omin(a)
Hlen(a)
5
5.1.2 加和乘操作
注意要同类型才行 ahelloprint(a world)
hello worldprint(a world你好)
hello world你好print(a*2)
hellohello
5.1.3 切片操作
sname[start : end : step]
start :切割开始索引默认为0 end : 切割结束索引默认为数组最大长度 step切割步长默认为1. strHello worldprint(str[:2]) #注意是左闭右开[0,2)切割
Heprint(str[::2]) #step表示切割的步长默认为1
Hlowrdprint(str[:])
Hello world
5.1.4 成员测试
成员测试运算符有两个: in和not in。 strHelloe in str
TrueE not in str
True
5.2 列表
5.2.1 创建列表
创建列表的方式非常简单既可以使用中括号“[]”创建也可以使用内置的list()函数快速创建。 ①使用中括号“[]”创建列表
list_one[] #空列表
list_two[p,y,t , h , o , n]#列表中元素类型均为字符串类型
list_three[1,a,,2.3]#列表中元素类型不同
②使用list ()函数创建列表 list_onelist(1) #参数传递的对象必须是可迭代的
Traceback (most recent call last):File pyshell, line 1, in module
TypeError: int object is not iterablelist_twolist(python)list_two
[p, y, t, h, o, n]list_threelist([1,python])type(list_three)
class listlist_three
[1, python]from collections import Iterable #导入迭代器模块print(isinstance([],Iterable))
True
5.2.2 追加元素 list_onelist([1,2,3,4])
print(list_one)
list_one.append(10)
print(list_one)
lint_twolist([5,6])
list_one.extend(lint_two)
print(list_one)
lint_twolist_one
print(lint_two)
5.2.3 插入元素 names[zhang,li,wang]names.insert(2,xiaoqian) #在索引为2处插入names
[zhang, li, xiaoqian, wang]
5.2.4 替换元素 names
[zhang, li, xiaoqian, wang]names[1]lihuanames
[zhang, lihua, xiaoqian, wang]
5.2.5 删除元素
删除列表元素的常用方式有del语句、remove()方法和pop()方法
①del语句 names
[zhang, lihua, xiaoqian, wang]del names[0]names
[lihua, xiaoqian, wang]
②remove()方法元素替换只会替换第一个符合要求的元素 chars[h,e,l,l,o]print(chars)
[h, e, l, l, o]chars.remove(e)print(chars)
[h, l, l, o]
③pop()方法 numb[1,2,3,4,5,6]print(numb.pop())
6print(numb.pop(2))
3print(numb)
[1, 2, 4, 5]
5.3 元组
元组 (tuple) 是一种不可变序列类型。元组中的元素是不允许修改的除非在元组中包含可变类型的数据。
5.3.1 创建元组
①使用圆括号“()”创建元组
tuple_one()#空元组
tuple_two(t, e,p,l,e)#元组中元素类型均为字符串类型
tuple_three(0.3,1,python,)#元组中元素类型不同
②使用tuple()函数创建元组
tuple_nulltuple()
print(tuple_null)tuple_strtuple(abc)
print(tuple_str)tuple_listtuple([1,2,3])
print(tuple_list)结果%Run main.py
()
(a, b, c)
(1, 2, 3)5.3.2 元组拆包 val(10,121)a,bvala
10b
121
5.4 集合
集合集合 (set) 是一种可迭代的、无序的、不能包含重复元素的容器类型的数据。
5.4.1 创建集合
set([iterable]) 可变集合 frozenset([iterable]) 不可变集合
①可变集合的创建 set_oneset([1,2,3,4])set_twoset((1,2,3))type(set_one)
class settype(set_two)
class setset_one
{1, 2, 3, 4}set_three{1,21,2,11,44,21}set_three
{1, 2, 11, 44, 21} #集合元素是无序的#集合是元素唯一的set_oneset([1,2,3,1])set_one
{1, 2, 3}
②不可变集合的创建 frozenset_onefrozenset((1,2,3,4))frozenset_twofrozenset([1,2,3,4])type(frozenset_one)
class frozensetfrozenset_two
frozenset({1, 2, 3, 4})
5.4.2 修改集合
①添加元素 set_oneset()set_one
set()set_one.add(py) #添加一个元素set_one
{py}set_one.update(thon) #添加多个元素set_one
{h, n, t, py, o}
②删除元素 set_one{zhangsan,liai,wangwu,xiaoq}set_one.remove(zhangsan)set_one
{liai, xiaoq, wangwu}set_one.remove(zhangsan)
Traceback (most recent call last):File pyshell, line 1, in module
KeyError: zhangsanset_one.discard(zhangsan) #如果存在则删除set_one.discard(liai)set_one
{xiaoq, wangwu}set_one.pop() #随机删除一个元素
xiaoqset_one
{wangwu}
③清空元素 set_one{zhangsan,liai,wangwu,xiaoq}set_one.clear()set_one
set()
5.5 字典
字典 (dict) 是可迭代的、通过键 (key) 来访问元素的可变的容器类型的数据
5.5.1 创建字典
①使用花括号{}创建字典{键1:值1键2:值2...}
注意字典元素是无序的键和值成对出现键唯一值可以不唯一 scores{zhangsan:100,lisi:98,wangwu:45}scores
{zhangsan: 100, lisi: 98, wangwu: 45}scores_null{}type(scores)
class dictscores_null
{}type(scores_null)
class dict
②使用内置的dict ( 函数创建字典dict(键1值1,键2值2...) dict_onedict(namezhangsan,age20)dict_one
{name: zhangsan, age: 20}dict_twodict({100:zhangsan,101:lisi,102:wangwu})dict_two
{100: zhangsan, 101: lisi, 102: wangwu}dict_threedict(((100,zhangsan),(101,lisi)))dict_three
{100: zhangsan, 101: lisi}dict_fourdict([(100,zhangsan),(101,lisi)])dict_four
{100: zhangsan, 101: lisi}dict_fivedict([(100,zhangsan),(100,wangwu),(101,lisi)])dict_five
{100: wangwu, 101: lisi}
5.5.2 修改字典
①添加和修改字典元素 add_dict{stu1:zhangsan}add_dict.update(stu2lisi)add_dict
{stu1: zhangsan, stu2: lisi}add_dict[stu3]wangwuadd_dict
{stu1: zhangsan, stu2: lisi, stu3: wangwu}add_dict[stu1]xiaoqianadd_dict
{stu1: xiaoqian, stu2: lisi, stu3: wangwu}
②删除字典元素 dist_one{001:zhangsan,002:lisi,003:wangwu,004:xioaqian}dist_one.pop()
Traceback (most recent call last):File pyshell, line 1, in module
TypeError: pop expected at least 1 arguments, got 0dist_one.pop(001) #要传递参数键才能删除
zhangsandist_one
{002: lisi, 003: wangwu, 004: xioaqian}dist_one.popitem() #随机删除一对键值对
(004, xioaqian)dist_one
{002: lisi, 003: wangwu}dist_one.clear() #清空dist_one
{}
5.5.3 访问字典 dist_one{001:zhangsan,002:lisi,003:wangwu,004:xioaqian}print(info.items())
dict_items([(001, zhangsna), (002, lisi), (003, wangwu)])for i in info.items():print(i)(001, zhangsna)
(002, lisi)
(003, wangwu)print(info.keys())
dict_keys([001, 002, 003])for i in info.keys():print(i)001
002
003print(info.values())
dict_values([zhangsna, lisi, wangwu])for i in info.values():print(i)zhangsna
lisi
wangwu