1.内置类型
1)整数
a = 123
print(a)
print(type(a))
123
<class 'int'\>
2)列表
b = [1, 2, 3]
print(b)
print(type(b))
[1, 2, 3]
<class 'list'>
3)字符串
string = 'string'
print(string)
print(type(string))
bs = b'bytes'
print(bs)
print(type(bs))
string
<class 'str'>
b'bytes'
<class 'bytes'>
4)字典
c = {'id': '123', 'name' : 'Leo'}
print(c)
print(type(c))
{'id': '123', 'name': 'Leo'}
<class 'dict'>
5)元组
d = (1, 'Leo', 99, 'boy')
print(d)
print(type(d))
(1, 'Leo', 99, 'boy')
<class 'tuple'>
6)集合
e = set('abcde')
print(e)
print(type(e))
{'c', 'd', 'e', 'b', 'a'}
<class 'set'>
7) 布尔
f = True
#f = None
print(f)
print(type(f))
True
<class 'bool'>
2.条件语句
a = 3
b = 5
if a > b :
print (a)
elif a < b :
print (b)
else :
print("equal")
5
c = {'id': '123', 'name' : 'Leo'}
ch = 'name'
if ch in c :
print(c[ch])
else :
print("error")
Leo
3.循环语句
for i in 'Python' :
print(i, end = ' ')
P y t h o n
i = 0
while i < 3 :
print(i)
i += 1
if i == 2 :
break;
else :
#只有循环正常离开时才会执行,即没有break
print(i)
print(i)
0
1
2
4. 异常处理
try :
num = "10" ** 2
except :
print("error")
error
5.print语句
#print([object, ...][, sep = ' '], [, end = '\n'],[, file = sys.stdout])
print('a', 'b', sep = '+', end = ' ')
print('= c')
a+b = c
6. 注释
"""
python: 注释文档
"""
print(__doc__)
python: 注释文档
7.函数
def fun(a, b) :
return a + b
print(fun(2,3))
5
#lambda表达式
x = (lambda a = "Python", b = ",hello!": a + b )
print(x("lambda"))
lambda,hello!
8.类
class student(object) :
def __init__(self, id, name) :
self.id = id
self.__name = name
def print_student(self) :
print(self.id, self.__name)
a = student('01111295', 'Leo')
a.print_student()
try :
print('id:', a.id)
print('name:', a.__name)
except :
print('error')
01111295 Leo
id: 01111295
error
9.继承
class fruit(object) :
def __init__(self, name) :
self.name = name
def output_name(self) :
print(self.name)
class apple(fruit) :
def __init__(self, name) :
self.name = name
a = apple("apple")
a.output_name()
type(a)
apple
__main__.apple