Python内置函数

目录

1>print

2>input

补充内容:

4>from ... import ...

<1>from 路径 import 文件

<2>from 文件 import 函数名

<3>from 文件 import *

5>for ... in ...

6>if ... elif ... else/if... else.../if...

7>id

8>type

9>eval

10>exec

11>del

12>help

1>单独help()

2>help(string var)

3>help(func)

13>while

14>break

15>continue

16>def

17>global

18>nonlocal

19>is不建议使用

20>pass

21>as

22>open

23>and,or,not

 24>Errors&Warnings

25>try...except...else...finally/try...except...finally/try...except.../try...finally...

26>raise

27>in

28>copyright()



 

1>print

Print()函数_html_finder的博客-CSDN博客print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)builtins.py里的释义:Prints the values to a stream, or to sys.stdout by default.Optional keyword arguments:file: a file-like object (stream); defaults to the current sys.stdout.sep: .https://blog.csdn.net/html_finder/article/details/123559812

2>input

【3】Input函数_html_finder的博客-CSDN博客_input函数只能输入字符串inputhttps://blog.csdn.net/html_finder/article/details/123619717

补充内容:

Python主要类型有

int str float list tuple dict set

还有一些特殊类型

Nonetype type

还有一些其他库中自定义类型

(numpy)numpy.float64

(turtle)turtle.Vec2D

如果你想把类型互相转换:

例:str转int

num="12345"

num=int(num)

所以,可以根据需要把input返回的字符串类型转换成你想要的:

num=int(input())

string=str(input())

floatvalue=float(input())

3>import 

Python函数就那么一点点,需要导入其他库,才能发现更广阔的世界!

import 库名

引用方法:库名.库所包含的变量/函数

常用库:

requests random os sys tkinter turtle

4>from ... import ...

import的2.0用法

<1>from 路径 import 文件

可以导入某一路径下的文件

<2>from 文件 import 函数名

只导入文件下的某一函数,可以直接引用

<3>from 文件 import *

导入文件下全部函数,引用方法为直接引用

5>for ... in ...

Python for ... in ...【1】_html_finder的博客-CSDN博客1245https://blog.csdn.net/html_finder/article/details/124068646

Python for...in... 【2】_html_finder的博客-CSDN博客for ... in ... 循环的第二篇https://blog.csdn.net/html_finder/article/details/124077488

6>if ... elif ... else/if... else.../if...

【5】Python-if函数_html_finder的博客-CSDN博客If判断语句https://blog.csdn.net/html_finder/article/details/124013473

7>id

返回地址。

"="就是把右边地址复制给左边。

8>type

返回类型(Type对象)。

9>eval

反回一个字符串表达式的实际值。

eval("4+1")返回5

eval("6==4")返回False

eval("not True")返回False

eval("print(11)")输出11

10>exec

比eval还要强!

执行字符串内代码。

exec("a=1")

创建一个名为a,值为1的变量。

11>del

删除值。

a=1

del a

删掉了变量a.

12>help

1>单独help()

启动help模式,输入函数名称返回简介,直到输入quit退出。

2>help(string var)

见下。

3>help(func)

输出func的简介。

13>while

while(布尔值):

        ...

表示循环做某事,直到布尔值为False.

14>break

跳出循环。

15>continue

返回到循环开头处,结束本次循环,继续新的一次循环。

16>def

创建新的函数。

def 函数名(参数一,...):

        ...
def 函数名(参数一=默认值一,…):

        ...

例:

def output(string):
    print(string)

这就是一个函数。

当调用为output("1")时,

输出1

当调用output()时,

报错,因为参数string未指定。

 改进后。。。

def output(string="1"):
    print(string)

调用output("1")输出1

调用output()自动默认string为"1",输出1

17>global

1>将变量设为全局变量。

2>声明使用全局变量而非局部。

例:

a=0
def change(new):
    a=new
    print(a)
change(5)
print(a)

结果:

5

0

为什么?因为函数change中a只是局部变量,并没有改动全局变量a(即开头定义的)。

解决方法:

a=0
def change(new):
    global a
    a=new
    print(a)
change(5)
print(a)

结果:

5

5

这次,声明改动的是全局变量,就成功了。

---------------------------------------------------

例2

def plus(a,b):
    result=a+b
plus(1,2)
print(result)

报错。因为定义的result也是局部变量,出了plus函数就不好使了。

改进:

def plus(a,b):
    global result
    result=a+b
plus(1,2)
print(result)

结果:3

将result设为全局变量,就不会报错了。

18>nonlocal

当出现嵌套函数时:

def a():
    z=1
    def b():
        z+=1
    b()
a()

报错:找不到变量z

用老办法:

def a():
    z=1
    def b():
        global z
        z+=1
    b()
a()

又㕛叒报错!

原因:没有定义过全局变量,即使在a里,z也只是一个局部变量,怎么办?

nonlocal 闪   亮    登   场

def a():
    z=1
    def b():
        nonlocal z
        z+=1
    b()
a()

可-以-了

19>is不建议使用

比较两个地址是否相等(区别于==)

但是,如果在较高版本下运行is 或is not:

C:\Users\************\Desktop\MEME\1\__init__.py:5: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if a is 5:

不会停止运行,但这行Warning实在难受,好像无 法 屏 蔽!

可以改为:

if id(a)==id(5):

20>pass

如果你写了一个带冒号的语句,但没有语句后需执行的函数:不写会报错,语法错误。

写注释也没有。

于是,pass应运而生。它是占位符,没有实际意义。

def kong():
    pass
a=0
if a<0:
    pass
else:
    print("a>=0!")
for i in range(10):
    pass

21>as

定义一个别名。

常用法:

与import连用。

import random

random.randint(1,2)

等于:

import random as rd

rd.randint(1,2)

22>open

打开一个文件。

23>and,or,not

见:逻辑运算符https://blog.csdn.net/html_finder/article/details/123553622#t23

 24>Errors&Warnings

显示语法错误或者警告。

25>try...except...else...finally/try...except...finally/try...except.../try...finally...

try:

语句

接下来添加附件!

1)except xxx:当报xxx错时,运行except内语句,不填表示只要报错,就运行except内语句。

2)else:没报错是运行。

3)finally:最后不管报不报错都运行。

26>raise

抛出xxx错误或警告。

27>in

逻辑运算,表示量是否在一个列表/集合/数组/...里。

或者字符串是否被包含在另一字符串里。

28>copyright()

输出copyright.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unconquerable p

给点吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值