当前位置:   article > 正文

Python 基本语法_class int

class int

打印 整型和字符串

  1. print(3) # 打印整型
  2. print('hello world') # 打印字符串

运行结果

  1. 3
  2. hello world

变量

  1. var = 10
  2. strVar = "hello world"
  3. bVar = True
  4. print(type(var))
  5. print(type(strVar))
  6. print(type(bVar))

运行结果

  1. <class 'int'>
  2. <class 'str'>
  3. <class 'bool'>

类型转换

  1. # 把整型转换成字符串
  2. print(str(20)) # 把整型 20 转换成字符串 "20"
  3. print(int("21")) # 把字符串 21 转换成 整型 21

运行结果

  1. 20
  2. 21

计算符号

  1. # ** 代表 幂
  2. print(10**2)

运行结果

100

列表

  1. # LIST 列表
  2. week = [] # 新建一个空列表
  3. print(type(week)) # 打印列表类型
  4. print(week) # 打印列表
  5. print("-----------------------------------")
  6. week.append("Monday") # 给列表中添加一个元素
  7. week.append("Tuesday")
  8. week.append("Wednesday")
  9. week.append("Thursday")
  10. week.append("Friday")
  11. week.append("Saturday")
  12. week.append("Sunday")
  13. print(week)
  14. print(week[0]) # 打印列表中的第一个元素,列表中的元素索引从0开始
  15. print(week[1])
  16. print("-----------------------------------")
  17. print(week[-1]) # 打印列表中的最后一个元素
  18. print(week[-2]) # 打印列表中的倒数第二个元素
  19. print("-----------------------------------")
  20. print(week[2:5]) # 取出第3个元素 到 第5个元素, 索引是 2 到 4
  21. print(week[3:]) # 取出从第3个元素 到 最后一个元素
  22. print("-----------------------------------")
  23. # 遍历列表
  24. for day in week:
  25. print(day)
  26. print("-----------------------------------")
  27. # 差看元素是否在列表中
  28. if "Thursday" in week:
  29. print("week contain Thursday")

运行结果

  1. <class 'list'>
  2. []
  3. -----------------------------------
  4. ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  5. Monday
  6. Tuesday
  7. -----------------------------------
  8. Sunday
  9. Saturday
  10. -----------------------------------
  11. ['Wednesday', 'Thursday', 'Friday']
  12. ['Thursday', 'Friday', 'Saturday', 'Sunday']
  13. -----------------------------------
  14. Monday
  15. Tuesday
  16. Wednesday
  17. Thursday
  18. Friday
  19. Saturday
  20. Sunday
  21. -----------------------------------
  22. week contain Thursday

列表嵌套

  1. # 列表嵌套
  2. hList = [["a","b","c","d"], ["1","2","3","4"]]
  3. # 打印列表
  4. print(hList)
  5. print("-----------------------------------")
  6. # 遍历链表
  7. for i in hList:
  8. print(i)
  9. print("-----------------------------------")
  10. # 遍历列表
  11. for i in hList:
  12. for j in i:
  13. print(j)

运行结果

  1. [['a', 'b', 'c', 'd'], ['1', '2', '3', '4']]
  2. -----------------------------------
  3. ['a', 'b', 'c', 'd']
  4. ['1', '2', '3', '4']
  5. a
  6. b
  7. c
  8. d
  9. 1
  10. 2
  11. 3
  12. 4

循环语句

  1. # 循环语句
  2. i=0
  3. while i < 3:
  4. i += 1
  5. print(i)
  6. print("-----------------------------------")
  7. for i in range(5): # range(5) 是从0 到 4 的5个元素
  8. print(i)

运行结果

  1. 1
  2. 2
  3. 3
  4. -----------------------------------
  5. 0
  6. 1
  7. 2
  8. 3
  9. 4

if 语句

  1. # if 语句
  2. A = 10
  3. B = A > 20
  4. if B:
  5. print("A > 20")
  6. else:
  7. print("A < 20")
  8. strA = "test1"
  9. if strA == "test1":
  10. print("strA is test1")
  11. hListA = ["1", "2"]
  12. hListB = ["1", "2"]
  13. if hListA == hListB:
  14. print("hListA == hListB")

运行结果

  1. A < 20
  2. strA is test1
  3. hListA == hListB

列表中查找元素

  1. # 列表中查找元素
  2. weeks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  3. for day in weeks:
  4. if day == "Wednesday":
  5. print("Wednesday exist")
  6. print("---------------------------")
  7. # 建议使用的方法
  8. if "Tuesday" in weeks:
  9. print("Tuesday exist")
  10. print("---------------------------")
  11. TuesdayFound = "Tuesday" in weeks
  12. print(TuesdayFound)

运行结果

  1. Wednesday exist
  2. ---------------------------
  3. Tuesday exist
  4. ---------------------------
  5. True
字典  
  1. # 列表中查找元素
  2. weeks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  3. for day in weeks:
  4. if day == "Wednesday":
  5. print("Wednesday exist")
  6. print("---------------------------")
  7. # 建议使用的方法
  8. if "Tuesday" in weeks:
  9. print("Tuesday exist")
  10. print("---------------------------")
  11. TuesdayFound = "Tuesday" in weeks
  12. print(TuesdayFound)
运行结果

Wednesday exist
---------------------------
Tuesday exist
---------------------------
True

  1. # 字典 Dictionaries
  2. students = ["xiaoming", "xiaohua", "zhangsan", "lisi"]
  3. scores = [60, 50, 70, 80]
  4. # 查找 xiaohua 的成绩
  5. indexes = [0,1,2,3]
  6. name = "xiaohua"
  7. score = 0
  8. for i in indexes:
  9. if students[i] == name:
  10. score = scores[i]
  11. print(score)
运行结果

50

  1. # 使用字典结构 key value
  2. scores = {}
  3. print(type(scores))
  4. scores["xiaoming"] = 60
  5. scores["xiaohua"] = 50
  6. scores["zhangsan"] = 70
  7. #打印字典 key 值
  8. print(scores.keys())
  9. # 打印字典
  10. print(scores)
  11. print(scores["xiaohua"])
运行结果
<class 'dict'>
dict_keys(['xiaoming', 'xiaohua', 'zhangsan'])
{'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70}
50
 
  1. # 使用字典结构 key value
  2. scores = {}
  3. print(type(scores))
  4. scores["xiaoming"] = 60
  5. scores["xiaohua"] = 50
  6. scores["zhangsan"] = 70
  7. #打印字典 key 值
  8. print(scores.keys())
  9. # 打印字典
  10. print(scores)
  11. print(scores["xiaohua"])
运行结果
<class 'dict'>
dict_keys(['xiaoming', 'xiaohua', 'zhangsan'])
{'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70}
50
  1. # 创建字典的另一种方法
  2. students = {
  3. "xiaoming":60,
  4. "xiaohua":50,
  5. "zhangsan":70
  6. }
  7. print(students)
  8. # 修改字典的值
  9. students["xiaohua"] = 95
  10. print(students)
  11. students["xiaohua"] = students["xiaohua"] - 10
  12. print(students)
  13. # 在字典中判断有没有值
  14. print("xiaohua" in students)
运行结果
{'xiaoming': 60, 'xiaohua': 50, 'zhangsan': 70}
{'xiaoming': 60, 'xiaohua': 95, 'zhangsan': 70}
{'xiaoming': 60, 'xiaohua': 85, 'zhangsan': 70}
True
  1. # 字典 用于统计
  2. fruits = ["apple", "orange", "grape", "apple", "orange", "apple", "tomato", "potato", "grape"]
  3. fruits_counts = {}
  4. for item in fruits:
  5. if item in fruits_counts:
  6. fruits_counts[item] = fruits_counts[item] + 1
  7. else:
  8. fruits_counts[item] = 1
  9. print(fruits_counts)
运行结果
{'apple': 3, 'orange': 2, 'grape': 2, 'tomato': 1, 'potato': 1}

文件操作

  1. # 文件读取 并显示出来
  2. f = open("test.txt", "r")
  3. g = f.read()
  4. print(g)
  5. f.close()
  1. # 文件写入
  2. f = open("test_w.txt", "w")
  3. f.write('12345678')
  4. f.write('\n')
  5. f.write('qwerrty')
  6. f.close()
test.csv


  1. test_data = []  
  2. f = open("test.csv", "r")  
  3. data = f.read()  
  4. rows = data.split('\n'
  5. print(rows)
  6. print("-------------------------------------------------")
  7. for row in rows:  
  8.     split_row = row.split(",")  
  9.     test_data.append(split_row)  
  10. print(test_data) 
  11. f.close()
  12. print("-------------------------------------------------")
  13. test = []
  14. for row in test_data:
  15.     test.append(row[1])
  16. 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 定义函数

  1. def printHello():
  2. print("hello!")
  3. def add(a,b):
  4. return a+b
  5. printHello()
  6. print(add(1,2))
运行结果

hello!
3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/886884
推荐阅读
相关标签
  

闽ICP备14008679号