"""
一、基本数据类型
integer
float
string
"""
print("hello\tworld");
print("hello"+str(122));
print(int(1)+4);
print(float(1.5)+4.1);
"""
二、运算符与变量
+-*/%//
"""
print(3**2);
print(5/3);
print(8//3);
a=1.0;
b=2.0;
c=3.0;
print(a,b,c);
print("a+b=",c);
print(str(a)+'+'+str(b)+'='+str(a+b))
"""
三、while & for
"""
condition=100;
while(condition<500):
print(condition);
condition+=99;
for i in range(2,10,3):
print(i);
for i in [1,3,5,7]:
print(i);
for i in (11,22,33,44):
print(i);
"""
四、list
"""
a_list=[1,2,30,30,30,4,2];
print(a_list);
print(a_list[2]);
print(a_list[-2]);
print(a_list[1:4]);#output id=1 2 3
print(a_list[:-3]);#output id=1~-4
print(len(a_list));
print(a_list.index(30));
print(a_list.count(30));
a_list.sort();
print(a_list);
a_list.sort(reverse=True);
print(a_list);
"""
五、list
"""
a_list=[1,2,30,30,30,4,2];
a_list[0]=100;#update
a_list.append(200);#add
a_list.insert(2,300);#add
a=a_list.pop();#delete
a_list.remove(30);#delete
print(a_list);
b_list=[[1,2,3],
[4,5,6],
[7,8,9]]
print(b_list[2][1])
"""
六、tuple
"""
a_tuple=(1,2,30,30,4,5)
b_tuple=1,2,30,30,4,5
print(a_tuple)
print(b_tuple)
print(a_tuple[1])
print(a_tuple[1:4])
print(a_tuple.count(30))
for i in a_tuple:
print(i)
"""
七、if
> >= < <= == !=
"""
a=1
b=2
c=3
d=1
if(a>b):
print("right")
elif(a<b):
print("wrong")
else:
pass
"""
八、if
and or
"""
a=1
b=2
c=3
d=1
if(a<b and a==d):
print("rihgt")
if(b>3 or c<4):
print("hello")
colors=['red','blue','black','green']
for color in colors:
if(color=="black"):
print("black")
break;
else:
print("no")
continue;
print("not black")
"""
九、dictionary
"""
a_list=[1,2,3,4,5]
d={"pen":7,"apple":3,"applepen":10}
d2={1:"a",5:"b",2:"d"}
d3={1.2:3,"a":3.5,"1":"aaa"}
print(d)
print(d2)
print(d3)
print(d["apple"])
print(d2[2])
d4={"a":[1,2,3,4],"b":(1,2,3,4),"c":{"aa":1,"bb":2}}
print(d4)
print(d4["c"]["aa"])
d["pen"]=10#update
print(d)
d["pineapple"]=3#add
print(d)
del d["pineapple"]#delete
print(d)
for key,value in d.items():
print("key:",key,"\t","value:",value)
for key in d.keys():
print("key:",key)
for value in d.values():
print("values:",value)
for key in sorted(d.keys()):
print(key)#字典本身不能排序
"""
十、函数
"""
def function():#无参
a=1;
b=2;
c=a+b;
print(a,b,c);
function();
def function2(a,b=20):#有参
c=a+b
print(a,b,c)
function2(10,20);
def function3(a,b=20):#有返回
c=a+b;
return c;
print(function3(1));
kkk=97
def function4():#用全局变量
global kkk
print(kkk);
function4();
"""
十一、模块
def func_max(a,b):
if(a>b):
return a
else:
return b
如何生成模块呢?
新建max.py文件
内容只有上面func_max函数
然后在同一目录下的另一个PY文件中
import max
(可写成import max as m)
或者
from max import func_max
或者
from max import *
就可以用了(用*不太好)
"""
import max
print(max.func_max(10,20))#output20
from max import test
print(test(10))
"""
十二、类
"""
class human:
#类的属性
name="someone"
age=100
#类的方法
def __init__(self,name,age):
self.name=name;
self.age=age;
def my_name(self):#self指针指向自身
print(self.name)
def my_age(self):
print(self.age)
def think(self,a,b):
print(a+b)
person1=human("cj",23)
print(person1.name)
person2=human(age=22,name="cjs")
print(person2.age)
"""
十三、继承
"""
class student(human):
def __init__(self,name="gjl",age=22,grade=1,school="MIT"):
super().__init__(name,age)
self.grade=grade
self.school=school
self.score=100
print("student init")
def learn(self):#自己方法
print(str(self.name)+" is learning")
def think(self,a,b):#重写
print(a*b)
stu1=student(name="cj",age=23,grade=1,school="gdut")
stu1.learn()
stu1.think(3,4)
"""
十四、input
"""
#a_input=input("please input:\n")
#print(a_input)
"""
十五、文件的读写
"""
text="writing a text\nhello world"
my_file=open("file1.txt",'a')
my_file.write(text)
my_file.close()
#open the file as writing, create it if it doesn't exist
#不写绝对路径就视为当前路径下
#另一种写法是with open("file1.txt",'w') as my_file#这么做的话不用close
with open("file1.txt",'r')as f2:
content=f2.readlines()#读取所有行存进列表中,每行是一个元素
for line in content:
print(line)
#三个操作符:清空重写,r读,a末尾追加
"""
十六、异常处理
"""
#file=open('hahaha','r+')#先读,能开就写
try:#因为打开的时候不知道是否存在,除非把文件名处理一次,但这没必要,因为下面这写法够简洁
file=open("hahaha","r+")
except Exception as e:
print(e)
response=input("do you want to create it: ")
if(response=='yes'):
with open("hahaha",'w') as f:
pass
print("ok")
else:
pass
else:#正常打开没有出错就执行以下操作
file.write("hahaha")
file.close()
"""
十七、数据存储
"""
import json
a_dict={"user_id":"qbf","user_name":"hello",100:200}
with open("example.json",'w')as f:
json.dump(a_dict,f)
with open("examle.json")as f:
content=json.load(f)
print(content)