


Summary of variables, input and output in Python (code examples)
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.
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!
can be a calculation formula and print the result:
In [3]: print(1+4) 5
After learning the above two methods, combine Get up and be naughty:
In [5]: print('你','是',200+50) 你 是 250
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
is modified to -
In [6]: print(5,2,0,sep='-') 5-2-0
is modified to none Any connector
In [8]: print(5,2,0,sep='') 520
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
Change the end character
In [13]: print(5,end='我是5的结束符'), print(2,end='我是2的结束符'), print(0,end='我是0的结束符') 5我是5的结束符2我是2的结束符0我是0的结束符
Print multiple statements on one line
In [14]: print(5,end=''), print(2,end=''), print(0) 520
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
Output to test.txt file
with open('test.txt', 'w') as f: print(5, 2, 0, file=f)
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.
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'
3. Input with parameters:
In [19]: input('请输入:') 请输入:Hi #我只输入了Hi Out[19]: 'Hi'
You should understand after seeing this, let me add one more thing:
In [20]: input('>>') >>520 Out[20]: '520'
这里需要格外注意的是,输出的520是被单引号引起来的,这就是字符串,而不是数字了
二、Python变量
看到变量,这可能是所有萌新最头疼的地点,因为很难理解的概念,其实变量在小学就遇到了,让我来勾起你的记忆:
小学题目:现有一个长方体,长10厘米,宽5厘米,请问这个长方体面积是多少? (答对不得分,答错扣41分)
高中题目:现有一个长方体,长a=10cm,宽b=5cm,计算其面积s。 (答对不得分,答错扣41分)
大学题目:现有 一个长方体,长为a,宽为b,请计算其面积s。 (答对不得分,答错扣41分)
我的题目:请以一个程序员的角度从以上三个题目中找出全部的变量!
1.什么是变量
维基百科这么说:在程序设计中,变量(英语:Variable,scalar)是指一个包含部分已知或未知数值或信息(即一个值)之存储地址,以及相对应之符号名称(识别字)。通常使用变量名称引用存储值;将名称和内容分开能让被使用的名称独立于所表示的精确消息之外。计算机源代码中的识别字能在运行期间绑扎一个值,且该变量的值可能在程序运行期间改变。 程序设计中的变量不一定能直接对应到数学中所谓的变量之概念。在程序设计中,变量的值不一定要为方程或数学公式之一部分。程序设计中的变量可使用在一段可重复的程序:在一处赋值,然后使用于另一处,接着在一次赋值,且以相同方式再使用一次(见迭代)。程序设计中的变量通常会给定一个较长的名称,以描述其用途;数学中的变量通常较为简洁,只给定一、两个字母,以方便抄写及操作。
我这么说:变量就是房子的门牌号
2.变量的申明
a = 10
这就就申明了一个变量,变量为 a,变量的值为10
3.变量的修改
In [21]: a = 10 In [22]: a Out[22]: 10 In [23]: a = 20 In [24]: a Out[24]: 20
变量的修改直接用新值覆盖掉以前的就可以
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)
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)
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)
从上面的三段代码可以看出,变量指向的永远是值,而不会指向变量,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)
但但但但但是,如果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)
不要惊讶(看你一副 没见过世面的样子,下面还有更精彩的),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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.
