数据类型和变量

原文: https://thepythonguru.com/datatype-varibles/


于 2020 年 1 月 7 日更新


变量是命名位置,用于存储对内存中存储的对象的引用。 我们为变量和函数选择的名称通常称为标识符。 在 Python 中,标识符必须遵守以下规则。

  1. 所有标识符都必须以字母或下划线(_)开头,您不能使用数字。 例如:my_var是有效的标识符,但1digit不是。
  2. 标识符可以包含字母,数字和下划线(_)。 例如:error_404_save是有效的标识符,但$name$(不允许$)和#age(不允许#)是无效的标识符。
  3. 它们可以是任何长度。
  4. 标识符不能是关键字。 关键字是 Python 用于特殊目的的保留字)。 以下是 Python 3 中的关键字。
  1. False class finally is return
  2. None continue for lambda try
  3. True def from nonlocal while
  4. and del global not with
  5. as elif if or yield
  6. pass else import assert
  7. break except in raise

给变量赋值


值是程序可以使用的基本东西。 例如:1113.14"hello"均为值。 在编程术语中,它们通常也称为字面值。 字面值可以是不同的类型,例如111int类型,3.14float"hello"string。 记住,在 Python 中,所有东西都是对象,甚至是基本数据类型,例如 int,float,string,我们将在后面的章节中对此进行详细说明。

在 Python 中,您不需要提前声明变量类型。 解释器通过包含的数据自动检测变量的类型。 要将值赋给变量等号(=)。 =符号也称为赋值运算符。

以下是变量声明的一些示例:

  1. x = 100 # x is integer
  2. pi = 3.14 # pi is float
  3. empname = "python is great" # empname is string
  4. a = b = c = 100 # this statement assign 100 to c, b and a.

试试看:

  1. x = 100 # x is integer
  2. pi = 3.14 # pi is float
  3. empname = "python is great" # empname is string
  4. a = b = c = 100 # this statement assign 100 to c, b and a.
  5. print(x) # print the value of variable x
  6. print(pi) # print the value of variable pi
  7. print(empname) # print the value of variable empname
  8. print(a, b, c) # print the value of variable a, b, and c, simultaneously

提示

将值分配给变量后,该变量本身不存储值。 而是,变量仅将对象的引用(地址)存储在内存中。 因此,在上面的清单中,变量x存储对100int对象)的引用(或地址)。 变量x本身不存储对象100

注释


注释是说明程序目的或程序工作方式的注释。 注释不是 Python 解释器在运行程序时执行的编程语句。 注释也用于编写程序文档。 在 Python 中,任何以井号(#)开头的行均被视为注释。 例如:

  1. # This program prints "hello world"
  2. print("hello world")

试一试:

  1. # This program prints "hello world"
  2. print("hello world")

在此清单中,第 1 行是注释。 因此,在运行程序时,Python 解释器将忽略它。

我们还可以在语句末尾写注释。 例如:

  1. # This program prints "hello world"
  2. print("hello world") # display "hello world"

当注释以这种形式出现时,它们称为最终注释。

试一试:

  1. # This program prints "hello world"
  2. print("hello world") # display hello world

同时赋值


同时赋值或多重赋值允许我们一次将值赋给多个变量。 同时分配的语法如下:

  1. var1, var2, ..., varn = exp1, exp2, ..., expn

该语句告诉 Python 求值右边的所有表达式,并将它们分配给左边的相应变量。 例如:

  1. a, b = 10, 20
  2. print(a)
  3. print(b)

试一试:

  1. a, b = 10, 20
  2. print(a)
  3. print(b)

当您想交换两个变量的值时,同时分配非常有帮助。 例如:

  1. >>> x = 1 # initial value of x is 1
  2. >>> y = 2 # initial value of y is 2
  3. >>> y, x = x, y # assign y value to x and x value to y

预期输出

  1. >>> print(x) # final value of x is 2
  2. 2
  3. >>> print(y) # final value of y is 1
  4. 1

试一试:

  1. x = 1 # initial value of x is 1
  2. y = 2 # initial value of y is 2
  3. y, x = x, y # assign y value to x and x value to y
  4. print(x) # final value of x is 2
  5. print(y) # final value of y is 1

Python 数据类型


Python 即有 5 种标准数据类型。

  1. 数字
  2. 字符串
  3. 列表
  4. 元组
  5. 字典
  6. 布尔值 - 在 Python 中,TrueFalse是布尔字面值。 但是以下值也被认为是False
    • 0 - 00.0
    • [] - 空列表,()-空元组,{}-空字典,''
    • None

从控制台接收输入


input()函数用于从控制台接收输入。

语法input([prompt]) -> string

input()函数接受一个名为prompt的可选字符串参数,并返回一个字符串。

  1. >>> name = input("Enter your name: ")
  2. >>> Enter your name: tim
  3. >>> name
  4. 'tim'

试一试:

  1. name = input("Enter your name: ")
  2. print(name)

请注意,即使输入了数字,input()函数也始终返回字符串。 要将其转换为整数,可以使用int()eval()函数。

  1. >>> age = int(input("Enter your age: "))
  2. Enter your age: 22
  3. >>> age
  4. 22
  5. >>> type(age)
  6. <class 'int'>

试一试:

  1. age = int(input("Enter your age: "))
  2. print(age)
  3. print(type(age))

导入模块


Python 使用模块组织代码。 Python 随附了许多内置模块,可用于例如数学相关函数的math模块,正则表达式的re模块,与操作系统相关的函数的os模块等。

要使用模块,我们首先使用import语句将其导入。 其语法如下:

  1. import module_name

我们还可以使用以下语法导入多个模块:

  1. import module_name_1, module_name_2

这是一个例子

  1. >>> import math, os
  2. >>>
  3. >>> math.pi
  4. 3.141592653589793
  5. >>>
  6. >>> math.e
  7. 2.718281828459045
  8. >>>
  9. >>>
  10. >>> os.getcwd() # print current working directory
  11. >>> '/home/user'
  12. >>>

试一试:

  1. import math, os
  2. print(math.pi)
  3. print(math.e)
  4. print(os.getcwd())

在此清单中,第一行导入了mathos模块中定义的所有函数,类,变量和常量。 要访问模块中定义的对象,我们首先编写模块名称,后跟点(.),然后编写对象本身的名称。 (即类或函数或常量或变量)。 在上面的示例中,我们从math数学访问两个常见的数学常数pie。 在下一行中,我们将调用os模块的getcwd()函数,该函数将打印当前工作目录。

在下一章中,我们将介绍 Python 中的数字