python的基本语术_Python基础语法

r''

表示''内部的字符串默认不转义>>> print('\\\t\\')

\ \

>>> print(r'\\\t\\')

\\\t\\

'''...'''

表示多行内容>>> print('''line1

... line2

... line3''')

line1

line2

line3

布尔值

只有True、False两种值(请注意大小写)。

and、or和not运算

and是与运算>>> True and True

True

>>> True and False

False

>>> False and False

False

>>> 5 > 3 and 3 > 1

True

or是或运算>>> True or True

True

>>> True or False

True

>>> False or False

False

>>> 5 > 3 or 1 > 3

True

not是非运算>>> not True

False

>>> not False

True

>>> not 1 > 2

True

空值

用None表示,None不能理解为0,因为0是有意义的,而None是一个特殊的空值。

除法

/

除法计算结果是浮点数,即使是两个整数恰好整除,结果也是浮点数:>>> 10 / 3

3.3333333333333335

>>> 9 / 3

3.0

地板除,两个整数的除法仍然是整数:>>> 10 // 3

3

%

得到两数相除的余数>>> 10 % 3

1

ifif <条件判断1>:

<执行1>

elif <条件判断2>:

<执行2>

elif <条件判断3>:

<执行3>

else:

<执行4>

if判断条件还可以简写,比如写:只要x是非零数值、非空字符串、非空list等,就判断为True,否则为False。if x:

print('True')

fornames = ['Michael', 'Bob', 'Tracy']

for name in names:

print(name)

sum = 0

for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:

sum = sum + x

print(sum)

--->

sum = 0

for x in range(11):

sum = sum + x

print(sum) //55 0-10的和

>>> list(range(5))

[0, 1, 2, 3, 4]

while break continuen = 1

while n <= 100:

if n > 10: # 当n = 11时,条件满足,执行break语句

break # break语句会结束当前循环

print(n)

n = n + 1

print('END')

n = 0

while n < 10:

n = n + 1

if n % 2 == 0: # 如果n是偶数,执行continue语句

continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行

print(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值