Python学习系列《二》【变量和基本数据类型】

古城微笑少年丶 2021-09-22 13:48 539阅读 0赞

二、变量和简单数据类型

2.1 变量的命名规则

  1. Python中变量的命名需要遵守以下规则:

(1)变量名只能包含字母、数字和下划线,且不能以数字开头。例如dxc1994、_num等都是合法的,2x、&age这种是不合法的;

(2)不能使用Python的关键字和函数名用作变量名。关键字自不用说,函数名尤其是Python自带的一些内置函数,将内置函数名用作变量名称时,由于函数名称被占用,导致这些函数的行为被覆盖了,这些函数就无法使用了。这里举个栗子:Python内置求最大值函数 max();

  1. max()使用,E:\\test.py代码如下:
  2. max_num = max(100,200,123)
  3. print("max_num=",max_num)

输出结果为:

70

现在,我们首先定义一个变量叫做max,给它赋值之后,再使用max()函数时就会报错了,如下:

  1. max =12
  2. max_num = max(100,200,123)
  3. print("max_num=",max_num)

Python解释器会报如下错误:

70 1

所以不应该使用函数名作为变量名称。

(3)变量名中不能包含有空格。

2.2 简单数据类型

2.2.1 整数

  1. Python中,可以对整数执行简单的加(+)减(-)乘(\*)除(/)等运算:

70 2

需要注意的是除(/)运算,Python 3中整数除以整数计算的结果是浮点数,Python 2中结果是整数。还有一种,就是其他语言中常见的整数除法,称之为地板除(//),二者区别见如下代码执行效果:

70 3

可见地板除(//),直接将结果的小数点抹去了。

2.2.2 浮点数

  1. 浮点数就是我们常用的小数,但是使用时需要注意一下几点:

(1)浮点数运算结果的位数可能是不确定的:

70 4

(2)可以使用科学计数法表示浮点数,如0.0000016可以记为1.6e-6,120000000.0可以表示为1.2E8,如下:

70 5

2.2.3 字符串

  1. Python的字符串,可以用单引号,也可以使用双引号:
  2. >>> print('hello world')
  3. hello world
  4. >>> print("hello world")
  5. hello world
  6. >>>
  7. 字符串拼接使用加号(+):
  8. >>> greet = "hello," + "my name is LingYingQiaoRen."
  9. >>> print(greet)
  10. hello,my name is LingYingQiaoRen.
  11. >>>
  12. 字符串有一些常用函数,如upper()起全部大写作用,lower()起全部小写作用,title()起首字母大写作用等,但这些函数并不改变原有的参数实际值:
  13. >>> greet = "hello," + "my name is LingYingQiaoRen."
  14. >>> print(greet)
  15. hello,my name is LingYingQiaoRen.
  16. >>> print(greet.upper())
  17. HELLO,MY NAME IS LINGYINGQIAOREN.
  18. >>> print(greet.lower())
  19. hello,my name is lingyingqiaoren.
  20. >>> print(greet.title())
  21. Hello,My Name Is Lingyingqiaoren.
  22. >>> print(greet)
  23. hello,my name is LingYingQiaoRen.
  24. >>>

还有左侧删除空白函数lstrip(),右侧删除空白函数rstrip(),两端删除空白函数strip(),这些函数也不改变原有的参数值:

  1. >>> string = ' python '
  2. >>> print(string.lstrip())
  3. python
  4. >>> print(string.rstrip())
  5. python
  6. >>> print(string.strip())
  7. python
  8. >>> print(string)
  9. python
  10. >>>

2.2.4 布尔类型

  1. Python中的布尔类型只有TrueFalse(注意这两个单词的大小写,全部小写和全部大写都是错误的),与运算符(and)、或运算符(or)和非运算符(not),这三个运算符只能是小写。
  2. >>> 3 > 2
  3. True
  4. >>> 3 == 2
  5. False
  6. >>> 3 > 2 or 1 == 2
  7. True
  8. >>> not true
  9. Traceback (most recent call last):
  10. File "<stdin>", line 1, in <module>
  11. NameError: name 'true' is not defined
  12. >>> not TRUE
  13. Traceback (most recent call last):
  14. File "<stdin>", line 1, in <module>
  15. NameError: name 'TRUE' is not defined
  16. >>> not True
  17. False
  18. >>> NOT True
  19. File "<stdin>", line 1
  20. NOT True
  21. ^
  22. SyntaxError: invalid syntax
  23. >>> 3 > 2 OR 1 == 2
  24. File "<stdin>", line 1
  25. 3 > 2 OR 1 == 2
  26. ^
  27. SyntaxError: invalid syntax
  28. >>> 3 > 2 Or 1 == 2
  29. File "<stdin>", line 1
  30. 3 > 2 Or 1 == 2
  31. ^
  32. SyntaxError: invalid syntax
  33. >>> not False
  34. True
  35. >>> True and False
  36. False
  37. >>> True or False
  38. True
  39. >>>

2.2.5 空值

  1. Python中的空值用None表示:
  2. >>> var = None
  3. >>> print(var)
  4. None
  5. >>>

2.3 注释

Python中的单行注释使用井号(#)标识,,多行注释使用三个单引号 ‘’’ 或者三个双引号 “”” 将注释括起来:

  1. #这是注释
  2. import sys
  3. """
  4. 这也是注释
  5. """
  6. i = sys.maxsize
  7. '''
  8. 这还是注释
  9. '''
  10. print(i)
  11. print("end !")

输出结果为:

  1. C:\Windows\system32>python E:\test.py
  2. 9223372036854775807
  3. end !

发表评论

表情:
评论列表 (有 0 条评论,539人围观)

还没有评论,来说两句吧...

相关阅读