0


python编写交互界面怎么用,python交互界面有什么用

大家好,小编来为大家解答以下问题,python编写交互界面怎么用,python交互界面有什么用,今天让我们一起来看看吧!

Source code download: 本文相关源码

目录

6.5.1 理解交互模式

6.5.2 进入交互模式

6.5.3 交互模式的基本用法

6.5.4 在交互模式中获取帮助

6.5.5 退出交互模式

6.5.6 系统学习python


6.5.1 理解交互模式

理解交互模式,首先得理解什么是交互。“交互”在日常生活中很常见,比如人与人之间的互动就是一种交互,在你来我往的语言交流中,彼此可以接收到对方在语言中传递的信息python四瓣花怎么画。计算机中的交互,是指程序对用户的输入所做出的反馈,例如在浏览器的地址栏中输入Python官网的URL:

浏览器会渲染并呈现出Python官网的页面内容:

在命令行中执行命令,命令的输出就是系统所做出的反馈:

以上,都为一种交互。交互是一种互动,人与人的互动,人与计算机程序之间的互动。

6.5.2 进入交互模式

进入windows系统的命令行以后,直接在命令行中执行Python命令,可以进入Python的交互模式。

从交互模式输出的提示信息中,可以知道Python的版本号,标签名,运行环境,以及获取Python更多相关信息的四个命令。在这四个命令中,help用来获取帮助信息,copyright用来获取版权信息,credits用来获取感谢信息,license用来获取软件许可证信息。可以直接在交互模式中输入以上命令来获取相关信息,例如查看Python的版权信息:

>>> copyright
Copyright (c) 2001-2019 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.

6.5.3 交互模式的基本用法

在Python的交互模式中可以直接定义变量,控制结构,函数等,并进行相应的运算处理。

(1) 在交互模式中定义变量

>>> number=1

在交互模式中输入变量名,并敲下回车键,会自动输出变量指向的值:

>>> number
1

(2) 在交互模式中进行算数运算

>>> 1+1
2
>>> x=1
>>> y=1
>>> x+y
2

(3) 在交互模式中调用函数

在6.1节中介绍了id函数,调用id函数可以输出对象的内存地址。

>>> total = 66
>>> amount = total
>>> id(66)
140705939880240
>>> id(total)
140705939880240
>>> id(amount)
140705939880240

从id函数的输出可知,变量total与变量amount保存的都是值66的内存地址。对内存地址还不是很熟悉的同学,可以复习6.1节中的内容,学习是一个反复迭代的过程,要善于将学过的知识融会贯通。

内存地址与机器相关,在不同的运行环境中,输出的是不同的内存地址。

(4) 在交互模式中执行条件控制

>>> number = 1
>>> if number > 0:
...     print("number > 0")
...
number > 0

同学们在交互模式中输入条件语句的代码时,要注意代码缩进。在交互模式中同样可以进行其它的操作,例如编写循环结构,函数定义,模块导入等。在实际开发中,通常使用交互模式进行代码调试和快速验证。交互模式还有一个很重要的用途-获取帮助信息,在下节内容中会着重讲解。

6.5.4 在交互模式中获取帮助

利用Python的内置函数help,可在交互模式中离线地获取帮助信息。help函数的基本用法:

help(object)

object表示传递给help函数的参数。参数传递有如下两种形式:

(1) 字符串类型参数

help(字符串类型参数)

在Python中用英文的单引号,双引号,三引号括住的都为字符串。字符串类型参数通常是Python中预定义的关键字名,属性名,模块名等。keywords是系统预定义的标识符,中文释义为关键词,在交互模式中执行help("keywords"),可以查询Python中的所有关键字:

>>> help("keywords")

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not

在交互模式中查询if关键字的定义和用法:

>>> help("if")
The "if" statement
******************

The "if" statement is used for conditional execution:

   if_stmt ::= "if" expression ":" suite
               ("elif" expression ":" suite)*
               ["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.

Related help topics: TRUTHVALUE

当help的输出过长时,会在页面底部显示一个-- More --的提示符:

>>> help("for")
The "for" statement
*******************
The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

   for_stmt ::= "for" target_list "in" expression_list ":" suite
                ["else" ":" suite]
The expression list is evaluated once; it should yield an iterable
object.  An iterator is created for the result of the
......
-- More  --

按下键盘的回车键可以继续浏览。当按下键盘的q键时,会退出help模式。在交互模式中直接执行help(),不传递任何参数时,会进入help模式,进入help模式后直接输入待查的属性名或对象名,即可执行相应的查找。在help模式中执行quit命令退出help模式。

(2) 对象名

help(对象名)

对象名参数可以是Python中内置的全局变量,函数名,已导入的模块名, 也可以是用户自定义的对象名(变量名,函数名等)。在交互模式中查看内置函数print的用法:

>>> help(print)
Help on built-in function print in module builtins:
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    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:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

6.5.5 退出交互模式

在交互模式中执行exit()或quit()函数来退出交互模式。

D:\>Python
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) 
[MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" 
or "license" for more information.

>>> exit()
D:\>

6.5.6 系统学习python

薯条老师简介:资深技术专家,技术作家,著有《Python零基础入门指南》,《Java零基础入门指南》等技术教程。薯条老师的博客:http://www.chipscoco.com, 系统学习后端,爬虫,数据分析,机器学习


本文转载自: https://blog.csdn.net/2301_81337765/article/details/136007954
版权归原作者 2301_81337765 所有, 如有侵权,请联系我们删除。

“python编写交互界面怎么用,python交互界面有什么用”的评论:

还没有评论