目录
[# 输出直接print就行了](# 输出直接print就行了)
[# 次方,除法,取整](# 次方,除法,取整)
[# 定义变量直接写就可以,不用写类型](# 定义变量直接写就可以,不用写类型)
[# 基础的while不用写()和{},直接用冒号即可,缩进对齐](# 基础的while不用写()和{},直接用冒号即可,缩进对齐)
[# 这里的for循环直接用in就可以,意思是从...中一个一个取出来值放在i中](# 这里的for循环直接用in就可以,意思是从...中一个一个取出来值放在i中)
[# 可以连续比较,elseif改为elif](# 可以连续比较,elseif改为elif)
[# TODO 定义函数](# TODO 定义函数)
[# 读写文件](# 读写文件)
[# 类的定义,类的属性在init里面写出来就行了# 类里面的函数要引用自己的属性的时候要用self](# 类的定义,类的属性在init里面写出来就行了# 类里面的函数要引用自己的属性的时候要用self)
[# 输入input(返回值是String)](# 输入input(返回值是String))
[# 元组和列表](# 元组和列表)
[# import引入模块](# import引入模块)
[# continue AND break 跳过本次循环 和 跳出循环](# continue AND break 跳过本次循环 和 跳出循环)
[# 错误处理](# 错误处理)
[# zip 合并元素放在一起,组成元组# lamda 简单的def函数# map 函数+参数一起输入访问,参数是一个一个的放入的](# zip 合并元素放在一起,组成元组# lamda 简单的def函数# map 函数+参数一起输入访问,参数是一个一个的放入的)
[# copy](# copy)
[# pickle 存储之前的计算结果](# pickle 存储之前的计算结果)
[# set集合 去重](# set集合 去重)
[# 正则表达式](# 正则表达式)
# 输出直接print就行了
python
print("你好")
# 次方,除法,取整
python
print(2**3)
print(9/4)
print(9//4)
# 定义变量直接写就可以,不用写类型
python
apple=10
print(apple)
# 基础的while不用写()和{},直接用冒号即可,缩进对齐
python
while apple>0:
apple = apple - 1
print(apple)
# 这里的for循环直接用in就可以,意思是从...中一个一个取出来值放在i中
python
example_list=[1,2,3,4,5,6,7,8,9]
for i in example_list:
print(i)
# 这种是for循环的计数
for i in range(10):
print("yhb\n")
# 可以连续比较,elseif改为elif
python
x,y,z=2,3,4
if x<y<z:
print("True")
elif x>y>z:
print("False")
else:
print("False")
# TODO 定义函数
python
def function(x,y):#没有默认值的参数不能放在有默认值的参数的后面
print("This is a function.")
return x+y
print(function(1,2))
a=None
# TODO global其实际上是函数实参和形参之间的桥梁,相当于传进去的就是这个全局的a
def fun():
global a
a=20
a=a+100
return a
print("begin a=",a)
print(fun())
print("end a=",a)
# 读写文件
python
text ="This is my first python text\nThis is my second python text\n"
append_text="This is my first append python text\nThis is my second append python text"
# 写open里面指出打开的文件位置,和写的方式
my_file1=open("C:/pyCode/test.txt",'w')
my_file1.write(text)
my_file1.close()
#追加
my_file2=open("C:/pyCode/test.txt",'a')
my_file2.write(append_text)
my_file2.close()
#读
my_file3=open("C:/pyCode/test.txt",'r')
read_text=my_file3.read()
print(read_text)
my_file3.close()
#按行读
my_file4=open("C:/pyCode/test.txt",'r')
read_text2=my_file4.readlines()
print(read_text2)
my_file4.close()
# 类的定义,类的属性在init里面写出来就行了
类里面的函数要引用自己的属性的时候要用self
python
class Caculate:
name="A good caculate"
price=18
#建立这个类的时候的初始化
def __init__(self,name,price,hight,width,weight):
self.name=name
self.price=price
self.hight=hight
self.width=width
self.weight=weight
def add(self,x,y):
print(self.name)
result =x+y
print (result)
def minus(self,x,y):
print(x*y)
def divide(self,x,y):
print(x/y)
caculate=Caculate("caculate",100,30,50,10)
print(caculate.price)
caculate.add(2,3)
# 输入input(返回值是String)
python
# a_input=input("please give me a num:")
# print("this input num is :"+a_input)
# 元组和列表
python
# 元组
a_tuple1=(1,2,3,4,5,6)
a_tuple2=1,2,3,4,5,6
# 列表
a_list=[1,2,3,4,5,6]
a_list.append(0) #
a_list.remove(2) #只会删除第一个2
print(a_list[-1]) #打印最后一位
print(a_list[0:3]) #打印0,1,2位
a_list.sort(reverse=True)
# 多维列表
a_listmulti=[[1,2,3],[1,2,3],[1,2,3]]
print(a_listmulti[1][2])
# 字典:没有顺序的K-V键值对
d={"apple":1,"banana":2}
d["orange"]=20
print(d)
# import引入模块
python
print(time.localtime())
# 自己的模块
import myfunc
myfunc.printme("yhb")
# continue AND break 跳过本次循环 和 跳出循环
python
a=10
while True:
if a<15:
a=a+1
print(a)
if a==15:
break
# 错误处理
python
try:
file=open("eeee","r+")
except Exception as e:
print(e)
response =input("do you want to create a file which called \"eeee\"")
if response=="y":
file=open("eeee","w")
else :
pass
else:
file.write("ssss0")
file.close()
# zip 合并元素放在一起,组成元组
lamda 简单的def函数
map 函数+参数一起输入访问,参数是一个一个的放入的
python
a=[1,2,3,4,5,6]
b=[10,20,30,40,50,60]
print(list(zip(a,b)))
fun2=lambda x,y:x+y
print(fun2(1,2))
print(list((map(fun2 ,[1,2],[2,3]))))
# copy
python
import copy
# a,b是一样的,改变一个都改变,索引指向一块内存区域
a=[1,2,3]
b=a
print(id(a))
print(id(b))
#a,c是不一样的,c相当于重新创建的内存区域
c=copy.copy(a)
print(id(a)==id(c))
# a,d是不一样的,但是a和d的列表的列表是一样的,列表一开始引用的时候可以看成直接引用的地址
a=[1,2,[3,4]]
d=copy.copy(a)
print(id(a)==id(d))
print(id(a[2])==id(d[2]))
# pickle 存储之前的计算结果
python
# d用二进制法写入到file里
import pickle
d={"apple":1,"banana":2,"orange":20}
file =open("pickle_example.pickle","wb")
pickle.dump(d,file)
file=open("pickle_example.pickle","rb")
a_dict=pickle.load(file)
print(a_dict)
# set集合 去重
python
char_list=['a','b','c','a','e','f']
print(set(char_list))
unique_char=set(char_list)
unique_char.add('F')
print(set(unique_char))
# clear 清空
# remove 删除某字符
# set1.difference(set2)返回2里面没有1里面有的
# 正则表达式
r开头的字符表示这是一个正则表达式
[]里面的内容表示都可以,任有一个就可以,0-9、a-z,A-Z
\d表示所有数字 \D不能是数字
\s表示一个空格 \S不能是空格
\w表示一个数字 \W不能是数字
\b表示一个空白符 \B不能是空白符
#(aaa)?空格里的文本有没有不影响 比如Monday和Mon缩写Mon(day)?
#\b 匹配文字前一个空白字符
#\B 匹配文字前空白字符
#\\匹配反斜杠\
注意:上述\表示为去除字母/数字本身含义
. 匹配除了\n的任何
^ 匹配句首(放在前)
$ 匹配句尾(放在后)
r"ab*"0次或多次
r"ab+"1次或多次
r"ab{2,10}"自选次数
python
import re
patton1="cat"
patton2="dog"
demo="cat and dog run"
print(patton1 in demo)
ptn=r"r[ua]n"
print(re.search(ptn,demo))#search表示表达式的匹配