2算符和表达式

2020-03-10  本文已影响0人  萌二宝

算符是连接一个或多个元素的符号,用来表达计算。

常见算符

运算按种类可分为算术运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算.

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
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

表达式

表达式为一串由算符连接的元素

执行流控制

执行流控制代码控制流的先后执行顺序

逻辑行和物理行

Python语句执行以行为单位。每行可选的可以以分号;结尾。
但是物理上的一行可以包含多个执行单位。

单行不超过 80 个字符

格式对齐

python 严格对齐 缩进不同 逻辑不同

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工具: 基于命令行的代码调试工具

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 i in range(1,5): # 前闭后开区间
    print(i)
else:
    print('The for loop is over')
------------------------------------
1
2
3
4
The for loop is over
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

今天你又博学了么?

上一篇 下一篇

猜你喜欢

热点阅读