python 第二章

第二章

while 循环

# while -- 关键字(死循环)
# while 条件 冒号
# 缩进 循环体
# 停止while要点击红色停止,不能只关闭窗口
while True:
    print("痒")
    print("鸡你太美")
    print("卡路里")
    print("好运来")

while False:
    print("痒")
    print("鸡你太美")
    print("卡路里")
    print("好运来")# 无输出

print(1)
while True:
    print("痒")
    print("鸡你太美")
    print("卡路里")
    print("好运来")
print(2)# 循环输出,print(2)不输出

print(1)
while False:
    print("痒")
    print("鸡你太美")
    print("卡路里")
    print("好运来")
print(2)# 输出12

falg = True
while falg:
    print(1)
print(2)# 循环输出1

# 通过数字转成布尔值
print(bool(1))
# 数字里面非0的都是True

count = 1
while count:
    print(count)
    count = count + 1# 循环输出,每次+1

# 正序
count = 1
while count <= 5:
    print(count)
    count = count + 1# 输出12345

# 倒序
count = 5
while count:
    print(count)
    count = count - 1# 输出54321
# 正序打印从 25-57
count = 25
while count <= 57:
    print(count)
    count = count + 1

# 倒序打印从 57-25
count = 57
while count >= 25:
    print(count)
    count = count - 1
# break 终止当前循环,break下方的代码不会进行执行
while True:
    print(123)
    print(234)
    break
    print(345)
print(111)# 输出123234111

# continue 临时当作循环体中的最后一段代码
# 跳出当前循环,继续下次循环
# 下面的代码不会再执行
while True:
    print(123)
    print(234)
    continue # 伪装成最后一段代码
    print(345)
print(111)# 循环输出123234

# else 的用法
# 条件不成立跳出当前while循环
# 当为真的时候
while True:
    print(111)
else:
    print(222)# 循环输出111

while True:
    print(111)
print(222)# 循环输出111

#当为假的时候
while False:
    print(111)
else:
    print(222)# 输出222

while False:
    print(111)
print(222)# 输出222

#为真且有break
while True:
    print(111)
    break
else:
    print(222)# 循环输出111

while True:
    print(111)
    break
print(222)# 循环输出111222

#为假且有break
while False:
    print(111)
    break
else:
    print(222)# 循环输出222

while False:
    print(111)
    break
print(222)# 循环输出222
# 总结:
# 打断循环的方式:
# 1.通过自己修改条件
# 2.break
# break -- 打破当前循环(终止当前循环)
# continue -- 跳出当前循环,继续下次循环(伪装成最后一段代码continue 什么时候用?)
# break和continue相同之处:以下代码均不执行

字符串的格式化

a = "---info---"
b = "name"
c = "age"
d = "job"
e = "---end---"
print(a+b+c+d+e)# 输出
# ---info---nameagejob---end---

a = "---info---"
b = "name"
c = "age"
d = "job"
e = "---end---"
print(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e)# 输出
# ---info---
# name
# age
# job
# ---end---

name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
a = "---info---"
b = "name:"
c = "age:"
d = "job:"
e = "---end---"
print(a+'\n'+b+name+'\n'+c+age+'\n'+d+job+'\n'+e)# 输出
# 请输入姓名:张三
# 请输入年龄:25
# 请输入职业:python
# ---info---
# name:张三
# age:25
# job:python
# ---end---
# %s 占位符 类型为s 是占的字符串类型的位置
# %d 占位符 类型为数字 是占的数字类型的位置
# s% 按照位置顺序传递,占位和补位一一对应
s = """
---info---
name:%s
age:%d
job:%s
---end---
"""
name = input("请输入姓名:")
age = int(input("请输入年龄:"))
job = input("请输入职业:")
print(s%(name,age,job))# 输出
# 请输入姓名:张三
# 请输入年龄:25
# 请输入职业:python
# ---info---
# name:张三
# age:25
# job:python
# ---end---

#多、少位置均不行
s = """
--info--
name:%s
age:%d
job:%s
---end---
"""
name = input("name")
age = int(input("age"))
job = input("job")
print(s%(name,job,))
# 少位置报错:TypeError: %d format: a number is required, not str

# %% 两个百分号为转义
num = input("学习进度:")
B = "学习进度为:%s %%"
print(B%(num))# 输出学习进度为:30 %

sll = "学习进度为:%s"
print(sll%("不错"))# 输出学习进度为:不错

# f也可以做格式化
s = f"{1}{2}{3}"
print(s)# 输出123

s = f"今天下雨了{input('>>>')}"
print(s)# 输出 今天下雨了+输入的内容

运算符

# 算术运算符
# + - * /
# // (整除--地板除)
# python2跟python3的除有区别
# ptyhon2 获取到的是整数
# python3 获取到的是小数 浮点型
print(5//2)# 输出2
# ** 幂 (次方)
print(3**2)# 输出9
# % 模 (取余)
print(5%2)# 输出1

# 比较运算符
# >
# <
# ==(等于)
# !=(不等于)
# >=
# <=

# 赋值运算符
# = 赋值
# += 自加
a = 10
a += 1
print(a)# 输出11
# -= 自减
# *= 自乘
a = 10
a *= 2
print(a)# 输出20
# /= 除法赋值
# //= 取整赋值
# **= 幂赋值
# %= 取模赋值

# 逻辑运算符
# and (与/和)
# and 都为真,取and后面的值
print(3 and 4)# 输出 4
# and 都为假。取and前面的值
print(0 and False)
# and 一真一假取假的,假的取前面的值
print(0 and 4)# 输出0

print(3 and 5 and 9 and 0 and False)# 输出0
print(5 and False and 9 and 0)# 输出False
print(1 and 2 and 5 and 9 and 6)# 输出6

# or  (或)
# or 都为真,取or前面的值
print(1 or 2)# 输出1
# or 都为假。取or后面的值
print(False or 0)# 输出0
# or 一真一假取真的
print(0 or 1)# 输出1

print(1 or 9 or 4 or 0 or 9)# 输出1
print(1 or 2 or 4 or 8 or 6)# 输出1
print(0 or False)# 输出False

# not  (非)
print(not False)# 输出True

# 优先级
# ()> not > and > or
# and 从左向右执行

print(9 and 1 or not False and 8 or 0 and 7 and False)# 输出1
print(9 and 1 or True and 8 or 0 and 7 and False) # not False = True
print(1 or True and 8 or 0 and 7 and False)# 9 and 1 取1
print(1 or 8 or 0 and 7 and False)# True and 8 取8
print(1 or 8 or 0 and False)# 7 and False 取False
print(1 or 8 or 0)# 0 and False 取0
# 1 or 8 or 0 取1

# 成员运算符
# in 存在
# not in 不存在

s = "abc"
if "ab" in s:
    print(True)
else:
    print(False)# 输出True

s = "abc"
if "ab" not in s:
    print(True)
else:
    print(False)# 输出False

编码初识

# ascii (美国)      不支持中文
# gbk    (国标)      英文8位,中文16位
# unicode(万国码)    英文16位 中文32位
# utf-8  (可变长编码) 英文8位 欧洲文16位 亚洲24位

# linux -- utf -8
# mac -- utf -8
# windows -- gbk

# 单位转换:
# 1字节 = 8 位
# 1Bytes = 8bit
# 1024Bytes = 1KB
# 1024KB = 1MB
# 1024MB = 1GB
# 1024GB = 1TB
# 1024TB = 1PB
# 1024PB = 1EB

# 1024EB = 1ZB
# 1024ZB = 1YB
# 1024YB = 1NB
# 1024NB = 1DB

今日总结

# 1、while循环 死循环:
# while 条件 冒号
# 缩进 循环体
#
# 打断死循环:
# break:终止当前循环
# 改变条件:自动自定义修改控制执行次数
#
# 关键字:
# break:终止当前循环
# continue:伪装成循环体中最后一行代码(跳出本次循环,继续下次循环)
# while else:while 条件成立,后面代码不执行;条件不成立,继续执行else
#
# while else
# while 条件成立的时候不执行
# 条件成立执行else
#
# 2、字符串格式化:
# %  占位
# %s 占字符串类型的位
# %d 占数字类型的位
# %% 转义成普通的百分号
# 变量名%() 起到连接的作用
# s = "您好%s"
# s%("我好")
# print(s%("我好"))
# f"{变量名}{字符串}{}"3.6以及以上才能使用
#
# 3、运算符:
# 算数运算符: + - * / // ** %
# 比较运算符: > < >= <= == !=
# 赋值运算符: = += -= *= /= //= **= %=
# 逻辑运算符: and or not () >  优先级:not > and > or
# 成员运算符: in not in
#
# 4、编码:
# 编码集(密码本)
# ascii:不支持中文
# gbk:
# 英文 8位 1字节
# 中文 16位 2字节
# unicode:
# 英文 16位 2字节
# 中文 32位 4字节
# utf-8:
# 英文 8位  1字节
# 欧洲 16位 2字节
# 亚洲 24位 3字节
# # 单位转换:
# # 1字节 = 8位
# # 1Bytes = 8bit ***
# # 1024Bytes = 1KB
# # 1024KB = 1MB
# # 1024MB = 1GB
# # 1024GB = 1TB

原文地址:https://www.cnblogs.com/zhangshan33/p/11139803.html

时间: 2024-08-13 04:49:08

python 第二章的相关文章

machine learn in python 第二章2.1.1

1关于 sklearn.datasets from sklearn.datasets import load_iris import numpy as np data = load_iris() data 的属性如下: 数据存储在.data项中 每个观察对象的种类存储在数据集的.target属性中 数据的特征的种类存储在数据集的.feature_names属性中. 2关于遍历问题 a:遍历不取item的序号i for item in sequence: process(item) b: for 

[Head First Python] - 第二章 python of comment

1- 多行注释 ''' ''' 或 """ """ '''this is the standard way to include a multiple-line comment in you code''' """this is the standard way to include a multiple-line comment in you code""" 2- 单行注释 # # t

【原创】Python第二章——字符串

字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符.Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而不是修改现有的字符串. 字符串常量的表示 1. 3种表示 1 #单引号 2 A = 'Python' 3 #引号 4 B = "Python" 5 #三引号 6 C = """Python""" 2. 为什么要这么麻烦? (1)当

Python第二章

1. 下划线_代表最后一个表达式的值.注意:只在解释器中有用,既显示上一次的操作结果. 2. Print语句可以用%进行格式控制,与C相似. 如: >>> print “%s is number %d!” % (“python”, 1) Python is number 1! 3. Python 非常灵活,所以即使你将数字传递给 %s,也不会像其他要求严格的语言一样引发严重后果. 4. 符号 >> 用来重定向输出,下面这个例子将输出重定向到标准错误输出: import sys

Head First Python 第二章 函数模块&amp;第三章 文件与异常&amp;第四章 持久存储&amp;第五章 处理数据

第三章 1.共享模块 模块和发布工具箱全世界共享模块 编写函数,以.py为文件后缀名,第三方库都在PyPI *注释代码:三引号(单双都可以) 发布过程P40 使用发布工具,函数模块变成了一个“发布”,可以使用import导入其他程序 2.如果函数模块功能不满意 添加参数以供api选择比添加函数工作量小! 首先考虑BIF内置函数 ----------------------------------------------------------- 第四章 1.文件 open()语句,readlin

python 第二章 列表,if循环

列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列都可以进行的操作包括索引,切片,加,乘,检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型 创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起

python 第二章 二进制运算、字符编码、数据类型

>>> len(s.ljust(50,'*'))50>>> 1.二进制转换: bin(342) '0b101010110' 2.ASCII码与二进制: 每一个ASCII码都是用8位表示,ASCII码一共有255个.每一位0或者1所占的空间单位为bit(比特),这是计算机中最小的表示单位,8位表示一个字节. 8bit = 1bytes(字节),最小的存储单位,1bytes缩写为1B 1KB = 1024B 1MB = 1024KB 1GB = 1024MB 1TB = 1

流畅的python第二章序列构成的数组学习记录

python内置序列类型概览 列表推导和生成器表达式 列表推导是构建列表的快捷方式,而生成器表达式可以用来创建其他任何类型的序列 列表推导的示例 >>>test = [i*2 for i in range(3)] >>>test [0,2,4] 使用列表推导通常的原则是,只能列表推导来创建新的列表,并且尽量保持简短. python2中列表推导可能有变量泄露问题,而python3解决了变量泄露问题 列表推导和map/filter的对比 生成器表达式 虽然也可以用列表推导来

python第二章函数以及常用模块

装饰器: 定义:本质是函数,(装饰其他的函数)就是为其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 实现装饰器的知识储备 1,函数即"变量" 2,高阶函数 a:把一个函数名当做实参传给另一个函数 b:返回值包含函数名 3,嵌套函数 高阶函数+嵌套函数==装饰器 import time def timemer(func): def wrapper(*args,**kwargs): start_time=time.time() func(*