预习课2-基础函数+循环:if...elif..

Python自带去重

h={1,2,1,3,4}
h
{1, 2, 3, 4}

取地板值(取整)

9//10
0

11//10
1

11//3
3

四舍五入函数round

round(0.9,0)
1.0

round(0.5,0)
0.0

round(0.51,0)
1.0

round(1/3,0)
0.0

round(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)
45

min(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.0

math.sqrt(4)
2.0

math.sqrt(8)
2.8284271247461903

math.pi
3.141592653589793

ASCII举例:字母和数字互相转换
字母转换成字符

ord(‘a’)
97

ord(‘A’)
65
字符转换成字母

chr(98)
‘b’

chr(66)
‘B’

除数取余

4%3
1

5%2
1

divmod()函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组

divmod(9,4)
(2, 1)

divmod(3,4)
(0, 3)

divmod(3,4)[0]
0

divmod(3,4)[1]
3

例:

age = input(“输入你的年龄:”)
输入你的年龄:6

age=int(10)
if age>5:
… print(“你的年龄大于5岁”)
… else:
… print(“你的年了小于5岁”)

你的年龄大于5岁

练习:输入一个成绩,然后大于等于60,打印及格,否则打印不及格

a=input(“输入你的成绩:”)
输入你的成绩:61

a=int(61)
if a>60:
… print(“及格”)
… else:
… print(“不及格”)

及格

bool举例

type(True)
<class ‘bool’>

isinstance(True,bool)
True

bool(5>4)
True

bool(5<4)
False

bool(“a”>“A”)
True

当bool中是空和0时,返回false

bool([])
False

bool("")
False

bool(())
False

bool({})
False

bool(0)
False

练习:输入一个数字,如果能同时被3或5整除,输出:OK,不然输出:Not Ok

a=int(input(“请输入一个数字:”))
请输入一个数字:10

if a%30 and a%50:
… print(“OK”)
… else:
… print(“Not OK”)

Not OK

练习:输入一个字符串,如果长度大于3,打印大于3,如果长度等于3,打印等于3,否则打印小于3

s=input(“输入一个字符:”)
输入一个字符:abcdE

if len(s)❤️:
… print(“小于3”)
… elif len(s)==3:
… print(“等于3”)
… else:
… print(“大于3”)

大于3

Python中没有switch的语法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值