长大后想做什么?做回小孩!

0%

初识python

目标:一周学会python3的使用。

目的:为后序机器学习打基础。

简介

python是一门解释型的面向对象的高级语言,逐行执行。

后缀名:

.py — python源文件

.pyc — python字节码文件 c:code-byte

.pyo — python优化文件 o:optimizing

基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#单行注释

"""
多行注释
"""

#基本输出函数 print(value1,value2,...)
a = 100 #python变量名区分大小写
print()#空行
print(1,2,3) #1 2 3
print(a) #100
print("\np\ny\nt\nh\no\nn\n") #同下
print("""
p
y
t
h
o
n
""")

#流程控制
num = 19
if num >= 20:
print(">=20")
else:
print("<20") # <20

score = 93
if score >= 100:
print("[100,无穷)")
elif score >95:
print("(95,99]")
else:
print("(-无穷,95]") # (-无穷,95]

for i in range(5):
print(i) # 0 1 2 3 4
for i in range(1,5):
print(i) # 1 2 3 4
for i in range(1,5,2):
print(i) # 1 3

n = 1
while(n<5):
print(n)
n += 1
else:
print("100") # 1 2 3 4 100

for n in range(1,10):
for m in range(1,n+1):
print(f"{n}*{m}={n*m}",end=" ") #乘法口诀
print()

n = 1
while n<=9:
m = 1
while m<=n:
print(f"{m}*{n}={m*n}",end=" ") #乘法口诀
m+=1
n+=1
print()

#break 结束整个循环
for i in [1,2,3,4,5]:
if i == 3:
break
print(i) #1 2

#continue 跳过本层循环
for i in [1,2,3,4,5]:
if i == 3:
continue
print(i) #1 2 4 5

猜数字

在1~100之间随机产生一个数字num,用户输入一个数字ans,提示用户猜测的ans和目标num的大小关系,直至准确猜中位置。

1
2
3
4
5
6
7
8
9
10
num = random.randint(1,100)
while True:
ans = int(input("输入1~100:"))
if ans == num:
print("bingo!")
break
elif ans > num:
print("大了!")
else:
print("小了!")

变量

字符串

单引号 ==‘ ’== 或者双引号 ==“ ”==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#idx  0  1  2  3  4
#-idx -5 -4 -3 -2 -1
#val 1 2 3 4 5
s = "12345"
print(s[0]," ",s[1]," ",s[2]," ",s[3]," ",s[4]," ") # 1 2 3 4 5
print(s[-1]," ",s[-2]," ",s[-3]," ",s[-4]," ",s[-5]," ") # 5 4 3 2 1

#切片操作 name[起始(含):结束(不含):步长]
print(s[0:5:2]) #135

name1 = "张三"
name2 = "李四"
print("{}和{}".format(name1,name2)) #同下
print(f"{name1}{name2}") #张三和李四
name = name1 + name2
print(name) #张三李四 字符串之间可以通过 "+" 连接

列表

用一对 ==[]== 表示列表,是一个含有多个元素的序列。

1
2
3
4
5
6
7
8
9
list = [1,"abc",[2,3],4]
print(list) # [1, 'abc', [2, 3], 4]
list.append(5) #在末尾追加
list.insert(1,"insert") #在1号索引插入
list.extend("python") #扩充list,参数必须是一个可迭代的序列
list.pop() #弹出末尾元素
list.pop(6) #弹出6号索引元素
list.remove("o") #删除元素"o"
print(list) #[1, 'insert', 'abc', [2, 3], 4, 5, 'y', 't', 'h']

元组

用一对 ==()== 表示元组,是一个含有多个元素的不可变序列。

不支持对元素赋值的操作,但是支持切片操作,切片后仍然是元组。

1
2
3
4
my_tuple = (1,"abc",[2,3],4)
print(my_tuple) # (1, 'abc', [2, 3], 4)
my_tuple[1] = 1 #TypeError: 'tuple' object does not support item assignment
print(my_tuple[0:4:2]) # (1, [2, 3])

字典

用一对 =={}== 表示字典,是若干组键值对的集合。

1
2
3
4
5
6
7
8
user = {
'name':'tom',
"age":23,
111:222
}
user["age"] = "24"
user["gender"] = "male"
print(user) #{'name': 'tom', 'age': '24', 111: 222, 'gender': 'male'}

函数

1
2
3
4
5
6
7
8
9
10
def sum(n,m):
res = 0
while n<=m:
res+=n;
n+=1;
return res

print(sum(1,100)) #5050
print(sum(50,100)) #3825
print(sum(1,3)) #6

文件操作

1
2
3
4
5
6
7
8
f = open("hi.txt",mode="r",encoding="utf-8")
s = f.read()
print(s)

f = open("new.txt",mode="w",encoding="utf-8")
f.write("人生苦短\n")
f.write("我用python")
f.close()

面向对象

1
2
3
4
5
6
7
8
9
10
11
12
13
class Person:
def __init__(self,name,gender,birthday):
self.name = name
self.gender = gender
self.birthday = birthday

def say_hi(self,word):
print(f"{self.name}说:hi,{word}")

zhangsan = Person("张三","男","1998-11-23")
zhangsan.say_hi("学python么") #张三说:hi,学python么
lisi = Person("李四","女","1999-07-23")
lisi.say_hi("不学") #李四说:hi,不学

《HelloWorld》完结,接下来是通过《学生管理》练习基本语法和熟悉常用的包。


菜鸟本菜,不吝赐教,感激不尽!

更多题解源码和学习笔记:githubCSDNM1ng