算符是连接一个或多个元素的符号,用来表达计算。
常见算符
运算按种类可分为算术运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算.
- 算数运算符
a = 10;
b = 20;
print(a + b); # 30
print (a - b); #-10
print (a * b); #200
print (b/a);#2
print(b%a);#0取余
print(a**b);#求10的20次方
print(b // a);#整除没有小数
----------------------------------------------
30
-10
200
2.0
0
100000000000000000000
2
- 比较运算符
a = 10;
b = 20;
print(a == b); # 比较是否相等
print (a != b); #不相等
print (a > b); #
print (a < b);#
print(a <= b);#
print(a >= b);#
----------------------------------------
False
True
False
True
True
False
- 赋值运算符
a = 10;
b = 20
c = a+b;
print("赋值运算符={0}".format(c));
c += a;
print("加法赋值运算符c={0},等效于c=c+a".format(c));
c -= a; #运算前此时C = 40啦
print("减法赋值运算符c={0},等效于c=c-a".format(c));
c *= a;
print("乘法赋值运算符c={0},等效于c=ca".format(c));
c /= a;#运算前此时C = 300啦
print("除法赋值运算符c={0},等效于c=c/a".format(c));
c %= a;
print("取余赋值运算符c={0},等效于c=c%a".format(c));
c = 2;
c **=a;
print("幂赋值运算符c={0},等效于c=**a".format(c));
c = 25;
c //= a;
print("取整除赋值运算符c={0},等效于c=c//a".format(c));
-------------------------------------------------
赋值运算符=30
加法赋值运算符c=40,等效于c=c+a
减法赋值运算符c=30,等效于c=c-a
乘法赋值运算符c=300,等效于c=ca
除法赋值运算符c=30.0,等效于c=c/a
取余赋值运算符c=0.0,等效于c=c%a
幂赋值运算符c=1024,等效于c=**a
取整除赋值运算符c=2,等效于c=c//a
-
逻辑运算符
- and 布尔与
and 前项为真看后项
x and y, x为真,值是y,x为假,值是x - or 布尔或
or 前项为真即前项
x or y, x为真,值就是x,x为假,值是y - not 布尔非
在没有()情况下 not 优先级高于 and, and 优先级高于 or,即优先级关系为()> not > and > or, 同意优先级从左到右进行计算。
- and 布尔与
-
成员运算
判断子元素是否在原字符串(字典、列表、集合)中- in
- not in
print ('喜欢' in 'sldkfjlkwje喜欢slkdfjlksjldf');
print ('a' in 'asfe');
print ('y' not in 'sssssss');
print ('y' not in 'ssyssssssss');
--------------------------------
True
True
True
False
格式化字符串
将变量的值格式化到字符串中,形成新的字符串。常用于输出数据内容
name = 'coco';
print('my name is {0}, and the length of name if {1}'.format(name, len(name)));
print('name is %s , length is %d' % (name, len(name)));
------------------------------------
my name is coco, and the length of name if 4
name is coco , length is 4
-
format的标准格式化格式
‘{0}{1}’.format(a,b)前面0、1 实际就是对应括号里面的a,b不过在很长字符串的时候一般使用
'{a}{b}'.format(a=xxx,b='xxx') -
%d表示按整型数据的实际长度输出数据。
-
%c用来输出一个字符。
-
%s用来输出一个字符串。
-
%x表示以十六进制数形式输出整数
表达式
表达式为一串由算符连接的元素
执行流控制
执行流控制代码控制流的先后执行顺序
逻辑行和物理行
Python语句执行以行为单位。每行可选的可以以分号;结尾。
但是物理上的一行可以包含多个执行单位。
单行不超过 80 个字符
格式对齐
python 严格对齐 缩进不同 逻辑不同
- if
number = 23
guess = int (input('Please enter an integer:'))
if guess == number:
print('Congratulations! You win.')
elif guess < number:
print ('higher,please')
else:
print('lower,please')
print('DONE') #注意缩进不同,逻辑不同
Pdb工具: 基于命令行的代码调试工具
- while
number = 23
running = True
while running:
guess = int (input('Please enter an integer:'))
if guess == number:
print('Congratulations! You win.')
running = False
elif guess < number:
print ('higher,please')
else:
print('lower,please')
else:
print('The while loop is over')
print('DONE')
---------------------------------
Please enter an integer:2
higher,please
Please enter an integer:20
higher,please
Please enter an integer:32
lower,please
Please enter an integer:23
Congratulations! You win.
The while loop is over
DONE
- for
for i in range(1,5): # 前闭后开区间
print(i)
else:
print('The for loop is over')
------------------------------------
1
2
3
4
The for loop is over
- break
number = 23
running = True
for i in range (1, 10):
print(i)
if (i > 5):
print('{0}---break了?'.format(i))
break
print('break你执行了')
print('DONE')
----------------------------------------------
1
2
3
4
5
6
6---break了?
DONE
今天你又博学了么?
网友评论