python -- while循环,格式化输出,运算符,初识编码
一、while循环
1、语法
while 条件:
循环体(结果)
如果条件为真,则直接执行结果),然后再次判断条件,知道条件为假,停止循环。
while True:
print('你是谁呢')
退出循环:1、break 2、改变条件
while true:
content = input('请输入你想要唱的歌曲,输入aq退出:')
if content == 'aq':
break
print('你唱的是什么:',content)
count = 1
while count <=3: # 最多输入三次
# count = 1 # 次数,死循环
song = input('请输入你想要唱的歌曲:')
print('你想要唱的歌曲是:',song)
# 改变count
count = count + 1
3、流程控制-break 和continue
break:立刻跳出此次循环,彻底结束
continue:停止当前循环,继续执行下一次循环
count = 1
while count <=3:
song = input('请输入你想要唱的歌曲:')
if song == '':
continue # 停止本次循环,继续执行下一次循环,不会彻底中断循环
print('你想要唱的歌曲是:',song)
count = count + 1
一些程序
# 输出 1-100
num = 1
while num <= 100:
print(num)
num = num + 1 #请输入1-100之间的偶数
num = 1
while num <=100:
a = num%2
if a == 0:
print(num)
num = num + 1 #请输入1-100之间的奇数
num = 1
while num <=100:
a = num%2
if a == 1:
print(num)
num = num + 1 #请输入1-100的和
sum = 0
num = 1
while num <= 100:
sum = sum + num
num = num + 1
print(sum)
# 用户输入个数. 判断这个数是否是一个质数
num = int(input('请输入一个数:'))
i = 2
if num < i and num >= 0:
print('这不是一个质数')
else:
while i < num:
if num % i == 0:
print('这不是一个质数')
break
i += 1
else:
print('这是一个质数')
二、格式化输出
%s 表示字符串的占位,是全能占位,既可以是字符串也可以是数字
%d 表示数字的占位,只能占位数字
注: 如果一句话中使用了格式化输出,%就是占位,如果想正常显示%,需写为%%,表转义
name = input('你的名字是什么:')
gender = input('你的性别是什么:')
star = input('你最喜欢的明星是谁:')
science =input('你最喜欢的科学家是:')
# 连接
print("我的名字是"+name+",性别"+gender+",我喜欢的明星是"+star+",我喜欢的科学家是"+science)
# 格式化%s
print("我的名字是%s,性别是%s,喜欢的明星是%s,喜欢的科学家是%s"%(name,gender,star,science))
# 新版本可支持
print(f"我的名字是'{name},性别是{gender},喜欢的明星是{star},喜欢的科学家是{science}")
三、运算符
计算机可以进行的运算有很多种:算术运算(+ - * / % **(幂) //(整除)),比较运算(== != <> > < >= <=),逻辑运算(and or not),赋值运算,成员运算,身份运算,位运算
着重解释逻辑运算:
and 并且,左右两端同时为真,结果才能为真
or 或者,左右两端有一个为真,结果则为真
not 非 ,非真即假,非假即真
混合运算
运算顺序: () -> not -> and -> or
当出现 x or y 时,判断 x 是否为0,如果 x == 0 则为 y ,否则返回 x
当出现 x and y 时,情况正好和 or 相反
True 当成 1,False当成 0 .
print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #True
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ) #False
print(8 or 3 and 4 or 2 and 0 or 9 and 7) #8
print(0 or 2 and 3 and 4 or 6 and 0 or 3) #4
print(6 or 2 > 1) #6
print(3 or 2 > 1 ) #3
print(0 or 5 < 4) # False
print(5 < 4 or 3) #3
print(2 > 1 or 6) # True
print(3 and 2 > 1) # True
print(0 and 3 > 1) #0
print(2 > 1 and 3) #3
print(3 > 1 and 0 ) #0
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) # 2
成员运算
ad = input("请输入你的广告:")
if "最" in ad or "第一" in ad or "全球" in ad:
print("不合法的")
四、初识编码
1、ASCII码 为8bit(1 byte(字节)),有256个码位,用到了前128个,首位都是0,包括大小写英文字母,一些符号。
2、GBK ,国标码,兼容ASCII码,16bit(2字节)
3、unicode 对所有的编码进行统一,万国码,32bit(4字节)
4、utf-8 可变长度的unicode
1byte = 8 bit
1 KB = 1024 byte
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB
1 PB = 1024 TB
python -- while循环,格式化输出,运算符,初识编码的更多相关文章
- python全栈 流程控制;while 循环 格式化输出 运算符 及编码
python全栈开发 1循环 2break和continue的区别 3格式化输出 4运算符 5编码 一.流程控制while循环 while条件: 代码块(循环体) 1.死循环; while True; ...
- while循环,格式化输出,运算符及编码初识
一.while循环 1.基本循环(死循环) while 条件: 循环体 2.使用while计数 count = 0 # 数字里面非零的都为True while True: count = count ...
- python之while循环/格式化输出/运算符/初始编码/成员变量
一.主要内容:1.while 循环 (难点)while 条件: 循环体 break: 直接跳出循环continue:停止当前本次循环,继续执行下一次循环.不会中断循环能让循环退出:(1)break ( ...
- Python基础篇(格式化输出,运算符,编码):
Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s 替换字符串 %d 替换整体数字 ...
- while循环 格式化输出 密码本 编码的初识
第二天课程整理 while 循环 why : while ' 循环' 的意思 what : while 无限循环 how : 1.基本结构 while + 条件 循环的代码 初识循环 while tr ...
- 记录我的 python 学习历程-Day02-while 循环/格式化输出/运算符/编码的初识
一.流程控制之--while 循环 循环就是重复做同一件事,它可以终止当前循环,也可以跳出这一次循环,继续下一次循环. 基本结构(基本循环) while 条件: 循环体 示例 # 这是一个模拟音乐循环 ...
- day 02 while 循环 格式化输出 运算符 and or not - 编码的初识
while 循环 while 条件: 循环体 循环如何终止? 改变条件. flag = Truewhile flag: print('狼的诱惑') print('我们不一样') ...
- while循环,格式化输出%,运算符,数据类型的转换,编码的初识,
1.内容总览 while循环 格式化输出 运算符 and or not 编码的初识 2. 具体内容 while 循环 where:程序中:你需要重复之前的动作,输入用户名密码时,考虑到while循环. ...
- 04 Python之while循环/格式化输出/运算符/编码
1. while循环 while 条件: 循环体(break,continue) else: 循环体(break,continue) break:彻底干掉一个循环,直接跳出. continue:停止当 ...
- day02 python流程控制 while循环 格式化输出 运算符 编码
day02 python 一.循环: while循环 while expression: while_suite 1.break 停止当前循环(如果多个循环嵌套, ...
随机推荐
- HBase 笔记2
Hadoop 服务启动顺序: zookeeper ->journalnode->namenode -> zkfc -> datanode HBase Master WEB控制台 ...
- IO流的总结(二)
缓冲字节流: 我们先说一下缓存区的概念: 缓冲区就好比一辆车,一车一车的把数据拉走,这样就效率快多了 按照流的方向分类: 写入数据到流中,字节缓冲输出流 BufferedOutputStream 读取 ...
- 在网页中运用统计Web Service接口
(2017-02-10 银河统计) 在"统计随机数及临界值Web Service接口"一文中介绍了常用统计分布四类Web Service接口(随机数.分位数.密度函数和累积分布函数 ...
- nodejs cannot find module 'mysql' 问题分析
在windows平台下,测试nodejs连接mysql数据库. 首先 在控制台中安装mysql依赖包 npm install mysql 安装成功后,mysql依赖包可以在User目录中的node_m ...
- STAT UN2102 Homework
STAT UN2102 Homework 4 [100 pts]Due 11:59pm Monday, May 6th on CanvasYour homework should be submitt ...
- 【java】J2EE、J2SE和J2ME的区别
本文向大家简单介绍一下J2EE.J2SE.J2ME概念及区别,J2EE,J2SE,J2ME是java针对不同的的使用来提供不同的服务,也就是提供不同类型的类库. Java2平台包括:标准版(J2SE) ...
- BZOJ4779: [Usaco2017 Open]Bovine Genomics
题目描述 Farmer John owns Ncows with spots and N cows without spots. Having just completed a course in b ...
- Mac cnpm安装失败及解决方案
首先安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 淘宝镜像安装cnpm, 在终端输入: npm install -g cnpm --registry=h ...
- ABAP search help (搜索帮助) 几种种方法
ABAP search help (搜索帮助) 几种种方法 域范围 ABAP 的搜索帮助有很多种方法,掌握下面的几种基本差不多了 *&------------------------- ...
- 6th-Python基础——集合、函数
1.集合 主要作用: (1)去重 (2)关系测试,交集.差集difference().并集union().反向差集symmetric_difference().子集issubset().父集issup ...