Summary of variables, input and output in Python (code examples)

不言
Release: 2019-01-25 10:38:41
forward
3682 people have browsed it

This article brings you a summary of variables and input and output in Python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

To learn a programming language, the most basic thing is to learn its variable rules, conditional statements, loop statements and functions. The next few sections will begin to record these basic syntax. This section mainly records the variable rules!

1. Python input and output

Before talking about Python variables, let me first add the input and output statements of Python. During the writing process of Python, you will encounter many errors and basic debugging. The method is to print (output) the intermediate variables, so Python's input and output statements are very important, especially the output statements, which must be mastered flexibly.

Output

You may have come across the output statement print in the previous article. In fact, it is a built-in function in Python3 (the concept of functions will be discussed later). In Python, it is often called print. Specifically The usage is as follows:

1. View the help information

Enter help(print) in IPyone to get its help information. If you want to view the help information of other built-in functions, you can also use this method Method

In [1]: 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.
Copy after login

You can see that the parameters of the print function are value, sep, end, file, flush

where sep, end, file , flush have been assigned, that is to say, these parameters have default values. Whether you need to change them depends on your needs, but value has not been assigned a default value, that is to say, you must assign a value to value. That's it. If you still don't understand, take a look at the example:

2.value

Since the value parameter is located in the first position of the print function, there are two ways to assign a value to it: print(value='hi') or print('hi'), of course everyone prefers the latter

You can pass in multiple parameters, separated by commas:

In [2]: print('hello','hi','i am the best man in the world!')
hello hi i am the best man in the world!
Copy after login

can be a calculation formula and print the result:

In [3]: print(1+4)
5
Copy after login

After learning the above two methods, combine Get up and be naughty:

In [5]: print('你','是',200+50)
你 是 250
Copy after login

3.sep

sep is the separator, the default is a space, let's play with it:

Default

In [7]: print(5,2,0)
5 2 0
Copy after login

is modified to -

In [6]: print(5,2,0,sep='-')
5-2-0
Copy after login

is modified to none Any connector

In [8]: print(5,2,0,sep='')
520
Copy after login

In actual use, it is rare to go back and modify the sep value, usually the default is fine

4.end

end is the end character, the default is '\n' (actually carriage return and line feed), change it:

Default

In [12]: print(5), print(2), print(0)
5
2
0
Copy after login

Change the end character

In [13]: print(5,end='我是5的结束符'), print(2,end='我是2的结束符'), print(0,end='我是0的结束符')
5我是5的结束符2我是2的结束符0我是0的结束符
Copy after login

Print multiple statements on one line

In [14]: print(5,end=''), print(2,end=''), print(0)
520
Copy after login

In actual use, the value of the end parameter will be changed if necessary

5.file

file is the output stream, which is output to the screen by default. You can modify its value by Print to other locations, such as files:

Open VSCode, create a new #5 folder in the Learn Python with MS folder, and create a new test.py file to practice

Default

print(5,2,0)
输出结果为:5 2 0
Copy after login

Output to test.txt file

with open('test.txt', 'w') as f:
    print(5, 2, 0, file=f)
Copy after login

After that, you will see the test.txt file in the directory, the content of which is 5 2 0

6.flush

flush is to force flush to the output stream, the default is no. To understand this, you need to know how computer storage works. In order to speed up computer storage, the data is actually not written directly to the hard disk, but remains in the memory of the transfer station. When the amount of data in the memory reaches the specified value, it is The data in the memory will be written to the hard disk at high speed. If the data in the memory does not reach the specified value and the computer suddenly loses power, this part of the data will disappear. All print functions have such a parameter. In the future, once there is data, it will be written to the hard disk immediately, and it will not be affected by the power outage. Or other circumstances may lead to data loss =====( ̄▽ ̄*)b

Input

After talking about output, it’s time to enter the function input. It is very easy to input relative output:

1. View the help information

In [15]: help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.

    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.

    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
Copy after login

As you can see, the input is super simple, so without further ado, let’s check it out!

In Python3, everything input is a string (this is a data type of Python, which will be discussed later, but let’s learn about it now), the above code:

2. Input without parameters:

In [18]: input()
Hi   #这个是我输入的Out[18]: 'Hi'
Copy after login

3. Input with parameters:

In [19]: input('请输入:')
请输入:Hi     #我只输入了Hi
Out[19]: 'Hi'
Copy after login

You should understand after seeing this, let me add one more thing:

In [20]: input('>>')
>>520
Out[20]: '520'
Copy after login

这里需要格外注意的是,输出的520是被单引号引起来的,这就是字符串,而不是数字了

二、Python变量

看到变量,这可能是所有萌新最头疼的地点,因为很难理解的概念,其实变量在小学就遇到了,让我来勾起你的记忆:

小学题目:现有一个长方体,长10厘米,宽5厘米,请问这个长方体面积是多少? (答对不得分,答错扣41分)

高中题目:现有一个长方体,长a=10cm,宽b=5cm,计算其面积s。 (答对不得分,答错扣41分)

大学题目:现有 一个长方体,长为a,宽为b,请计算其面积s。 (答对不得分,答错扣41分)

我的题目:请以一个程序员的角度从以上三个题目中找出全部的变量!

1.什么是变量

维基百科这么说:在程序设计中,变量(英语:Variable,scalar)是指一个包含部分已知或未知数值或信息(即一个值)之存储地址,以及相对应之符号名称(识别字)。通常使用变量名称引用存储值;将名称和内容分开能让被使用的名称独立于所表示的精确消息之外。计算机源代码中的识别字能在运行期间绑扎一个值,且该变量的值可能在程序运行期间改变。 程序设计中的变量不一定能直接对应到数学中所谓的变量之概念。在程序设计中,变量的值不一定要为方程或数学公式之一部分。程序设计中的变量可使用在一段可重复的程序:在一处赋值,然后使用于另一处,接着在一次赋值,且以相同方式再使用一次(见迭代)。程序设计中的变量通常会给定一个较长的名称,以描述其用途;数学中的变量通常较为简洁,只给定一、两个字母,以方便抄写及操作。

我这么说:变量就是房子的门牌号

2.变量的申明

a = 10
Copy after login

这就就申明了一个变量,变量为 a,变量的值为10

3.变量的修改

In [21]: a = 10
In [22]: a
Out[22]: 10
In [23]: a = 20
In [24]: a
Out[24]: 20
Copy after login

变量的修改直接用新值覆盖掉以前的就可以

4.变量的命名规则

变量只能是字母、数字或下划线的任意组合

变量的第一个字符不能是数字

关键字不能申明为变量,Python关键字有:and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, fom, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield(这些关键字之后 都会学到,而且要熟练掌握哦,喔哈哈哈哈哈)

5.变量的深入探索

来看三段代码

No.1

In [25]: a=10
In [26]: b=10
In [27]: a,b
Out[27]: (10, 10)
In [28]: a=20
In [29]: a,b
Out[29]: (20, 10)
Copy after login

No.2

In [30]: a=10
In [31]: b=a
In [32]: a,b
Out[32]: (10, 10)
In [33]: a=20
In [34]: a,b
Out[34]: (20, 10)
Copy after login

No.3

In [51]: a=b=10
In [52]: a,b
Out[52]: (10, 10)
In [53]: b=20
In [54]: a,b
Out[54]: (10, 20)
Copy after login

从上面的三段代码可以看出,变量指向的永远是值,而不会指向变量,a=b=10的真实含义是a指向10,b指向10,这里的两个10是同一个10,后来b=20意思是b变心了,b现在指向了另外一个值20,但是a指向的依旧是10,这里一定要注意,a指向的是10,而不是指向变量b,理解不了那就上图:

这次懂了哇,弟弟们ㄟ( ▔, ▔ )ㄏ

6.变量的交换

如果你有其他语言的基础,那么对于交换变量这一块一定很熟悉,你会毫不犹豫的说找一个中间变量 t 不就行了,的确,Python也可以这样:

In [55]: a=10
In [56]: b=20
In [57]: a,b
Out[57]: (10, 20)
In [58]: t=a
In [59]: a=b
In [60]: b=t
In [61]: a,b
Out[61]: (20, 10)
Copy after login

但但但但但是,如果Python也用这种方法的话,我这里肯定就不会提及了,来看一名专业的Pythonic是如何交换变量的:

In [66]: a=10
In [67]: b=20
In [68]: a,b
Out[68]: (10, 20)
In [69]: a,b=b,a
In [70]: a,b
Out[70]: (20, 10)
Copy after login

不要惊讶(看你一副 没见过世面的样子,下面还有更精彩的),Python就是这么

The above is the detailed content of Summary of variables, input and output in Python (code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template