【Python基础】Python基础语法14个知识点大串讲
共 1107字,需浏览 3分钟
· 2020-08-18
![](https://filescdn.proginn.com/80c253cc06c4223173cc5015f24798f6/cd8c6576499b8af10af0734d6c20c94e.webp)
Python基础语法大串讲
1、print 函数
print("hello")
print("world!")
print("hello",end='')
print("world!")
2、变量与基本数据类型
int_var = 2
float_var = 3.13
str_var = "hello"
3、标识符
# 正确标识符
a_1 = 1
#错误标识符
1_a = 1
#直接使用中文标识符
变量1 = 5
print(变量1)
4、保留字
![](https://filescdn.proginn.com/d99d9f53b2fd750ea8c0a496262adc3f/f4da3f72338834c7596f709d91603d5b.webp)
5、数据类型
数字数据类型
var1 = 2 + 1.2j
var2 = complex(2,1.2)
布尔类型
T = True
F = False
6、基本运算
算数运算
![](https://filescdn.proginn.com/c34b75294bc5bc687f3b93490b23b769/ade66047e99b1e9db36ce9d06579dce4.webp)
比较运算
![](https://filescdn.proginn.com/382511436e3026a1ed62e457e70a92b3/b4db496331902f028c6d19313a88f53a.webp)
赋值运算
![](https://filescdn.proginn.com/192777a99d78f010f7829ab21d9d170c/8836672a37df0c13df2f2709b7136309.webp)
逻辑运算
![](https://filescdn.proginn.com/82386365f67c38e5f7ff8ad8decaeb5a/d09cf81c05fcb8d4e8e458d337415bdc.webp)
7、List 列表
List 列表介绍
list1 = [1,2,3,"hello",[4,5,6]]
print(list1)
列表元素访问
list2 = [1,2,3,4,5,6]
list2[0] #1
list2[-1] #6
list2[6] #访问越界!!
list3 = [1,2,3,4,5,6]
list3[0:3] #[1,2,3]
list3[::2] #[1,3,5]
8、Tuple 元组
tuple1 = (1,2,3,"hello",[4,5,6],(7,8,9))
print(tuple1)
tuple2 = (1,2,3,"hello",[4,5,6],(7,8,9))
print(tuple1[0]) #1
print(tuple1[-1]) #(7,8,9)
print(tuple1[-1][-1]) #9
![](https://filescdn.proginn.com/8e8c7fada357e361555d524e8744c9b9/d1dd6588ed0fc7229e20f7e94a90811a.webp)
9、Set 集合
set1 = {1,2,2,3}
print(set1)
set2 = {1,2,3}
set3 = {2,3,4}
3 in set2 #True
4 in set2 #False
set2 | set3 #{1,2,3,4,5}
set2 & set3 #{2}
10、Dictionary 字典
dict1 = {"name":"a","height":170,"weight":60}
dict1["height"] #170
dict1.keys() #['name','height','weight']
dict1.values() #['a',170,60]
dict1.clear() #{}
11、流程控制
if 条件语句
![](https://filescdn.proginn.com/221114ad433cd4218c964d94d7dfc307/d12bd8faa98be7d82ad36294247051fe.webp)
循环语句
while 循环
![](https://filescdn.proginn.com/cca7dda21c295677f0702f864b3ff72e/474d8c88e3e6f6563fbc3f4f4f8c6688.webp)
for 循环
![](https://filescdn.proginn.com/d3d1b883537a16bdb81c42220b520a0f/79c6a97618f528055e9b3ed6ec23ab5f.webp)
# 例:使用for和range来枚举列表中的元素
for i in range(10)
print(i)
12、列表推导式
list1 = [1,2,3]
list2 = [3,4,5]
[ x * y for x in list1 for y in list2]
[ x for x in list1 if 4 > x > 1] #[2,3]
13、函数
def functionname(parameters):
"函数_文档字符串"
function_suite
return [expression]
14、文件
#写文件
with open("a.txt","wt") as out_file:
out_file.write("写下要写的内容")
#读文件
with open("a.txt","rt") as in_file:
text = in_file.read()
print(text)
小结
往期精彩回顾
获取一折本站知识星球优惠券,复制链接直接打开:
https://t.zsxq.com/662nyZF
本站qq群1003271085。
加入微信群请扫码进群(如果是博士或者准备读博士请说明):