第二节 变量和表达式

本节概要

  • 变量;
  • 常见数据类型;
  • 常见的运算符。

一、变量

1.变量的命名规则

  • 变量名可以包含 字母、数字和下划线;
  • 变量名可以以字母或下划线打头,但不能以数字打头。比如 var1 , 或者 _var
  • 变量名中不能包含空格;
  • 变量名 不能 和 关键字 相同。

2.Python常见关键字

print input
and or not in 
if elif else
for while continue break pass
global
def return 
from import
try except finally
class with None True False
lambda del raise assert lambda yield

二、常用的数据类型

  • 整型 int,比如 1
  • 浮点数 float,比如 1.2
  • 字符串 string,比如 'abcdef',"xyz"
  • 列表 List,比如 [1, 3, 5, 7, 9],['a', 'b', 'c']
  • 元组 Tuple,比如 (1, 3, 5, 7, 9),('a', 'b', 'c')
  • 字典 Dictionary,比如 {'张三':99, '李四':90 }

查看数据类型可以用内置函数 type(a)

例:

a = 'abc'
print(type(a)) # <class 'str'>

三、常见的运算符

1.算术运算符

运算符 意义
+
-
*
** 次方
/
// 取商
% 取余数

2.比较运算符

运算符 意义
< 小于
<= 小于等于
== 等于
>= 大于等于
> 大于
!= 不等于

注意:"="等号是用来赋值的。 "=="用来判断是否等于。

3.逻辑运算符

运算符 逻辑表达式 意义
and a==b and c==d
or a==b or b==c
not not a

4.其他运算符

运算符 意义 举例
in 在指定的序列中返回True a in [1, 2, 3]
not in 在在指定的序列中返回True a not in [1, 2, 3]

results matching ""

    No results matching ""