Python语法进阶
python优点
简单,易学,免费,开源,高层语言,可扩展性,可移植性,解释型语言,面向对象,丰富的库,规范的代码
变量的类型
标识符命名规则
-
小驼峰式命名法(lower camel case): 第一个单词以小写字母开始;第二个单词的首字母大写,例如:myName;
-
大驼峰式命名法(upper camel case): 每一个单字的首字母都采用大写字母,例如:FirstName;
-
下划线命名法:用下划线“_”来连接所有的单词,比如my_name。
如何在python中查找关键字
可以在Python Shell通过以下命令进行查看当前系统中python的关键字
import keyword
keyword.kwlist
常用的格式符号有哪些
格式符 | 转换 |
---|---|
%c | 字符 |
%s | 字符串 |
%d | 有符号十进制整数 |
%u | 无符号十进制整数 |
%f | 浮点数 |
算术运算符
运算符 | 描述 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
// | 取整除 |
% | 取余 |
** | 指数 |
python常用的数据类型转换
函数 | 说明 |
---|---|
int(x, [base]) | 将x转换为一个整数 |
float(x) | 将x转换为一个浮点数 |
str(x) | 将对象 x 转换为字符串 |
tuple(s) | 将序列 s 转换为一个元组 |
list(s ) | 将序列 s 转换为一个列表 |
python中的比较运算符
运算符 | 描述 | 示例 |
---|---|---|
== | 检查两个操作数的值是否相等,如果是则条件变为真。 | 如a=3,b=3,则(a == b) 为 True |
!= | 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 | 如a=1,b=3,则(a != b) 为 True |
> | 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 | 如a=7,b=3,则(a > b) 为 True |
< | 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 | 如a=7,b=3,则(a < b) 为 False |
>= | 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 | 如a=3,b=3,则(a >= b) 为 True |
<= | 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 | 如a=3,b=3,则(a <= b) 为 True |
逻辑运算符
运算符 | 逻辑表达式 | 描述 | 示例 |
---|---|---|---|
and | x and y | 布尔"与":如果 x 为 False,x and y 返回 False,否则它返回 y 的值。 | True and False, 返回 False。 |
or | x or y | 布尔"或":如果 x 是 True,它返回 True,否则它返回 y 的值。 | False or True, 返回 True。 |
not | not x | 布尔"非":如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 | not True 返回 False, not False 返回 True |
if-elif-else运用
score = 77
if score>=0 and score<60:
print('不及格')
elif score>=60 and score<90:
print('良')
else:
print('优')
等边三角形
for i in range(1, 6):
for j in range(1, 6 - i):
print(" ", end="")
print("* " * i)
9*9乘法表
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d*%d=%d " % (j, i, i * j), end='')
j += 1
print('\n')
i += 1
通过代码针对str=“abcdefg"进行如下操作:
str = "abcdefg"
# 1. 取出bcd
print(str[1:4])
# 2. 将str倒序
print(str[::-1])
# 3. 取出bdf
print(str[1::2])
通过代码针对mystr = “hello world python and python"进行如下操作:
mystr = "hello world python and python"
# 1.找到右边第一个p的索引
print(mystr.rfind('p'))
# 2.找z结果要求报错
print(mystr.index('z'))
# 3.将所有空格替换成逗点
print(mystr.replace(' ', ','))
# 4.通过逗点生成一个列表
print(mystr.split(' '))
# 5.将d全部替换成大写D
print(mystr.replace('d', 'D'))
通过代码针对a = [1,2,3,4,5,6]进行如下操作:
a = [1, 2, 3, 4, 5, 6]
# 1.在a中元素5前添加一个5.5元素
a.insert(4, 5.5)
# 2.在a中元素6后面添加"我心态炸了"
a.append('我心态炸了')
# 3.删除a中的元素1
a.remove(1)
print(a)
通过代码针对b = [“a”,“b”,“c”,“b”,“a”]进行如下操作:
b = ["a", "b", "c", "b", "a"]
# 1.查找索引区间在2-4的“b”的索引
a = b.index('b', 2, 4)
# 2.查找索引区间在3-4的“a”的索引
a = b.index('a', 3, 4)
print(a)
元组操作
tuple = ("xing", 23, 1.7)
tuple[0] = "ning"
print(tuple)
字典操作
dict = {'name': 'xing', 'age': 23, 'sex': '男'}
# 计算键值对的个数
print(len(dict))
#遍历字典的key
for key in dict.keys():
print(key)
# 遍历字典的value
for value in dict.values():
print(value)
# 遍历字典的item
for item in dict.items():
print(item)
# 删除字典中的第一个键所对应的数据
del dict['sex']
print(dict)
集合操作
使用discard删除 如果元素存在 直接删除 如果元素不存在 不做任何操作
交集(&)
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
new_set = set1 & set2
print(new_set)
并集(|)
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
new_set = set1 | set2
print(new_set)