Python变量
Python变量的定义方法与C语言的变量定义方法一致,
变量的命名
---- 变量名有字母、数字、下划线组合。
----- 数字不能开头
----- 不可以使用关键字。
------ a a1 a_ a_1
变量的赋值
变量赋值 等号 =
---- 是变量的申明和定义的过程
a = 1
Id(a) 取 a的地址
由图可知 两次赋值后 a 指向的位置变了, 所以变量只是一个标签, 只是对变量在内存中实际引用,
python的运算符与表达式
案例 写一个自己的四则运算器
running = True
while running:
try:
t=int(input("please input the fist count:"))
p=int(input("please input the second count:"))
except EOFError:
break
print("operator + result:", t+p)
print("operator - result:", t-p)
print("operator * result:", t*p)
print("operator / result:", t/p)
print('Hello World')
while running:
try:
t=int(input("please input the fist count:"))
p=int(input("please input the second count:"))
except EOFError:
break
print("operator + result:", t+p)
print("operator - result:", t-p)
print("operator * result:", t*p)
print("operator / result:", t/p)
print('Hello World')
Python运算符包括
--- 赋值运算符
---- 算术运算符
----关系运算符
----逻辑运算符
表达式是将不同的数据(包含变量、函数)用运算符按一定规则连接起来的一种式子
赋值运算符
“=" 等于: x=3, y="abcde"
"+="加等于: x+=2
’-=“ 减等于:x-=2
"*="乘等于:x*=2
"/=" 处等于:x/=2
"求余等于: x %=2
算术运算符
”+“ 加法: x+y
"-" 减法 :x-y
"*" 乘法 : x*y
"/" 实数除法: 3/2 3.0/2
"//" 整数除法:5.6//2 为 2.0 5.6/2 为2.8
”%"求余数:除法求余运算 例如: 17%7 结果为3
”**" 求幂运算: 2**3 = 8
关系运算符(与布尔类型连用 与c语言类似)
“<" 小于
">" 大于
"<=" 小于等于
">=" 大于等于
"!=" 不等于
"==" 完全等于
逻辑运算符(连接多个比较结果)
"and" 逻辑与:True and False
"or" 逻辑或: True or Flase
"not”逻辑非: not True
其他运算符:
按位与:& 按位或:| 按位与或:
按位翻转: ~x
Lambda
同一性测试:is , is not
成员测试:in, not in