于 2020 年 1 月 7 日更新
变量是命名位置,用于存储对内存中存储的对象的引用。 我们为变量和函数选择的名称通常称为标识符。 在 Python 中,标识符必须遵守以下规则。
- 所有标识符都必须以字母或下划线(
_)开头,您不能使用数字。 例如:my_var是有效的标识符,但1digit不是。 - 标识符可以包含字母,数字和下划线(
_)。 例如:error_404,_save是有效的标识符,但$name$(不允许$)和#age(不允许#)是无效的标识符。 - 它们可以是任何长度。
- 标识符不能是关键字。 关键字是 Python 用于特殊目的的保留字)。 以下是 Python 3 中的关键字。
False class finally is returnNone continue for lambda tryTrue def from nonlocal whileand del global not withas elif if or yieldpass else import assertbreak except in raise
给变量赋值
值是程序可以使用的基本东西。 例如:1,11,3.14和"hello"均为值。 在编程术语中,它们通常也称为字面值。 字面值可以是不同的类型,例如1,11是int类型,3.14是float和"hello"是string。 记住,在 Python 中,所有东西都是对象,甚至是基本数据类型,例如 int,float,string,我们将在后面的章节中对此进行详细说明。
在 Python 中,您不需要提前声明变量类型。 解释器通过包含的数据自动检测变量的类型。 要将值赋给变量等号(=)。 =符号也称为赋值运算符。
以下是变量声明的一些示例:
x = 100 # x is integerpi = 3.14 # pi is floatempname = "python is great" # empname is stringa = b = c = 100 # this statement assign 100 to c, b and a.
试试看:
x = 100 # x is integerpi = 3.14 # pi is floatempname = "python is great" # empname is stringa = b = c = 100 # this statement assign 100 to c, b and a.print(x) # print the value of variable xprint(pi) # print the value of variable piprint(empname) # print the value of variable empnameprint(a, b, c) # print the value of variable a, b, and c, simultaneously
提示:
将值分配给变量后,该变量本身不存储值。 而是,变量仅将对象的引用(地址)存储在内存中。 因此,在上面的清单中,变量x存储对100(int对象)的引用(或地址)。 变量x本身不存储对象100。
注释
注释是说明程序目的或程序工作方式的注释。 注释不是 Python 解释器在运行程序时执行的编程语句。 注释也用于编写程序文档。 在 Python 中,任何以井号(#)开头的行均被视为注释。 例如:
# This program prints "hello world"print("hello world")
试一试:
# This program prints "hello world"print("hello world")
在此清单中,第 1 行是注释。 因此,在运行程序时,Python 解释器将忽略它。
我们还可以在语句末尾写注释。 例如:
# This program prints "hello world"print("hello world") # display "hello world"
当注释以这种形式出现时,它们称为最终注释。
试一试:
# This program prints "hello world"print("hello world") # display hello world
同时赋值
同时赋值或多重赋值允许我们一次将值赋给多个变量。 同时分配的语法如下:
var1, var2, ..., varn = exp1, exp2, ..., expn
该语句告诉 Python 求值右边的所有表达式,并将它们分配给左边的相应变量。 例如:
a, b = 10, 20print(a)print(b)
试一试:
a, b = 10, 20print(a)print(b)
当您想交换两个变量的值时,同时分配非常有帮助。 例如:
>>> x = 1 # initial value of x is 1>>> y = 2 # initial value of y is 2>>> y, x = x, y # assign y value to x and x value to y
预期输出:
>>> print(x) # final value of x is 22>>> print(y) # final value of y is 11
试一试:
x = 1 # initial value of x is 1y = 2 # initial value of y is 2y, x = x, y # assign y value to x and x value to yprint(x) # final value of x is 2print(y) # final value of y is 1
Python 数据类型
Python 即有 5 种标准数据类型。
- 数字
- 字符串
- 列表
- 元组
- 字典
布尔值 - 在 Python 中,
True和False是布尔字面值。 但是以下值也被认为是False。0-0,0.0[]- 空列表,()-空元组,{}-空字典,''None
从控制台接收输入
input()函数用于从控制台接收输入。
语法: input([prompt]) -> string
input()函数接受一个名为prompt的可选字符串参数,并返回一个字符串。
>>> name = input("Enter your name: ")>>> Enter your name: tim>>> name'tim'
试一试:
name = input("Enter your name: ")print(name)
请注意,即使输入了数字,input()函数也始终返回字符串。 要将其转换为整数,可以使用int()或 eval()函数。
>>> age = int(input("Enter your age: "))Enter your age: 22>>> age22>>> type(age)<class 'int'>
试一试:
age = int(input("Enter your age: "))print(age)print(type(age))
导入模块
Python 使用模块组织代码。 Python 随附了许多内置模块,可用于例如数学相关函数的math模块,正则表达式的re模块,与操作系统相关的函数的os模块等。
要使用模块,我们首先使用import语句将其导入。 其语法如下:
import module_name
我们还可以使用以下语法导入多个模块:
import module_name_1, module_name_2
这是一个例子
>>> import math, os>>>>>> math.pi3.141592653589793>>>>>> math.e2.718281828459045>>>>>>>>> os.getcwd() # print current working directory>>> '/home/user'>>>
试一试:
import math, osprint(math.pi)print(math.e)print(os.getcwd())
在此清单中,第一行导入了math和os模块中定义的所有函数,类,变量和常量。 要访问模块中定义的对象,我们首先编写模块名称,后跟点(.),然后编写对象本身的名称。 (即类或函数或常量或变量)。 在上面的示例中,我们从math数学访问两个常见的数学常数pi和e。 在下一行中,我们将调用os模块的getcwd()函数,该函数将打印当前工作目录。
在下一章中,我们将介绍 Python 中的数字。
