目录
4.while for control+[/] 左/右缩进 (缩进是四个空格)
1.jupyter简单操作
首先是编译器的简单操作,这里我选择的是jupyter notebook。下面是常用到的三个简单快捷键:
选中代码块使其变为蓝色,之后输入快捷键。
dd delet
shift+enter光标移动/ctrl+enter run
m markdown
下面是python的基础语法!
2.print()用法
print(1)
print('yi')
print("i'm")
print('i\'m')
print('a'+'b')
输出: 1 yi i'm i'm ab
3.math 举一个平方的例子
2**3
8
4.while for control+[/] 左/右缩进 (缩进是四个空格)
i=0
while(i<3):
print(i)
i=i+1
for i in range(1,5):
print(i)
输出:0 1 2 1 2 3 4
5.if if_else if_elif_else
if a>b:
print()
if a>b:
print()
else:
print()
if a>b:
print()
elif a=b:
print()
elif ...:
.
.
.
else:
print()
6.function 函数定义格式
def function(a,b):
x=a+b
print(x)
function(3,4)
输出:7
函数默认参数的定义
def default_fun(a,b=3):
x=a+b
print(x)
default_fun(2)
输出:5
默认参数右边不能有需赋值的参数
def default_fun(a=3,b):
x=a+b
print(x)
default_fun(2)
报错 SyntaxError: non-default argument follows default argument
7.创建文件并写入
text="1,2,3"
file=open('text.txt','w')
file.write(text)
file.close()
8.向已有文件中写入
append="\nhello world"
file=open('text.txt','a')
file.write(append)
file.close()
9.读文件
file=open('text.txt','r')
a=file.read()
print(a)
输出:
1,2,3 hello world
10.class 类的创建
class calculator:
price=18
brand="casco"
def add(self,x,y):
result=x+y
print(result)
def show(self):#self
print(self.price)
calculator1=calculator()
calculator1.add(3,5)
calculator1.show()
输出
8 18
记住self和this指针类似不要忘记写
class calculator:
def __init__(self,name,price,brand,size):##init function
self.n=name
self.p=price
self.b=brand
self.s=size
def show(self):
print(self.n,self.p,self.b,self.s)
calculator2=calculator('q',3,'y',2)
calculator2.show()
calculator2.n
class calculator:
def __init__(self,name='e',price=8,brand='u',size=7):##init function default
self.n=name
self.p=price
self.b=brand
self.s=size
def show(self):
print(self.n,self.p,self.b,self.s)
calculator2=calculator()
calculator2.show()
输出:e 8 u 7
11.input函数
a=input()
if a=='1':##input 默认字符串输入可强制类型转换
print('yes')
else:
print('no')
输出
1 yes
12.元组与列表
a_tuple=(1,2,3,4,5,6)
a_list=[7,6,5,4,3,2]
for i in a_tuple:
print(i) # i
for i in range(len(a_list)):
print(a_list[i]) #[ ]
a_list=[7,6,5,4,3,2]
a_list.append('a')
print(a_list)
a_list.insert(3,'h')
print(a_list)
a_list.remove('h')
print(a_list)
print(a_list[-1])
print(a_list[2:4]) #2-3
print(a_list.index('a')) # index
b_list=[1,8,5,8,9,2]
b_list.sort()
print(b_list) #small->big
b_list.sort(reverse=True)
print(b_list) #big->small
输出:
[7, 6, 5, 4, 3, 2, 'a'] [7, 6, 5, 'h', 4, 3, 2, 'a'] [7, 6, 5, 4, 3, 2, 'a'] a [5, 4] 6 [1, 2, 5, 8, 8, 9] [9, 8, 8, 5, 2, 1]
多维列表
a_multi_list=[
[1,2,3],
[4,5,6],
[7,8,9]
]
print(a_multi_list[0][0]) #[a][b]
13.dictionary 字典
diction={'key1':'value1','key2':'value2','key3':'value3'}
print(diction['key2'])
del diction['key2']
print(diction)#字典中value可以是元组 列表 函数 字典。。
输出:
value2 {'key1': 'value1', 'key3': 'value3'}
14.import 导入模块
import time as t
print(t.localtime())
输出:
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=26, tm_hour=19, tm_min=57, tm_sec=2, tm_wday=6, tm_yday=177, tm_isdst=0)
15.break&continue 语句
while True:
b=input()
if b=='1':
print('end')
break
else:
print('go on')
while True:
b=input()
if b=='1':
print('end')
continue
else:
print('go on')
16.zip map lambda 函数
zip:
a=[1,2,3]
b=[4,5,6]
zip(a,b)
list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
a=[1,2,3]
b=[4,5,6] # 1 2 3
zip(a,b,b) # 4 5 6
list(zip(a,b,b)) # 4 5 6
[(1, 4, 4), (2, 5, 5), (3, 6, 6)]
lambda 的作用和函数类似
def plus(x,y):
return(x+y)
plus(2,6)
7
plus2=lambda x,y:x+y
plus2(4,3)
7
map:
def plus(x,y):
return(x+y)
map(plus,[1],[2])
list(map(plus,[1],[2]))
[3]