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 停止当前循环(如果多个循环嵌套, ...
随机推荐
- 【UML】NO.53.EBook.5.UML.1.013-【UML 大战需求分析】- 组合结构图(Composition Structure Diagram)
1.0.0 Summary Tittle:[UML]NO.52.EBook.1.UML.1.012-[UML 大战需求分析]- 交互概览图(Interaction Overview Diagram) ...
- form提交xml文件
--为何ajax提交不了xml?--原因:Request.Form["Data"]这种获取参数方式,原本就不是mvc路由参数获取方式,这是Asp.net中webfrom页面获取参数 ...
- 《linux就该这么学》第五节课,shell脚本的各种语句!
第四章shell语句 (据课本和虚拟机实验排版,借鉴请改动) 4.2:shell脚本 脚本包括:脚本声明,脚本注释,脚本内容和命令 例:#!/bin/bash ...
- 第三章 document对象及数组
1.数组的使用(1)声明数组var 数组名=new Array();(2)数组赋值数组名[下标]=值: 2.数组声明,分配空间,赋值同时进行var 数组名=new Array(值1,值2....)va ...
- ADB工具的使用
ADB即Android Debug Bridge调试桥,可以用来调试管理Android设备与设备模拟器的状态,比如,在Android设备上运行Shell,在电脑和设备之间互传文件... 那么问题来了. ...
- 改变FileUpload文件上传控件的显示方式,选择文件后自动上传
一.Aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="File ...
- springmvc+hibernate在实体类中设置外键
1.表User id主键,username,password,dept... 表Attendence id主键,uid外键,time... @ManyToOne @JoinColumn(name = ...
- Ansible在Ubuntu上的安装
#apt安装 apt-get install software-properties-common apt-add-repository ppa:ansible/ansible apt-get upd ...
- 批处理学习之Bat命令——获取当前盘符、当前目录、上级目录
命令 当前盘符:%~d0 当前路径:%cd% 当前执行命令行:%0 当前bat文件路径:%~dp0 当前bat文件短路径:%~sdp0 测试 下载testBatPath.bat测试文件,双击.bat运 ...
- SpringCloud学习2-Springboot监控模块(actuator)
前言 学习一项新技术最大的困难是什么? 是资料.让人高兴的是找到了一本系统学习Spring Cloud的教程,<Spring Cloud微服务实战>, 接下来的学习目标将以此书顺序演进. ...