python除法while_Python学习(三)while循环、字符串格式化、运算符 、编码

1、while循环

79d62eda52249cbf2294bf2ce0ec91ad.png

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

使用while打印1.2.3.4.5.6.8.9.10 #快速注释Ctrl+?

count= 1

while count <= 10:if count == 7:

count= count + 1pass #表示过,不执行下面程序else:

print(count)

count= count + 1print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

count = 1

while count <= 10:if count != 7: #!=表示不等于

print(count)

count= count + 1print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

错误示例

count= 1

while count <= 10 and count != 7:

print(count)

count= count + 1print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

关键字:break终止当前循环

continue #结束本次循环,直接开始下个循环

while True:#True首字需大写,否则报错name 'true' isnot defined(名称“true”没有定义)

print(666)break#终止当前循环

print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

通过break实现1~10

whileTrue:

print(count)if count==10:breakcount= count+1print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

whileTrue:

print('你好')whileTrue:

print(666)break

关键字:continue本次循环如果遇到continue,则不再继续下面程序,而是回到while条件位置

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

实现1234568910

count= 1

while count <= 10:if count == 7:

count= count + 1

continue

else:

print(count)

count= count + 1print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

课外知识:else在while中的应用(不需要记)

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

count = 1

while count <= 10:

print(count)

count= count + 1

continue

else:#当不再满足while后的条件时,触发。或 条件=False

print('else代码块')

print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

count = 1

whileTrue:

print(count)if count == 10:breakcount= count + 1

else:#当不再满足while后的条件时,触发。或 条件=False

print('else代码块')

print('end')

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

2、字符串格式化

%s:代指字符串

%d:代指数字

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

#字符串格式化存在的意义

name = input('姓名')

do= input('在干什么:')

template= "我是%s ,%s。"%(name,do,)print(template)

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

template = "我是%s ,年龄%d,职业%s。"%('alex',25,'engineer',)print(template)

特殊:当需要打印%时,要多加一个百分号(%%)

name = 'alex'template= '%s手机的电量是100%%'%(name)print(template)

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

name = input('请输入姓名:')

age= input('请输入年龄:')

job= input('请输入职业:')

hobby= input('请输入爱好:')

msg= '''---------info of alex li--------

name: %s

age: %s

job: %s

hobby: %s

---------end-------'''

print(msg%(name,age,job,hobby))

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

2-1:.format

name = '我叫{0},年龄{1}'.format('xxx',99)print(name)

3、运算符

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

输出1-100内的所有偶数

n=1

while n<101:

temp= n % 2#取模 - 返回除法的余数

if temp==0:print(n)else:passn=n+1

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

求1-2+3-4+5...99的所有数的和

n=0

a=0 #a是之前所有数的总和

while n<100:print(n)

temp=n%2

if temp==0:

a=a-nelse:

a=a+n

n=n+1

print(a)

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

Python算术运算符

以下假设变量a为10,变量b为21:

运算符

描述

实例

+

加 - 两个对象相加

a + b 输出结果 31

-

减 - 得到负数或是一个数减去另一个数

a - b 输出结果 -11

*

乘 - 两个数相乘或是返回一个被重复若干次的字符串

a * b 输出结果 210

/

除 - x 除以 y

b / a 输出结果 2.1

%

取模 - 返回除法的余数

b % a 输出结果 1

**

幂 - 返回x的y次幂

a**b 为10的21次方

//

取整除 - 向下取接近除数的整数

>>> 9//2

4

>>> -9//2

-5

Python赋值运算符

以下假设变量a为10,变量b为20:

运算符

描述

实例

=

简单的赋值运算符

c = a + b 将 a + b 的运算结果赋值为 c

+=

加法赋值运算符

c += a 等效于 c = c + a

-=

减法赋值运算符

c -= a 等效于 c = c - a

*=

乘法赋值运算符

c *= a 等效于 c = c * a

/=

除法赋值运算符

c /= a 等效于 c = c / a

%=

取模赋值运算符

c %= a 等效于 c = c % a

**=

幂赋值运算符

c **= a 等效于 c = c ** a

//=

取整除赋值运算符

c //= a 等效于 c = c // a

:=

海象运算符,可在表达式内部为变量赋值。Python3.8 版本新增运算符。

在这个示例中,赋值表达式可以避免调用 len() 两次:

if (n := len(a)) > 10:

print(f"List is too long ({n} elements, expected <= 10)")

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:

运算符

逻辑表达式

描述

实例

and

x and y

布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。

(a and b) 返回 20。

or

x or y

布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。

(a or b) 返回 10。

not

not x

布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

not(a and b) 返回 False

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

#对于or,如果遇到# 第一个值如果是转换成布尔值如果是真,则value=第一值;

# 第一个值如果是转换成布尔值如果是假,则value=第二值;

# 如果有多个or条件,则从左到右依次进行上述流程。

value1 = 0 or 1

value2= 8 or 10

value3= 0 or 9 or 8

print(value)

#答案:1、8、9

#对于and,如果遇到#第一个值如果是转换成布尔值如果是True,则value=第二值;#第一个值如果是转换成布尔值如果是False,则value=第一值;#如果有多个and条件,则从左到右依次进行上述流程。

value1 = 1 and 9value2= 1 and0

value3= 0 and 7value4= 0 and ""value5= 1 and 0 and 9

print(value)#答案:9、0、0、0、0、0

#综合#先看and在看or;如果有括号先算括号内容。优先级关系()>not>and>or,同一优先级从左往右计算。

value1 = 1 and 9 or 0 and 8

print(value)

#答案:9

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

补充

运算符补充

in

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

value = "我是中国人"

#判断‘中国’是否在value所代指的字符串中,‘中国’是否是value所代指的字符串的子序列

v1 = '中国' invalue#示例

content = input('请输入内容:')if '退钱' incontent:print('包含敏感字符:')##示例

whileTrue:

content= input('请输入内容:')if '退钱' incontent:print('包含敏感字符:')else:print(content)break

5e8dd2121e0e9068fe1be0a379ba8ff3.gif

not in

not 2 > 1

not 2 > 1 #错误

not 2 > 1 #正确

4、编码

ascii:

unicode:

ecs2

ecs4

utf-8:

utf-16:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值