夜雨聆风学习资料网

ARTICLE · 1433820

20 条 Python 一行代码,从最基础开始(含双语详解)

20 条 Python 一行代码,从最基础开始(含双语详解)

本文面向零基础读者,从最简单的 print 开始,每条代码都配中英双语解释和可运行例子。建议按顺序阅读,不要跳读。


第 1 条:print("Hello")

English:This is the simplest possible program. print is a built-in function; the parentheses contain its argument, the string "Hello". The function writes the text to standard output and adds a newline at the end. Nothing is computed — it just displays text.

中文:这是最简单的程序。print 是内置函数,括号里是它的参数——字符串 "Hello"。函数把文本写到标准输出,并在末尾加一个换行符。它不做任何计算,只是显示文字。

print(”Hello”)# Hello

第 2 条:print(1)

English:Prints the integer 1. Unlike a string, the number is not wrapped in quotes; print converts any object to its text form automatically before displaying it.

中文:打印整数 1。与字符串不同,数字不加引号;print 在显示前会自动把任何对象转换成文本形式。

print(1)# 1

第 3 条:print(1 + 2)

English:The expression 1 + 2 is evaluated first (addition of two integers yields 3), and then print displays the result. This shows the difference between an expression (which produces a value) and a statement (which performs an action).

中文:先计算表达式 1 + 2(两个整数相加得 3),再由 print 显示结果。这体现了「表达式」(产生值)与「语句」(执行动作)的区别。

print(1 + 2)# 3

第 4 条:x = 5

English:Creates a variable named x and assigns it the value 5. The = is the assignment operator: it stores the value on the right into the name on the left. No output is produced, because assignment is a statement, not a function call.

中文:创建一个名为 x 的变量并赋值为 5。= 是赋值运算符:把右边的值存入左边的名字。这条语句不会产生输出,因为赋值是语句而非函数调用。

x = 5print(x)# 5

第 5 条:print(x)

English:Prints the value stored in the variable x. When print receives a name, Python looks up what that name currently refers to and displays that value.

中文:打印变量 x 中存储的值。当 print 收到一个名字时,Python 会查找该名字当前指向的值并显示出来。

x = 5print(x)# 5

第 6 条:y = x + 1

English:Reads the current value of x, adds 1, and stores the result in a new variable y. The right side is fully evaluated before assignment, so y becomes 6 when x is 5. Changing x later does not change y.

中文:读取 x 当前的值,加 1,把结果存入新变量 y。右侧会先完整求值再赋值,所以当 x 为 5 时 y 变为 6。之后修改 x 不会影响 y。

x = 5y = x + 1print(y)# 6

第 7 条:print("a", "b")

English:print accepts multiple arguments separated by commas. By default it separates them with a single space, so the output is a b. This is different from printing one concatenated string.

中文:print 可以接收多个用逗号分隔的参数。默认用单个空格分隔它们,所以输出为 a b。这与打印一个拼接好的字符串不同。

print(”a”, ”b”)# a b

第 8 条:print("a" + "b")

English:The + operator on two strings performs concatenation, joining them into one new string "ab". Then the single string is printed, so the output is ab with no space.

中文:两个字符串之间的 + 执行拼接,合并成一个新字符串 "ab"。随后打印这一个字符串,因此输出 ab,没有空格。

print(”a” + ”b”)# ab

第 9 条:name = input()

English:Calls the built-in input() function, which pauses the program, waits for the user to type a line and press Enter, and returns that line as a string. The string is then assigned to name. The result is always a string, even if the user types digits.

中文:调用内置 input() 函数,程序暂停,等待用户输入一行并回车,然后把该行作为字符串返回。这个字符串被赋给 name。结果始终是字符串,即使用户输入的是数字。

name = input()# 用户输入: Tomprint(name)# Tom

第 10 条:n = int(input())

English:input() returns a string; wrapping it in int() converts that string to an integer so arithmetic can be performed. If the text is not a valid integer, Python raises a ValueError.

中文:input() 返回字符串;用 int() 包裹可把该字符串转换成整数,以便进行算术运算。如果文本不是合法整数,Python 会抛出 ValueError。

n = int(input())# 用户输入: 7print(n + 1)# 8

第 11 条:print(10 / 3)

English:The / operator performs true division and always returns a float, even when the operands are integers. So 10 / 3 gives 3.3333333333333335. Use // if you want integer (floor) division.

中文:/ 执行真除法,即使操作数是整数也总是返回浮点数。因此 10 / 3 得到 3.3333333333333335。若想要整数(向下取整)除法,应使用 //。

print(10 / 3)# 3.3333333333333335print(10 // 3)# 3

第 12 条:print(10 % 3)

English:The % operator is the modulo operator: it returns the remainder after division. 10 % 3 is 1 because 3 goes into 10 three times with remainder 1. It's commonly used to test divisibility (e.g., n % 2 == 0 for even numbers).

中文:% 是取模运算符:返回除法后的余数。10 % 3 为 1,因为 3 除 10 得 3 余 1。它常用于判断整除性(如 n % 2 == 0 判断偶数)。

print(10 % 3)# 1

第 13 条:print(2 ** 10)

English:The ** operator means exponentiation (power). 2 ** 10 computes 2 raised to the 10th power, which is 1024. In many other languages you'd need a function like pow(), but Python has a dedicated operator.

中文:** 运算符表示幂运算。2 ** 10 计算 2 的 10 次方,结果为 1024。在很多其他语言中需要 pow() 这类函数,而 Python 有专门的运算符。

print(2 ** 10)# 1024

第 14 条:print(3 > 2)

English:A comparison expression. The > operator compares two values and returns a Boolean: True if the left is greater, otherwise False. Here 3 > 2 is True. Booleans are printed as the words True and False.

中文:比较表达式。> 运算符比较两个值并返回布尔值:左边更大则为 True,否则为 False。此处 3 > 2 为 True。布尔值打印为 True 和 False 这两个词。

print(3 > 2)# Trueprint(2 > 3)# False

第 15 条:print(True and False)

English:The and operator returns True only if both operands are truthy. Since False is one of them, the result is False. Python's and/or/not are keywords, not symbols like &&/||/!.

中文:and 运算符仅在两个操作数都为真时才返回 True。因为其中一个是 False,结果为 False。Python 的 and/or/not 是关键字,而非 &&/||/! 这类符号。

print(True and False)# Falseprint(True or False)# Trueprint(not True)# False

第 16 条:if x > 0: print("positive")

English:An if statement whose body is a single statement on the same line. If the condition x > 0 is true, print runs; otherwise nothing happens. The colon : marks the start of the block. For clarity, multi-line blocks are usually preferred.

中文:if 语句,其语句体与条件写在同一行。若条件 x > 0 为真则执行 print,否则什么也不做。冒号 : 标记语句块的开始。为清晰起见,通常更推荐多行写法。

x = 5if x > 0: print(”positive”)# positive

第 17 条:for i in range(3): print(i)

English:A for loop that iterates over range(3), which yields 0, 1, 2. For each value, i is bound to it and the loop body (print(i)) executes. range(n) produces numbers from 0 up to but not including n.

中文:for 循环遍历 range(3),它依次产生 0, 1, 2。每次迭代 i 被绑定到当前值,循环体(print(i))执行。range(n) 生成从 0 到 n-1(不含 n)的数字。

for i in range(3): print(i)# 0# 1# 2

第 18 条:while x > 0: x -= 1

English:A while loop that keeps running as long as the condition x > 0 is true. Each iteration decrements x by 1 (-= is shorthand for x = x - 1). The loop ends when x reaches 0, since 0 > 0 is false.

中文:while 循环在条件 x > 0 为真时持续运行。每次迭代把 x 减 1(-= 是 x = x - 1 的简写)。当 x 变为 0 时循环结束,因为 0 > 0 为假。

x = 3while x > 0: x -= 1print(x)# 0

第 19 条:print(len("hello"))

English:len() is a built-in function that returns the number of items in a sequence. For a string, it returns the number of characters, so len("hello") is 5. It works on lists, tuples, dictionaries, and other sized objects too.

中文:len() 是内置函数,返回序列中元素的个数。对于字符串,它返回字符数,所以 len("hello") 为 5。它同样适用于列表、元组、字典等有长度的对象。

print(len(”hello”))# 5

第 20 条:print("Hello" * 3)

English:The * operator between a string and an integer repeats the string that many times, producing "HelloHelloHello". This is a convenient way to build repeated text without a loop.

中文:字符串与整数之间的 * 运算符把字符串重复指定次数,得到 "HelloHelloHello"。这是不用循环就能构造重复文本的便捷方式。

print(”Hello” * 3)# HelloHelloHello

小结

这 20 条从最基础的输出与赋值开始,逐步引入算术、比较、布尔、输入、if/for/while、len() 和字符串重复,每条都是单行可执行代码,覆盖 Python 入门最核心的语法点。

学习建议:不要只看,打开 Python 交互环境逐条敲一遍;每敲一条,先猜输出,再回车验证。猜错的地方,就是你需要重点理解的地方。


如果这篇文章对你有帮助,欢迎点赞、在看、转发给正在学 Python 的朋友。

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料