网站建设实训考试,wordpress中文主题下载地址,个人网站设计欣赏,广州腾虎网络网站建设文章目录 基础语法标识符注释行与缩进多行语句获取控制台输入print输出import 与 from...import 基本数据类型布尔类型数字类型字符串列表(List)元组(Tuple)集合(Set)字典(Dictionary) 数据类型转换隐式类型转换显式类型转换 条件控制if 语句match...case 循环语句whilewhile循… 文章目录 基础语法标识符注释行与缩进多行语句获取控制台输入print输出import 与 from...import 基本数据类型布尔类型数字类型字符串列表(List)元组(Tuple)集合(Set)字典(Dictionary) 数据类型转换隐式类型转换显式类型转换 条件控制if 语句match...case 循环语句whilewhile循环使用elsefor语句for语句使用else 函数参数必须参数关键字参数默认参数不定长参数 基于Python3
基础语法
标识符
第一个字符必须是英文字母或下划线_。标识符的其他的部分由字母、数字和下划线组成。标识符对大小写敏感。
注释
Python中单行注释以#开头
# 第一个Python程序
print(Hello Python) # 这里也是注释多行注释可以用多个#号还有三个英文单引号 或者三个英文双引号
# 第一行注释
# 第二行注释
第三行注释
第四行注释第五行注释
第六行注释
print(Hello Python)行与缩进
Python使用缩进代表代码块不需要使用大括号{}。
同一个代码块的语句必须包含相同的缩进空格数
age 18
if age 18:print(age 18)print(成年)
else:print(age 18)print(未成年)如果同一个代码块语句缩进空格数不一样则会报错如下所示
age 18
if age 18:print(age 18)print(成年)
else:print(age 18)print(未成年)报错提示如下 File D:\pycode\test\test1.py, line 7print(未成年)^
IndentationError: unindent does not match any outer indentation level多行语句
如果一条语句太长可以使用反斜杠 \ 来连接多行语句
a 123
b 456
c 789
d a \b \c
print(d)获取控制台输入
name input(请输入姓名:)
print(name)print输出
print 默认输出是换行的如果要实现不换行需要在变量末尾加上 end
如果不换行并且在后面加上某个表示符号则使用end标识符
age 18
name lzc
# 换行打印到控制台
print(age)
print(name)
print(------)
# 不换行打印到控制台
print(age, end,)
print(name, end)import 与 from…import
用import或者from...import来导入相应的模块。将整个模块somemodule导入格式为import somemodule从某个模块中导入某个函数,格式为from somemodule import somefunction从某个模块中导入多个函数,格式为from somemodule import firstfunc, secondfunc, thirdfunc将某个模块中的全部函数导入格式为from somemodule import *
基本数据类型
Python中的变量不需要指定类型。每个变量在使用前都必须赋值变量赋值以后该变量才会被创建。
可以通过type()函数查看变量的类型
a, b, c 123, 6.66, True
print(type(a), type(b), type(c))输出内容如下
class int class float class boolPython3中常见的数据类型有数字、字符串、布尔类型、列表、元组、集合、字典
布尔类型
布尔类型即True、False
在Python中所有非零的数字和非空的字符串、列表、元组等数据类型都被视为True只有0、空字符串、空列表、空元组等被视为False。
布尔类型可以用来控制程序的流程比如判断某个条件是否成立或者在某个条件满足时执行某段代码。
a True
if a:print(aTrue)
else:print(aFalse)布尔值可以用and、or和not运算
a True
b False
print(a and b) # False
print(a or b) # True
print(not a) # False布尔值类型转换
a True
b False
print(int(a)) # 1
print(float(b)) # 0.0
print(str(a)) # True数字类型
Python3支持int、float、bool
Python3中bool是int的子类True和False可以和数字相加 True1、False0 会返回 True
在Python3里只有一种整数类型int表示为长整型没有python2中的Long
字符串
Python中的字符串用单引号或双引号括起来使用反斜杠\转义特殊字符。
a 123
b 456
c \\n789
print(a) # 输出 123
print(b) # 输出 456
print(c) # 输出 \n789字符串的截取语法格式变量[头下标:尾下标]遵循左闭右开原则。下标为负数则代表从尾部开始比如下标为-1则代表最后一个元素。 使用len()函数可以获取字符串长度。
a 123456789
print(a) # 输出整个字符串
print(a[0:-1]) # 输出第一个到倒数第二个的所有字符
print(a[0]) # 输出第一个字符
print(a[2:5]) # 输出第三个到第五个的所有字符
print(a[2:]) # 输出从第三个开始的所有字符
print(len(a)) # 输出字符串长度加号是字符串的连接符。星号*表示复制当前字符串与之结合的数字为复制的次数。
a 123456789
print(a 10) # 连接字符串
print(a * 2) # 输出字符串两次使用in和not in可以判断字符串是否包含给定的字符串
a 123456789
b 456
print(b in a) # True
print(b not in a) # False在Python中字符串格式化使用与C中sprintf函数一样的语法。
print(我叫%s今年%d岁浮点数保留两位小数%.2f指定整数位数(位数不够前面补0)%03d % (张三, 18, 3.1415926, 1))
# 我叫张三今年18岁浮点数保留两位小数3.14指定整数位数(位数不够前面补0)001常见的占位符
占位符替换内容%d整数%f浮点数%s字符串%x十六进制整数
Python三引号允许一个字符串跨多行字符串中可以包含换行符、制表符以及其他特殊字符。
a 姓名\t年龄
李华\t18print(a)列表(List)
列表是写在方括号[]之间、用逗号分隔开的元素列表。
和字符串一样列表同样可以被索引和截取列表被截取后返回一个包含所需元素的新列表。
列表的截取语法格式变量[头下标:尾下标]遵循左闭右开原则。下标为负数则代表从尾部开始比如下标为-1则代表最后一个元素。 使用len()函数可以获取列表长度。
a [111, 222, 333, 444, 555]
print(a) # 完整列表
print(a[0]) # 获取第一个元素
print(a[1:3]) # 获取第二个到第三个的所有元素
print(a[1:]) # 获取从第二个开始的所有元素
print(len(a)) # 获取列表长度列表常用方法
a [111, 222, 333, 333]
print(a) # [111, 222, 333, 333]# 在列表末尾添加新的对象
a.append(666)
print(a) # [111, 222, 333, 333, 666]# 删除第二个元素
del a[1]
print(a) # [111, 333, 333, 666]# 移除列表中某个值的第一个匹配项
a.remove(333)
print(a)# 判断元素是否存在于列表中
print(666 in a) # True
print(777 in a) # False列表遍历
a [111, 222, 333, 333]
for x in a:print(x)元组(Tuple)
元组(Tuple)与列表类似不同之处在于元组的元素不能修改。元组写在小括号()里元素之间用逗号隔开。元组中的元素类型也可以不相同。
a (123, 456, 123.12)
print(a)集合(Set)
集合中的元素不会重复并且可以进行交集、并集、差集等常见的集合操作。集合使用大括号{}表示元素之间用逗号,分隔。
a {123, 456, 789, 123}
print(a , a) # 重复的元素会被自动去掉
b {456, 666, 777}
print(a , b)
print(a - b : a和b的差集 , a - b)
print(a | b : a和b的并集 , a | b)
print(a b : a和b的交集 , a b)
print(a ^ b : a和b不同时存在的元素 , a ^ b)字典(Dictionary)
字典是一种映射类型字典用{ }标识它是一个无序的键(key):值(value)的集合。键(key)必须使用不可变类型。在同一个字典中键(key)必须是唯一的。
a {}
a[name] 张三
a[age] 18
print(a)
b {city: shenzhen}
print(b)输出内容如下
{name: 张三, age: 18}
{city: shenzhen}数据类型转换
对数据内置的类型进行转换一般情况下你只需要将数据类型作为函数名即可。 数据类型转换可以分为两种显式类型转换(需要使用类型函数来转换)、隐式类型转换(自动完成)
隐式类型转换
Python会自动将一种数据类型转换为另一种数据类型。 对两种不同类型的数据进行运算较低数据类型整数就会转换为较高数据类型浮点数以避免数据丢失。
num_int 123
num_float 1.23
num_new num_int num_float
print(datatype of num_int:, type(num_int))
print(datatype of num_float:, type(num_float))
print(value of num_new:, num_new)
print(datatype of num_new:, type(num_new))输出如下
datatype of num_int: class int
datatype of num_float: class float
value of num_new: 124.23
datatype of num_new: class float显式类型转换
使用int()、float()、str()函数来执行显式类型转换
int()强制转换为整形
x int(1)
y int(2.8)
z int(3)
print(x, type(x))
print(y, type(y))
print(z, type(z))输出如下
1 class int
2 class int
3 class intfloat()强制转换成浮点类型
x float(1)
y float(2.8)
z float(3)
w float(4.2)
print(x, type(x))
print(y, type(y))
print(z, type(z))
print(w, type(w))输出如下
1.0 class float
2.8 class float
3.0 class float
4.2 class floatstr()强制转换成字符类型
x str(s1)
y str(2)
z str(3.0)
print(x, type(x))
print(y, type(y))
print(z, type(z))输出如下
s1 class str
2 class str
3.0 class str条件控制
条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
条件控制中常用的操作运算符和逻辑运算符
操作符描述小于小于或等于大于大于或等于等于比较两个值是否相等!不等于and与or或not非
if 语句
一般形式如下
if condition_1:statement_block_1
elif condition_2:statement_block_2
else:statement_block_3score 80
if score 60:print(不及格)
elif score 60 and score 80: # 这里可以写成 60 score 80print(良)
else:print(优秀)match…case
Python 3.10 增加了match...case的条件判断
match subject:case pattern_1:action_1case pattern_2:action_2case pattern_3:action_3case _:action_wildcard例如
day 1
match day:case 6:print(星期六)case 7:print(星期日)case _: # 当其他 case 都无法匹配时匹配这条print(工作日)循环语句
while
num 1
while num 10:print(num)num num 1在Python中没有do..while循环。
while循环使用else
如果while后面的条件语句为false时则执行else的语句块。
num 1
while num 5:print(num, 小于等于5)num num 1
else:print(num, 大于5)for语句
for 循环可以遍历任何可迭代对象如一个列表或者一个字符串。
for variable in sequence:statements
else:statements例如
list [aaa, bbb,ccc,ddd]
for x in list:print(x)for语句使用else
for…else 语句用于在循环结束后执行一段代码
list [aaa, bbb,ccc,ddd]
for x in list:print(x)
else:print(循环介绍)函数
使用def关键字定义函数
def 函数名(参数列表):函数体使用函数计算两个数相加
def sum(a, b):return a b
c sum(10, 20)
print(c)参数
必须参数
def hello(name):print(name:, name)hello()不传参数会报错
Traceback (most recent call last):File D:\pycode\test\test1.py, line 4, in modulehello()
TypeError: hello() missing 1 required positional argument: name关键字参数
使用关键字参数允许函数调用时参数的顺序与声明时不一致
def hello(name, age):print(name:, name)print(age:, age)hello(age18, namexiaoli)输出如下
name: xiaoli
age: 18默认参数
调用函数时如果没有传递参数则会使用默认参数。
def hello(name, age 20):print(name:, name)print(age:, age)hello(age18, namexiaoli)
print(-------------------)
hello(wangwu)输出
name: xiaoli
age: 18
-------------------
name: wangwu
age: 20不定长参数
加了星号*的参数会以元组(tuple)的形式导入存放所有未命名的变量参数。
def hello(a, *b):print(a)print(b)hello(123, 456, 789)输出
123
(456, 789)加了两个星号**的参数会以字典的形式导入。
def hello(a, **b):print(a)print(b)hello(123, namexiaoli, age18)输出
123
{name: xiaoli, age: 18}