#######################总结#############

1. 循环

while 条件:
循环体(break, continue)
循环的执行过程:
执行到while的时候. 首先判断条件是否成立.如果成立. 执行循环体. 再一次判断条件.... 如果不成立. 直接跳出循环

break 跳出当前本层循环
continue 结束本次循环. 继续执行下一次循环

2. 格式化输出

%s 占位字符串(常用)

%d 占位数字

%f 占位浮点数

name = input('你叫什么名字:')
add = input('你来自哪里:')
age =int(input('年龄:'))
notlike=input('请输入你不喜欢的明星')
# print("我叫"+name,'我来自'+add,'老婆'+wife,'不喜欢的明星:'+notlike) # print('我叫 %s,来自%s,年龄%d 不喜欢的明星%s' %(name,add,age,notlike ))
#
# print(f'我叫{name},来自{add},年龄{age} 不喜欢的明星{notlike}') #"{1} {0} {1}".format("hello", "world") # 设置指定位置

3. 运算符

!=
+= 累加 a += b  a = a + b

and 并且, 左右两端同时为真. 结果才能是真
or 或者, 左右两端有一个是真. 结果就是真
not 非. 非真既假, 非假既真

顺序: () => not => and => or

x and y   x为真返回y  x为假返回x

x  or  y   x为真返回真 x为假返回y

x or y => if x == 0 then y else x
and和or相反

4. 编码

1. ascii 8bit 1byte 包含了 英文, 数字, 特殊字符, 特殊操作符
2. gbk 16bit 2byte 主要包含的: 中文, 日文, 韩文, 繁体中文
3. unicode 32bit 4byte 万国码
4. utf-8
英文: 8bit 1byte
欧洲: 16bit 2byte
中文: 24bit 3byte

########################作业  ###################

1、判断下列逻辑语句的True,False.

1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

True

False

2、求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7
2),0 or 2 and 3 and 4 or 6 and 0 or 3

8

4

3、下列结果是什么?
1)、6 or 2 > 1

6

2)、3 or 2 > 1

3

3)、0 or 5 < 4

0

4)、5 < 4 or 3

3

5)、2 > 1 or 6

1

6)、3 and 2 > 1

1

7)、0 and 3 > 1

0

8)、2 > 1 and 3

3

9)、3 > 1 and 0

0

10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

2

4、while循环语句基本结构?
while 条件:
循环体(break, continue)

5、利⽤while语句写出猜⼤⼩的游戏:
设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
正确,然后退出循环。

while True:
content =int(input('请输入数字'))
if content > 66:
print('你输入的结果大了')
elif content < 66:
print('你输入的结果小了')
else:
print('你才猜了')
break

6、在5题的基础上进行升级:
给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。

number=66
count=3
while True:
content =int(input('请输入数字'))
if content > number:
print('你输入的结果大了')
elif content < number:
print('你输入的结果小了')
else:
print('你才猜了')
count -= 1
if count == 0:
print('你太笨了')
break number=66
count=1
while count<=3:
content =int(input('请输入数字'))
if content > number:
print('你输入的结果大了')
count+=1
continue
elif content < number:
print('你输入的结果小了')
count += 1
continue
else:
print('你猜对了')
count += 1
continue
print("已经 猜完了3次")

7.使用while循环输入1 2 3 4 5 6 8 9 10

number =0
while number < 10:
number+=1
print(number)

8.求1-100的所有数的和

sum = 0
number =1
while number <= 100:
sum=sum+number
number=number+1
print(sum)

9.输出 1-100 内的所有奇数

count = 1
while count <= 100:
if count % 2 == 1:
print(count)
count +=1

10.输出 1-100 内的所有偶数

count = 1
while count <= 100:
if count % 2 == 0:
print(count)
count += 1

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

count = 1
sum = 0
while count < 100:
if count %2 == 0:
sum -= count
#-2 -6 -12 -20 30
else:
sum += count
#+1 +3 +5
count += 1
print(sum)

12.用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤
字符串格式化)

count=0
number=3
while number:
number=number-1
user=input('请输入用户名:')
password=input('请输入密码:')
if user=='admin' and password=='':
print('输入正确')
else:
print('用户名或密码错误,你还剩余%s 次' %(number))

13. 用户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题).
2 3 5 7 11 13

1 2
1 3
1 5
1 7
1 11
1 13

num = int(input('请输入一个数字:'))
if num <= 1:
print('这不是质数')
elif num == 2:
print('这是一个质数!')
else:
i=2
while i < num:
if num%i == 0:
print('这不是一个质数')
break
i += 1
else:
print ('这是一个质数!')
#方法二
n=int(input('请输入一个数字:'))
for i in range(2,n):
if n %i ==0:
print('不是质数')
break #有了break就不会执行后面的else
else:
print('是一个质数')

14. 输个告标语. 判断这个公告是否合法. 根据最新的公告法来判断. ⼴
告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包
含. 提示, 广告不合法
例如, 1. ⽼男孩python世界第⼀. ==> 不合法
    2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法

content = input('请输入')
if '最'in content or '第⼀'in content or '稀缺' in content or '国家级'in content:
print('⽼男孩python世界第⼀')
else:
print('今年过年不收礼啊. 收礼只收脑')

14. 输入数字. 判断这个数是几位数(⽤算法实现)(升级题)

i = 0
num = int(input('请输入一个数:')) while num >= 1:
num = num // 10
i += 1
print('这个数是%s位数'% i)

明⽇默写代码:
1. 求1-100之间所有的数的和

number=0
sum=0
while number < 100:
number+=1
sum = number + sum
print(sum)

2. And or not的含义和特征

and 变量 a 和 b 都为 true 则为真
or 变量 a 和 b 都为 true,或其中一个变量为 true
not 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

3. break continue的含义. 有什么区别

#continue 停止当前本次循环,继续执行下一次循环
#break 彻底干掉一个循环

python while 格式化 运算符 编码的更多相关文章

  1. 老贾的幸福生活day5 while循环 格式化 运算符 编码初识

    while 循环 死循环 while 条件: print(结果) while 条件: print(结果) else: print(结果) break 终止当前循环 continue 跳出当前循环,进行 ...

  2. 记录我的 python 学习历程-Day02-while 循环/格式化输出/运算符/编码的初识

    一.流程控制之--while 循环 循环就是重复做同一件事,它可以终止当前循环,也可以跳出这一次循环,继续下一次循环. 基本结构(基本循环) while 条件: 循环体 示例 # 这是一个模拟音乐循环 ...

  3. Python基础篇(格式化输出,运算符,编码):

    Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s  替换字符串      %d 替换整体数字  ...

  4. python字符串格式化之学习笔记

    在python中格式化输出字符串使用的是%运算符,通用的形式为 •格式标记字符串 % 要输出的值组其中,左边部分的”格式标记字符串“可以完全和c中的一致.右边的'值组'如果有两个及以上的值则需要用小括 ...

  5. 六 Python基础 字符串和编码

    字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...

  6. 7. python 字符串格式化方法(2)

    7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...

  7. python字符串,常用编码

    Python的字符串和编码 1.常用编码 与python有关的编码主要有:ASCII.Unicode.UTF-8 其中ASCII如今可以视作UTF-8的子集 内存中统一使用Unicode编码(如记事本 ...

  8. python基础之运算符

    算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 31 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -11 * 乘 - 两个数相乘或是返回一个被重复 ...

  9. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

随机推荐

  1. 洛谷P4513 小白逛公园

    区间最大子段和模板题.. 维护四个数组:prefix, suffix, sum, tree 假设当前访问节点为cur prefix[cur]=max(prefix[lson],sum[lson]+pr ...

  2. mysql查询同一个字段下,不同内容的语句

    太久没有用SQL语句都有些忘记了,今天工作中遇到了那就尝试记录一下吧 需求是这样的:想查询同一个字段下,两条指定了不同内容,的其他的值 主要是要想到用where......in 语句如下:select ...

  3. python3,打印一年的某一天是一年的第几天

    year = int(input('year:')) month = int(input('month:')) day = int(input('day:')) months = (0,31,59,9 ...

  4. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  5. 【agc030f】Permutation and Minimum(动态规划)

    [agc030f]Permutation and Minimum(动态规划) 题面 atcoder 给定一个长度为\(2n\)的残缺的排列\(A\),定义\(b_i=min\{A_{2i-1},A_{ ...

  6. 【BZOJ5470】[FJOI2018]所罗门王的宝藏()

    [BZOJ5470][FJOI2018]所罗门王的宝藏() 题面 BZOJ 洛谷 有\(n+m\)个变量,给定\(k\)组限制,每次告诉你\(a_i+b_j=c_k\),问是否有可行解. 题解 一道很 ...

  7. Get The Treasury HDU - 3642(体积扫描线)

    给出n个立方体,要你求这些立方体至少被覆盖三次的部分. 先把这个立方体的信息存在来,发现Z的范围不大,z范围是是[-500,500],所以我们可以先离散化,然后枚举Z, 然后对于每一段Z的区域内,在当 ...

  8. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

  9. Python3 与 C# 并发编程之~进程先导篇

      在线预览:http://github.lesschina.com/python/base/concurrency/1.并发编程-进程先导篇.html Python3 与 C# 并发编程之- 进程篇 ...

  10. nodejs的某些api~(一)node的流2

    可写流writablewritable.write(chunk, [encoding], [callback])chunk {String | Buffer} 要写入的数据encoding {Stri ...