首页 > 代码库 > Python 基础

Python 基础

了解python基本语法 尝试写简单的python程序

 1 count = 0
 2 age_to_guess = 23
 3 
 4 while count<3:
 5     age_guessed = int(input("age:"))
 6     if age_guessed == age_to_guess:
 7         print("congratulations")
 8         break
 9     elif age_guessed > age_to_guess:
10         print("Too big")
11     else:
12         print("Too small")
13     count = count + 1
14     if count == 3:
15         confirm_input = input("Do you want to play more?")
16         if confirm_input != "n":
17             count = 0

 

1 age_to_guess = 23
2 age_guessed = int(input("age:"))
3 
4 if age_guessed == age_to_guess :
5     print("Congratulations")
6 elif age_guessed > age_to_guess:
7     print("Too big")
8 else:
9     print("Too small")

 

 1 import sys
 2 
 3 ‘‘‘
 4 print(sys.path) #打印环境变量
 5 print(sys.argv) #相对路径 可在后面加参数
 6 ‘‘‘
 7 
 8 #操作系统模块
 9 import os
10 
11 cmd_result = os.system("dir") #执行命令 不保存结果
12 print(cmd_result) #输出0 为什么? os.system是直接在频幕上输出 没返回
13 
14 cmd_result_internal_storage = os.popen("dir") #打开的是一个内存地址
15 print(cmd_result_internal_storage)
16 cmd_result_content = os.popen("dir").read() #读操作
17 print(cmd_result_content)
18 
19 os.mkdir("new_dir") #创建目录
20 
21 msg = "我爱北京天安门"
22 
23 msg_bytes= msg.encode("utf-8")
24 
25 print(msg_bytes)
26 
27 msg_str = msg_bytes.decode("utf-8")
28 
29 print(msg_str)

 python list 用法(增删改查):

 1 #author F
 2 
 3 names = ["num1", "num2", "num3", "num4", "num3"]
 4 
 5 print(names[0])
 6 print(names[2])
 7 print(names[1:3]) #左顾右盼 从左边到右边 不包括右边
 8 print(names[2:])
 9 print(names[:-1])
10 print(names[-3:-1]) #左边必须小于右边 左边是开始取值的地方 右边是结束取值的地方
11 
12 #
13 names.append("num10086") #在后面追加
14 print(names)
15 
16 names.insert(2, "num_")
17 print(names)
18 
19 names.insert(4, "_num")
20 print(names)
21 
22 #
23 names[2] = "change2"
24 print(names)
25 
26 #
27 names.remove("change2")
28 print(names)
29 del names[3]
30 print(names)
31 names.pop() #弹出 输入下标->删下标单位 默认->最后一个
32 print(names)
33 
34 #
35 print(names.index("num3"))
36 print(names[names.index("num3")])
37 print(names.count("num3"))
38 names.reverse() #反转
39 print(names)
40 names.sort() #按照字母顺序排序 特殊符号>数字>大写>小写 ascii码
41 print(names)
42 names2 = [1, 2, 3, 4]
43 names.extend(names2)
44 print(names)
45 del names2
46 # print(names2) #未定义
47 
48 
49 #清空
50 names.clear()
51 print(names)
执行结果:
 1 D:\python\python\python.exe D:/xampp/htdocs/python/day2/list_cut.py
 2 num1
 3 num3
 4 [num2, num3]
 5 [num3, num4, num3]
 6 [num1, num2, num3, num4]
 7 [num3, num4]
 8 [num1, num2, num3, num4, num3, num10086]
 9 [num1, num2, num_, num3, num4, num3, num10086]
10 [num1, num2, num_, num3, _num, num4, num3, num10086]
11 [num1, num2, change2, num3, _num, num4, num3, num10086]
12 [num1, num2, num3, _num, num4, num3, num10086]
13 [num1, num2, num3, num4, num3, num10086]
14 [num1, num2, num3, num4, num3]
15 2
16 num3
17 2
18 [num3, num4, num3, num2, num1]
19 [num1, num2, num3, num3, num4]
20 [num1, num2, num3, num3, num4, 1, 2, 3, 4]
21 []
22 
23 进程已结束,退出代码0

 python list 用法(复制 循环):

 1 #author F
 2 import copy
 3 
 4 
 5 #copy 列表浅复制
 6 
 7 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"]
 8 names2 = names.copy() #浅copy:只copy第一层地址 原因:复制的第二层列表的指针地址
 9 print(names)
10 print(names2)
11 
12 names[1] = "name_change"
13 # print(names)
14 # print(names2)
15 
16 names[3][1] = "BLILI"
17 print(names)
18 print(names2)
19 
20 ‘‘‘
21 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
22 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
23 [‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]
24 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]
25 ‘‘‘
26 
27 # = 列表复制
28 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"]
29 names2 = names
30 print(names)
31 print(names2)
32 
33 names[1] = "name_change"
34 # print(names)
35 # print(names2)
36 
37 names[3][1] = "BLILI"
38 print(names)
39 print(names2)
40 ‘‘‘
41 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
42 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
43 [‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]
44 [‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]
45 ‘‘‘
46 
47 #  模块复制
48 names = ["name1", "name2", "name3", ["alibaba", "blili"], "name4", "name5"]
49 names2 = copy.deepcopy(names) #深copy : 会占用两份独立的内存空间 慎用
50 print(names)
51 print(names2)
52 
53 names[1] = "name_change"
54 # print(names)
55 # print(names2)
56 
57 names[3][1] = "BLILI"
58 print(names)
59 print(names2)
60 ‘‘‘
61 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
62 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
63 [‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]
64 [‘name1‘, ‘name2‘, ‘name3‘, [‘alibaba‘, ‘blili‘], ‘name4‘, ‘name5‘]
65 ‘‘‘
66 
67 #循环
68 for i in names:
69     print(i)
70 
71 print(names[0:-1:1])  #[‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘]
72 print(names[0::1])  #[‘name1‘, ‘name_change‘, ‘name3‘, [‘alibaba‘, ‘BLILI‘], ‘name4‘, ‘name5‘]

 元组 浅copy补充

 1 person = ["name", ["saving", 123]]
 2 #浅copy的三种实现方式
 3 ‘‘‘
 4 p1 = copy.copy(person)
 5 p2 = person[:]
 6 p3 = list(person)
 7 ‘‘‘
 8 p1 = person[:]
 9 p2 = person[:]
10 
11 p1[0] = ale1
12 p2[0] = feng
13 
14 p1[1][1] = 50
15 
16 print(p1) #联合账号的用法
17 
18 #元组
19 names = (231,  21314)
20 # 元组只有index和count方法

 

Python 基础