Python自带去重
h={1,2,1,3,4}
h
{1, 2, 3, 4}
取地板值(取整)
9//10
011//10
111//3
3
四舍五入函数round
round(0.9,0)
1.0round(0.5,0)
0.0round(0.51,0)
1.0round(1/3,0)
0.0round(1/3,3)
0.333
查看函数类型
dir(builtins)
[‘ArithmeticError’, ‘AssertionError’, ‘AttributeError’, ‘BaseException’, ‘Blocki
ngIOError’, ‘BrokenPipeError’, ‘BufferError’, ‘BytesWarning’, 'ChildProcessError
', ‘ConnectionAbortedError’, ‘ConnectionError’, ‘ConnectionRefusedError’, ‘Conne
ctionResetError’, ‘DeprecationWarning’, ‘EOFError’, ‘Ellipsis’, ‘EnvironmentErro
r’, ‘Exception’, ‘False’, ‘FileExistsError’, ‘FileNotFoundError’, ‘FloatingPoint
Error’, ‘FutureWarning’, ‘GeneratorExit’, ‘IOError’, ‘ImportError’, ‘ImportWarni
ng’, ‘IndentationError’, ‘IndexError’, ‘InterruptedError’, ‘IsADirectoryError’,
‘KeyError’, ‘KeyboardInterrupt’, ‘LookupError’, ‘MemoryError’, ‘ModuleNotFoundEr
ror’, ‘NameError’, ‘None’, ‘NotADirectoryError’, ‘NotImplemented’, ‘NotImplement
edError’, ‘OSError’, ‘OverflowError’, ‘PendingDeprecationWarning’, ‘PermissionEr
ror’, ‘ProcessLookupError’, ‘RecursionError’, ‘ReferenceError’, 'ResourceWarning
', ‘RuntimeError’, ‘RuntimeWarning’, ‘StopAsyncIteration’, ‘StopIteration’, ‘Syn
taxError’, ‘SyntaxWarning’, ‘SystemError’, ‘SystemExit’, ‘TabError’, ‘TimeoutErr
or’, ‘True’, ‘TypeError’, ‘UnboundLocalError’, ‘UnicodeDecodeError’, ‘UnicodeEnc
odeError’, ‘UnicodeError’, ‘UnicodeTranslateError’, ‘UnicodeWarning’, ‘UserWarni
ng’, ‘ValueError’, ‘Warning’, ‘WindowsError’, ‘ZeroDivisionError’, ‘_’, ‘_build
class’, ‘debug’, ‘doc’, ‘import’, ‘loader’, ‘name’, ‘pa
ckage’, ‘spec’, ‘abs’, ‘all’, ‘any’, ‘ascii’, ‘bin’, ‘bool’, ‘bytearray’,
‘bytes’, ‘callable’, ‘chr’, ‘classmethod’, ‘compile’, ‘complex’, ‘copyright’, ‘c
redits’, ‘delattr’, ‘dict’, ‘dir’, ‘divmod’, ‘enumerate’, ‘eval’, ‘exec’, ‘exit’
, ‘filter’, ‘float’, ‘format’, ‘frozenset’, ‘getattr’, ‘globals’, ‘hasattr’, ‘ha
sh’, ‘help’, ‘hex’, ‘id’, ‘input’, ‘int’, ‘isinstance’, ‘issubclass’, ‘iter’, ‘l
en’, ‘license’, ‘list’, ‘locals’, ‘map’, ‘max’, ‘memoryview’, ‘min’, ‘next’, ‘ob
ject’, ‘oct’, ‘open’, ‘ord’, ‘pow’, ‘print’, ‘property’, ‘quit’, ‘range’, ‘repr’
, ‘reversed’, ‘round’, ‘set’, ‘setattr’, ‘slice’, ‘sorted’, ‘staticmethod’, 'str
', ‘sum’, ‘super’, ‘tuple’, ‘type’, ‘vars’, ‘zip’]
函数类型举例
max(1,2,3,43,45)
45min(1,2,3,43,45)
1
查看函数使用方法
help(max)
Help on built-in function max in module builtins:
max(…)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
调用数学的函数使用
import math
math.pow(2,3)
8.0math.sqrt(4)
2.0math.sqrt(8)
2.8284271247461903math.pi
3.141592653589793
ASCII举例:字母和数字互相转换
字母转换成字符
ord(‘a’)
97ord(‘A’)
65
字符转换成字母chr(98)
‘b’chr(66)
‘B’
除数取余
4%3
15%2
1
divmod()函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组
divmod(9,4)
(2, 1)divmod(3,4)
(0, 3)divmod(3,4)[0]
0divmod(3,4)[1]
3
例:
age = input(“输入你的年龄:”)
输入你的年龄:6age=int(10)
if age>5:
… print(“你的年龄大于5岁”)
… else:
… print(“你的年了小于5岁”)
…
你的年龄大于5岁
练习:输入一个成绩,然后大于等于60,打印及格,否则打印不及格
a=input(“输入你的成绩:”)
输入你的成绩:61a=int(61)
if a>60:
… print(“及格”)
… else:
… print(“不及格”)
…
及格
bool举例
type(True)
<class ‘bool’>isinstance(True,bool)
Truebool(5>4)
Truebool(5<4)
Falsebool(“a”>“A”)
True
当bool中是空和0时,返回false
bool([])
Falsebool("")
Falsebool(())
Falsebool({})
Falsebool(0)
False
练习:输入一个数字,如果能同时被3或5整除,输出:OK,不然输出:Not Ok
a=int(input(“请输入一个数字:”))
请输入一个数字:10if a%30 and a%50:
… print(“OK”)
… else:
… print(“Not OK”)
…
Not OK
练习:输入一个字符串,如果长度大于3,打印大于3,如果长度等于3,打印等于3,否则打印小于3
s=input(“输入一个字符:”)
输入一个字符:abcdEif len(s)❤️:
… print(“小于3”)
… elif len(s)==3:
… print(“等于3”)
… else:
… print(“大于3”)
…
大于3
Python中没有switch的语法。