day1 instance,round,divmod,imput, 字符串

 
>>> a = '123'
>>> isinstance(a, str)
True
>>> b = 1
>>> type(b)
<class 'int'>
>>> isinstance(b, int)
True
>>> c = [1,2,3,4]
>>> type(c)
<class 'list'>
>>> isinstance(c,list)
True
>>> b = {1,2,3}
>>> type(b)
<class 'set'>
>>> isinstance(b,set)
True
>>> e = (1,23,4)
>>> type(e)
<class 'tuple'>
>>> isinstance(e,tuple)
True
>>> isinstance(e,list)
False
 
isinstace 方法主要用来对比类型是否符合预期,第一个参数为传入的数据,第二个参数为预期的参数类型,如果一样则返回true,否则返回false。在这个过程中总是把isinstance写错。
isinstance isinstance isinstance isinstance isinstance isinstance isinstance isinstance isinstance
>>> a = 1
>>> b = 2
>>> a+b
3
>>> a - b
-1
>>> a*b
2
>>> a/b
0.5
>>> a//b 地板除
0
>>> a%b
1
>>> b%a
0
运算符号,加减乘除,%用来取余,可以用来判断是奇数还是偶数
>>> round(1/3,3)
0.333
>>> round(1/2,0)
0.0
>>> round(0.5,0)
0.0
>>> round(0.51,0)
1.0
round 内置函数,类似于四舍五入,区别在于如果是0.5向下取值,如果是0.51 会向上取值
dir(__builtins__) 查看Python的内置函数
help('max') 查看使用说明,ord()和chr()相互转换
>>> ord('a')
97
>>> ord('b')
98
>>> chr(97)
'a'
>>> divmod(4,3) divmod 用来取商和余,第一个值为商,第二个值为余
(1, 1)
>>> "a">"b"
False
>>> "a"<"b"
True
>>> print("hello word")
hello word
>>> print("hello word", end="")
hello word>>>
>>> print("hello word", end="*")
hello word*>>>
 
>>> inp = input("你多大了?")
你多大了?10
>>> print(inp)
10
>>> type(inp)
<class 'str'>
>>> int(inp)+10
20
 
字符串练习:
s = 'ab,cd:efg}eg/tt\\ss|fas*'
>>> re.split(";",s)
['ab,cd:efg}eg/tt\\ss|fas*']
>>> re.split("[,:]",s)
['ab', 'cd', 'efg}eg/tt\\ss|fas*']
>>> re.split("[,:}]",s)
['ab', 'cd', 'efg', 'eg/tt\\ss|fas*']
>>> re.split("[,:}]/",s)
['ab,cd:efg}eg/tt\\ss|fas*']
>>> re.split("[,:}/]",s)
['ab', 'cd', 'efg', 'eg', 'tt\\ss|fas*']
>>> re.split("[,:}\/]",s)
['ab', 'cd', 'efg', 'eg', 'tt\\ss|fas*']
>>> re.split("[,:}\\/|]",s)
['ab', 'cd', 'efg', 'eg', 'tt\\ss', 'fas*']
>>> re.split("[,:}\\/|*]",s)
['ab', 'cd', 'efg', 'eg', 'tt\\ss', 'fas', '']
使用re.split切分字符串需要注意,如果要切分多个,要把切分的符号用[]包括起来,单个不用
 
>>> age = input("请输入你的年龄:")
请输入你的年龄:10
>>> if int(age)>5:
... print("你的年龄大于5岁!")
... else:
... print("你的年龄小于5岁!")
...
你的年龄大于5岁!
>>> soce = input("请输入你的成绩:")
请输入你的成绩:61
>>> if int(soce) >= 60:
... print("恭喜你考试及格")
... else:
... print("考试不及格,还需要加油!")
...
恭喜你考试及格
 
>>> def chengji(score):
... s = int(score)
... if isinstance(s,int):
... if s >= 60 and s < 80:
... print("你的成绩是及格!")
... elif s >=80 and s<=100:
... print("你的成绩是优秀!")
... elif s == 100:
... print("哇,你真棒,满分!")
... else:
... print("成绩不及格,下次加油!")
... else:
... print("分数必须是int类型哦!")
...
>>> score = input("请输入你的分数:")
请输入你的分数:100
>>> chengji(score)
你的成绩是优秀!
>>> chengji(score)
你的成绩是优秀!
>>> score = 10
>>> chengji(score)
成绩不及格,下次加油!
>>> score = 61
>>> chengji(score)
你的成绩是及格!
>>> True and True
True
>>> True and False
False
>>> False and False
False
>>> False and True
False
出了真对真,其余都是假
 
>>> def zhengchu(zz):
... if 2//zz !=0 and 5//zz != 0:
... print("OK")
... else:
... print("NO")
...
>>> zz = 10
>>> zhengchu(zz)
NO
>>> zz = 1
>>> zhengchu(zz)
OK
>>> zz = 2
>>> zhengchu(zz)
OK
>>> zz = 3
>>> zhengchu(zz)
NO
>>> zz = 4
>>> zhengchu(zz)
NO
整除:可以用%号
>>> a = int(input("请输入数字:"))
请输入数字:10
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
OK
>>> a = 1
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
>>> a = 2
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
>>> a = 3
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
OK
>>> a = 4
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
>>> a = 5
>>> if a %3 == 0 or a%5 == 0:print("OK")
...
OK
 

posted @ 2019-12-23 08:07  绝世老中医  阅读(252)  评论(0编辑  收藏  举报