打印 整型和字符串
print(3) # 打印整型
print('hello world') # 打印字符串
运行结果
3
hello world
变量
var = 10
strVar = "hello world"
bVar = True
print(type(var))
print(type(strVar))
print(type(bVar))
运行结果
<class 'int'>
<class 'str'>
<class 'bool'>
类型转换
# 把整型转换成字符串
print(str(20)) # 把整型 20 转换成字符串 "20"
print(int("21")) # 把字符串 21 转换成 整型 21
运行结果
20
21
计算符号
# ** 代表 幂
print(10**2)
运行结果
100
列表
# LIST 列表
week = [] # 新建一个空列表
print(type(week)) # 打印列表类型
print(week) # 打印列表
print("-----------------------------------")
week.append("Monday") # 给列表中添加一个元素
week.append("Tuesday")
week.append("Wednesday")
week.append("Thursday")
week.append("Friday")
week.append("Saturday")
week.append("Sunday")
print(week)
print(week[0]) # 打印列表中的第一个元素,列表中的元素索引从0开始
print(week[1])
print("-----------------------------------")
print(week[-1]) # 打印列表中的最后一个元素
print(week[-2]) # 打印列表中的倒数第二个元素
print("-----------------------------------")
print(week[2:5]) # 取出第3个元素 到 第5个元素, 索引是 2 到 4
print(week[3:]) # 取出从第3个元素 到 最后一个元素
print("-----------------------------------")
# 遍历列表
for day in week:
print(day)
print("-----------------------------------")
# 差看元素是否在列表中
if "Thursday" in week:
print("week contain Thursday")
运行结果
<class 'list'>
[]
-----------------------------------
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Monday
Tuesday
-----------------------------------
Sunday
Saturday
-----------------------------------
['Wednesday', 'Thursday', 'Friday']
['Thursday', 'Friday', 'Saturday', 'Sunday']
-----------------------------------
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
-----------------------------------
week contain Thursday
列表嵌套
# 列表嵌套
hList = [["a","b","c","d"], ["1","2","3","4"]]
# 打印列表
print(hList)
print("-----------------------------------")
# 遍历链表
for i in hList:
print(i)
print("-----------------------------------")
# 遍历列表
for i in hList:
for j in i:
print(j)
运行结果
[['a', 'b', 'c', 'd'], ['1', '2', '3', '4']]
-----------------------------------
['a', 'b', 'c', 'd']
['1', '2', '3', '4']
a
b
c
d
1
2
3
4
循环语句
# 循环语句
i=0
while i < 3:
i += 1
print(i)
print("-----------------------------------")
for i in range(5): # range(5) 是从0 到 4 的5个元素
print(i)
运行结果
1
2
3
-----------------------------------
0
1
2
3
4
if 语句
# if 语句
A = 10
B = A > 20
if B:
print("A > 20")
else:
print("A < 20")
strA = "test1"
if strA == "test1":
print("strA is test1")
hListA = ["1", "2"]
hListB = ["1", "2"]
if hListA == hListB:
print("hListA == hListB")
运行结果
A < 20
strA is test1
hListA == hListB
列表中查找元素
# 列表中查找元素
weeks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in weeks:
if day == "Wednesday":
print("Wednesday exist")
print("---------------------------")
# 建议使用的方法
if "Tuesday" in weeks:
print("Tuesday exist")
print("---------------------------")
TuesdayFound = "Tuesday" in weeks
print(TuesdayFound)
运行结果
Wednesday exist
---------------------------
Tuesday exist
---------------------------
True
字典
# 列表中查找元素
weeks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in weeks:
if day == "Wednesday":
print("Wednesday exist")
print("---------------------------")
# 建议使用的方法
if "Tuesday" in weeks:
print("Tuesday exist")
print("---------------------------")
TuesdayFound = "Tuesday" in weeks
print(TuesdayFound)
运行结果
Wednesday exist
---------------------------
Tuesday exist
---------------------------
True
# 字典 Dictionaries
students = ["xiaoming", "xiaohua", "zhangsan", "lisi"]
scores = [60, 50, 70, 80]
# 查找 xiaohua 的成绩
indexes = [0,1,2,3]
name = "xiaohua"
score = 0
for i in indexes:
if students[i] == name:
score = scores[i]
print(score)
运行结果
50
# 使用字典结构 key value
scores = {}
print(type(scores))
scores["xiaoming"] = 60
scores["xiaohua"] = 50
scores["zhangsan"] = 70
#打印字典 key 值
print(scores.keys())
# 打印字典
print(scores)
print(scores["xiaohua"])
运行结果 <class 'dict'> dict_keys(['xiaoming', 'xiaohua', 'zhangsan']) {'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70} 50
# 使用字典结构 key value
scores = {}
print(type(scores))
scores["xiaoming"] = 60
scores["xiaohua"] = 50
scores["zhangsan"] = 70
#打印字典 key 值
print(scores.keys())
# 打印字典
print(scores)
print(scores["xiaohua"])
运行结果
<class 'dict'> dict_keys(['xiaoming', 'xiaohua', 'zhangsan']) {'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70} 50
# 创建字典的另一种方法
students = {
"xiaoming":60,
"xiaohua":50,
"zhangsan":70
}
print(students)
# 修改字典的值
students["xiaohua"] = 95
print(students)
students["xiaohua"] = students["xiaohua"] - 10
print(students)
# 在字典中判断有没有值
print("xiaohua" in students)
运行结果
{'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70} {'xiaoming': 60, 'xiaohua': 95, 'zhangsan': 70} {'xiaoming': 60, 'xiaohua': 85, 'zhangsan': 70} True
# 字典 用于统计
fruits = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"]
fruits_counts = {}
for item in fruits:
if item in fruits_counts:
fruits_counts[item] = fruits_counts[item] + 1
else:
fruits_counts[item] = 1
print(fruits_counts)
运行结果
{'apple': 3, 'orange': 2, 'grape': 2, 'tomato': 1, 'potato': 1}
文件操作
# 文件读取 并显示出来
f = open("test.txt", "r")
g = f.read()
print(g)
f.close()
# 文件写入
f = open("test_w.txt", "w")
f.write('12345678')
f.write('\n')
f.write('qwerrty')
f.close()
test.csv
test_data = []
f = open("test.csv", "r")
data = f.read()
rows = data.split('\n')
print(rows)
print("-------------------------------------------------")
for row in rows:
split_row = row.split(",")
test_data.append(split_row)
print(test_data)
f.close()
print("-------------------------------------------------")
test = []
for row in test_data:
test.append(row[1])
print(test)
运行结果
['1,Sunny', '2,Sunny', '3,Sunny', '4,Sunny', '5,Sunny', '6,Rain', '7,Sunny', '8,Sunny', '9,Fog', '10,Rain'] ------------------------------------------------- [['1', 'Sunny'], ['2', 'Sunny'], ['3', 'Sunny'], ['4', 'Sunny'], ['5', 'Sunny'], ['6', 'Rain'], ['7', 'Sunny'], ['8', 'Sunny'], ['9', 'Fog'], ['10', 'Rain']] ------------------------------------------------- ['Sunny', 'Sunny', 'Sunny', 'Sunny', 'Sunny', 'Rain', 'Sunny', 'Sunny', 'Fog', 'Rain']
函数
在python 中用 def 定义函数
def printHello():
print("hello!")
def add(a,b):
return a+b
printHello()
print(add(1,2))
运行结果
hello! 3