python3 day02 大纲
一. 格式化输出
%s 字符串的占位
%d 数字的占位 digit
%f 浮点数 字符串 % (数据) 模板字符串(3.5)
# username = "admin"
# password = "123456"
'''正常输出'''
# print("管理系统的账号是:", username, "密码是:", password) '''格式化输出方式一: %s代表字符串(string)占位 %d代表数字(digit)占位, %f代表浮点数(float)占位'''
# print("管理系统的账号是: %s 密码是: %s" % (username, password)) '''格式化输出方式二'''
# print("管理系统的账号是: {} 密码是: {}".format(username, password)) '''格式化输出方式三(3.5以上版本)'''
# print(f"管理消停的账号是: {username} 密码是: {password}") 二. 运算符
算数运算
+ - * / // %
比较运算
> < >= <= == !=
成员运算
in 是否在xxx里面
not in 是否不在xxx里面 逻辑运算(重点)
1. and 并且, 左右两端同时为真, 结果才能是真.
2. or 或者, 左右两端有一个是真, 结果就是真
3. not 非, 非真即假, 非假即真
运算顺序: () > not > and > or a or b
if a == 0: b else a
and和or相反 赋值扩展运算
= 赋值, 把等号右边的结果赋值给等号左边的变量
+=
-=
*=
/=
//=
%= a+=b => a = a + b
'''
判断某人的留言当中是否包含波多野结衣
'''
# msg = input("请输入您的留言: ").strip()
# if "波多野结衣" in msg:
# print("留言内容含有敏感关键字,留言失败!")
# else:
# print("恭喜您,留言成功!") '''
逻辑运算
'''
# print(1 > 2 and 4 < 6 and 5 > 7)
# print(1 < 2 and 3 < 4 or 1 > 2)
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
# print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) '''
or
'''
# print(1 or 2)
# print(0 or 1)
# print(1 or 0)
# print(0 or 3)
# print(2 and 3)
# print(1 or 2 and 3)
# print(0 or 3 or 5 or 1 or 0 or 4 or 7) '''
and和or 取值相反
'''
# print(1 and 0)
# print(0 and 3)
# print(3 and 4)
# print(4 and 0)
# print(1 and 2 and 0 and 3 and 4)
# print(0 and 1 or 2 and 0 or 1 or 2 and 3) # print(1 and 2 or 3)
# print(1 and 5 or 3 and 4 or 7 or 8 and 6 or 2)
# print(1 and 5 < 3 or 6)
# print(1 or 0 and 5 < 6 and 7 > 8 or 3) 三. 编码(记结论)
ascii: 英文+数字+标点+特殊字符 一共编码了128个. 最早的是7bit => 8bit
计算机网络传输的时候单位是字节 1byte => 8bit
ascii第一位是0 ANSI: 一个标准
GBK: 国标码的扩展码 16bit 2byte 65536个 主要是放中文 一定兼容ascii, 现在大家的windows默认就是GBK ISO+Unicode=> unicode 万国码 32bit -> 一个面 16bit 65536个文字.
ascii: a 10101010
unicode: a 10101010 ascii 8bit 1types
gbk 16bit 2types
unicode 32bit 4types utf:可变长度的unicode, 弥补了浪费的空间.
utf-8:
英文: 8bit 1byte
欧洲: 16bit 2byte
中文: 24bit 3byte python中默认使用的字符串就是unicode
进行网络传输和文件存储必须要转化成utf-8或者gbk 如何把字符串转化成utf-8 encode(编码) => 使用编码格式对文字进行编码
decode(解码) => 把bytes变回字符串 8bit -> 1Byte
1024B -> 1KB
1024KB -> 1MB
1024MB -> 1GB
1024GB -> 1TB 四. int
bit_length() 是int唯一的一个动作.
'''
int类型的数据,基本运算
bit_length()二进制长度
'''
# for i in range(1, 20):
# print(bin(i), i.bit_length(), oct(i), i, hex(i))
五. bool -> 数据类型之间的转化(记结论)
int(字符串) => 把字符串转化成数字
str(数字) => 把数字转化成字符串 结论1: 想要把x变成y y(x)
结论2: 所有的空都表示False, 所有的非空都表示True
'''
bool类型,基本数据类型之间的互相转换
bool => int
b = int(a)
print(b) True => 1
False => 0 0 => False
非0 => True 结论2: 把x转化成y 需要 y(x)
字符串 => 数字
int(字符串) '''
# a = True
# print(type(a))
# b = int(a)
# print(b)
# print(bool(0))
# print(bool(100)) '''
while 1:比 while True:(效率高一点点)
'''
# while 1:
# pass '''
所有表示空的东西都是假
'''
# print(bool("")) '''
None 空
'''
# print(bool(None))
六. str(重点, 记住, 课上总结的内容)
字符串的索引
程序员数数从0开始 字符串[索引]
切片:
字符串[start:end] 从start到end拿到数据. end取不到
字符串[start:end:step] 从start到end拿数据. 每step个拿出来一个.
step:
+ 左到右
- 右到左 常用操作
字符串是一个不可变的数据类型
1. upper() 转化成大写. 在忽略大小写的时候.
2. strip() 默认去掉左右两端的空白
3. replace() 字符串的替换
4. split() 字符串切割, 结果是列表
5. startswith() 判断是否以xxx开头
6. find() 查找字符串中xxx字符的位置. 如果找不到返回-1
7. len() 字符串长度
'''
str 由双引号,单引号,三个单引号,三个双引号括起来的内容
'''
# s = "字符串1"
# s2 = '字符串2'
# s3 = '''字符串3'''
# s4 = """字符串4"""
# print(s, s2, s3, s4) '''
字符串是索引,索引就是第几个字符,索引从0开始
'''
# s = "hello world"
# print(s[0])
# print(s[-1])
# print(s[-3]) '''
字符串的切片,顾头不顾尾, 默认从左到右 [start:end:step]
'''
# s = "hello world"
# print(s[:])
# print(s[:3])
# print(s[3:])
# print(s[::2])
# print(s[::-1]) '''
大小写转来转去
1.首字母大写 capitalize()
'''
# s = "hello world"
# s = s.capitalize()
# print(s) '''
2.把所有的字母转化成大写 upper()
'''
# s = "hello world"
# s = s.upper()
# print(s) '''
3.把所有的字母转化为小写 lower()
'''
# s = "Hello World"
# s = s.lower()
# print(s) '''
4.标题,每个单词的首字母大写 title()
'''
# s = "hello world"
# s = s.title()
# print(s) '''
字符串切换 split()
'''
# s = "hello_world"
# lst = s.split("_")
# print(lst) '''
字符串替换 replace()
'''
# s = "hello world"
# s = s.replace("l", "B")
# print(s) '''
去掉空白 strip() lstrip() rstrip()
'''
# s = " hello world \t \n \r"
# s1 = s.strip()
# print(s1)
# s2 = s.lstrip()
# print(s2, "--end")
# s3 = s.rstrip()
# print(s3) '''
判断字符串是否以什么开头或结尾 startswith() endswith()
'''
# s = "hello world"
# print(s.startswith("hello"))
# print(s.endswith("D")) '''
判断子字符串在字符串中出现的次数 count()
没有不报错
'''
# s = "hello world"
# print(s.count('o'))
# print(s.count('p')) '''
查找子字符串find() 的索引
如果找不到,返回-1
'''
# s = "hello world"
# print(s.find('o'))
# print(s.find('u')) '''
查找子字符串索引 index()
找不到会报错
'''
# s = "hello world"
# print(s.index('o'))
# print(s.index('u')) #ValueError: substring not found '''
判断是否由数字组成 isdigit()
判断是否由文字的基本组成 isalpha()
是否有数字,字母组成,能屏蔽特殊字符 isalnum()
判读是否是数字 isnumeric()
'''
# s = "hello world"
# print(s.isdigit())
# s = "1900"
# print(s.isdigit()) '''
字符串长度 len()
可以用来遍历字符串
'''
# s = "hello world"
# print(len(s))
# for i in s:
# print(i) '''
in 和 no in 成员运算
'''
# s = "hello sex world"
# if "sex" in s:
# print("不合法")
# else:
# print("合法") '''
while...else...
for...else...
break不会执行else,打断这个循环,没有经过条件判断,所以不执行else
'''
# i = 0
# while i < 10:
# print(i)
# i += 1
# else:
# print("数完了") # for i in range(10):
# print(i)
# else:
# print("数完了") 七. list - > 装数据的
列表使用[]表示
[]
list()
列表也有索引和切片 增删改查(重点)
增加:
1. append() 追加, 添加在列表的末尾
2. insert() 插入, 在xxxx位置插入一个元素
3. extend() 迭代新增.合并列表 删除:
1. pop() 删除. 指定索引删除
2. remove() 删除某个指定元素
3. del list[3]
4. clear() 清空列表 修改:
使用索引去修改 查询:
直接用索引查找.
使用for循环可以遍历列表 其他操作:
sort()
count() 元组(tuple): 不可变的列表, 只读列表
放一些不进行修改的数据
元组用()表示.
空元组必须用tuple()来创建 小知识点: 如果元组只有一个元素. 必须在末尾添加一个逗号
'''
存在的意义:存储大量的数据
列表使用[]括起来,内部元素用逗号隔开
'''
# lst = ["apple", "banana", "watermelon", "pear"] '''
列表和字符串一样也有索引和切片
'''
# lst = ["apple", "banana", "watermelon", "pear"]
# print(lst[0])
# print(lst[1::2])
# print(lst[-1::-2]) '''
列表的增删改查
添加
1.append() 追加 向列表的最后放数据
'''
# lst = []
# lst.append("apple")
# lst.append("banana")
# print(lst) '''
2.insert() 插入
'''
# lst = ["apple", "banana", "pear", "watermelon"]
# lst.insert(2, "strawberry")
# print(lst) '''
3.extend() 迭代增加
'''
# lst = ["apple", "banana"]
# lst.extend("melon")
# print(lst)
# lst.extend(["pear", "strawberry"])
# print(lst) '''
删除
1.pop()
2.remove()
3.del lst[index]
4.clear() 清空
'''
# lst = ["apple", "banana", "pear", "watermelon"]
# lst.pop()
# print(lst)
# lst.remove("apple")
# del lst[0]
# print(lst) '''
修改
'''
# lst = ["apple", "banana", "pear", "watermelon", "blueberry", "pineapple"]
# lst[1] = "orange"
# print(lst) '''
查询 -> 用的最多的是索引
列表是一个可迭代对象,可以使用for循环
'''
# lst = ["strawberry", "blueberry", "pineapple"]
# for item in lst:
# print(item) '''
列表排序
从小到大 sort()
从大到小 sort(reverse=True)
'''
# lst = [1, 4, 7, 1, 2, 8, 2, 9]
# lst.sort()
# print(lst)
# lst.sort(reverse=True)
# print(lst) '''
列表的深浅拷贝
is判断内存地址是否相同
'''
# a = 10
# # b = a
# # print(a is b) # lst = ["apple", "banana"]
# lst2 = lst
# print(lst is lst2)
# lst.append("pear")
# print(lst)
# print(lst2)
# print(lst is lst2) '''
切片方式,会对列表进行一个复制.浅拷贝
'''
# lst = ["apple", "banana", ["watermelon", "blueberry"]]
# lst3 = lst[:]
# print(lst is lst3)
# print(lst[2] is lst3[2])
# lst.append("pear")
# lst[2].append("strawberry")
# print(lst)
# print(lst3) '''
copy()
'''
# lst = ["apple", "banana", ["watermelon", "blueberry"]]
# lst2 = lst.copy()
# print(lst is lst2)
# print(lst[2] is lst2[2])
# lst.append("pear")
# lst[2].append("strawberry")
# print(lst)
# print(lst2) '''
深拷贝 inport copy
copy.deepcopy()
'''
# import copy
# lst = ["apple", "banana", ["watermelon", "blueberry"]]
# lst2 = copy.deepcopy(lst)
# print(lst is lst2)
# print(lst[2] is lst2[2])
# lst.append("pear")
# lst[2].append("strawberry")
# print(lst)
# print(lst2) '''
元组是不可变的列表,又被称为只读列表
元组用小括号表示,并且空元组用tuple()
如果元组中只有一个元素,必须在后面加逗号
元组是可迭代对象
'''
# tu = ("apple", "banana", "pear", "blueberry")
# for t in tu:
# print(t) 八. dict 字典 查询速度快
在存储数据的时候必须使用key:value的形式进行存储,
key不可以重复.
并且要求key必须可哈希-> 不可变(int, str, tuple, bool, float)
value没有要求 字典的增删改查:
新增:
1. 直接用新key添加
2. setdefault(key, value) 如果key存在, 不新增. 不存在, 新增. 最后都查询 删除:
1. pop(key) 指定key删除
2. popitem() 删除最后一个.
3. del dic[key]
4. clear() 修改:
dic[老key] = 新value 查询:
get(key) 使用key获取value
dic[key] 查询, 如果key不存在. 会报错 循环:
for k in dic:
k
dic[k] for k, v in dic.items():
print(k)
print(v)
'''
字典存储数据的时候,它是根据hash值进行存储的,字典中所有的key必须是可哈希的.
字典用{}表示.以key:value的形式进行保存数据
不可变就是可哈希,列表不能作为字典的Key
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# print(type(dic)) '''
字典的增删改查
1.查询
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# print(dic["香蕉"])
# print(dic["橘子"]) '''
字典.get(key) 找不到返回None
当key不存在,返回第二个数据
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# print(dic.get("苹果"))
# print(dic.get("大苹果"))
# print(dic.get("大苹果", "没有这个水果哦")) '''
添加元素
当key存在是,就修改,不存在就新增
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# dic["蓝莓"] = "blueberry"
# print(dic) '''
dict.setdefault(key) 如果只有一个参数,value放空
dict.setdefault(key, value) 1.首先判断key是否在字典中存在,如果存在,就不执行任何新增操作
2.不论前面是否执行新增,最后都会根据key把value查出来
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# dic.setdefault("苹果")
# print(dic)
# dic.setdefault("大苹果", "big apple")
# print(dic)
# f = dic.setdefault("苹果", "Apple")
# print(dic)
# print(f) '''
删除pop(key)
clear()清空
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# dic.pop("香蕉")
# print(dic) '''
del dict[key]
'''
# dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# del dic["香蕉"]
# print(dic) '''
字典是可迭代的
查询: keys(), values(), items()
'''
dic = {"苹果": "apple", "香蕉": "banana", "橘子": "orange"}
# print(dic.keys())
# print(dic.values())
# print(dic.items())
# for k in dic.keys(): #不常用
# print(k) # for k in dic: #简单,常用
# print(k) # for v in dic.values():
# print(v) '''
常用:直接获取到key和value的方式
'''
# for k,v in dic.items():
# print(k,v) '''
解构,解包:把元组,列表中的每一项拿出来复制给前面的变量
要求:数据和变量要能对的上
'''
# a, b ,c = (1, 2, 3)
# print(a, b, c)
九. 知识点补充
1. range
2. join
3. while...else
4. for循环的时候不要删除列表中的内容 欠的: is和==区别, 深浅拷贝, set集合
python3 day02 大纲的更多相关文章
- python3 今日大纲 day05
1. 上周内容回顾 1. 闭包: 内层函数对外层函数变量的使用 def outer(): a = 10 def inner(): print(a) return inner ret = outer() ...
- python3 day04 大纲
上节课内容回顾: 1. 小数据池 存储数据. 共享小数据池 int,str, bool == 和 is区别: == 判断的是两边的值 is 左右两端的内存地址 2. 深浅拷贝 1. = 没有创建新对象 ...
- python3 day03 大纲
一. 上次课内容回顾 1. 格式化输出 %d 占位数字 %s 占位字符串 %f 占位小数 "jfklasjkfj%s %d %f" % (值1, 值2, 值3) "{}爱 ...
- python3 day01 大纲
1. 简介python 龟叔 89年 人工智能 2. 特点 优点: 简单, 明确, 优雅,跨平台 缺点: 慢 解释型编程语言 分类: 解释型: 一行一行的把代码进行翻译. 执行效率比较低 优势: 跨平 ...
- python3笔记目录大纲汇总
篇一.python3基础知识和语句 python3笔记一:python基础知识 python3笔记二:进制转换与原码反码补码 python3笔记三:运算符与表达式 python3笔记四:if语句 py ...
- python3 练习题 day02
'''1.有变量name = "aleX leNb" 完成如下操作:1) 移除 name 变量对应的值两边的空格,并输出处理结果2) 移除name变量左边的"al&q ...
- python3.0 day02 列表、元组 、字典、字符串操作
1.列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作,类似于其他语言中的数组. 定义列表 names = ['Lioa',"Tenglan ...
- Python3 第五周大纲(模块,导入方法、import本质,导入优化,模块的分类)
1.定义: 模块:逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能,本质是.py结尾的文件) 2.导入方法 import module_name,module_name2,...... ...
- python开发学习-day02(元组、字符串、列表、字典深入)
s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
随机推荐
- Ubuntu下redis允许外部链接
原文地址: https://blog.csdn.net/a150827/article/details/51352395 redis在ubuntu安装后默认是只有本地访问,需要别的ip访问我们需要修改 ...
- Java集合之Vector源码分析
概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面 ...
- 解决VM提示:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary files.
问题: 在开启虚拟机的时候报: VMware Workstation cannot connect to the virtual machine. Make sure you have rights ...
- 配置Tree Shaking来减少JavaScript的打包体积
译者按: 用Tree Shaking技术来减少JavaScript的Payload大小 原文: Reduce JavaScript Payloads with Tree Shaking 译者: Fun ...
- Node.js+Koa开发微信公众号个人笔记(一)准备工作
本人也是在学习过程中,所以文章只作为学习笔记,如果能帮到你,那就更好啦~当然也难免会有错误,请不吝指出~ 一.准备工作 1.本人学习教程:慕课网Scott老师的<Node.js七天搞定微信公众号 ...
- Python中元组相关知识
下面给大家介绍以下元组的相关知识: ·元组可以看成是一个不可更改的list 1.元组的创建 # 创建空元祖 t = () print(type(t)) # 创建只有一个值的元组 # 观察可知元组中如果 ...
- git命令详解( 二 )
这是git详解的第二篇,最近这个加班有点虚,所以文章也写的比较懒了,一到加班不得已,保温杯里泡枸杞啊,不扯了,今天我们来看看git的第二篇 这篇内容主要是git的一些远程命令 远程仓库 git clo ...
- 【读书笔记】iOS-WiFi长连接
如果你的应用需要一个持久的WiFi长连接,你可以通过设置应用的Info.plist文件中的UIRequiresPersistentWiFi配置项的Boolean值来达到目的.如果这个配置项的值为YES ...
- Spring MVC异常处理 和 重定向传递数据
1.异常处理介绍 Spring在web项目中,如果在请求处理时出现异常,那输出会是Servlet响应.这时异常需要以某种方式转换为响应. Spring将异常转换为响应的方式: a.特定的Spring异 ...
- vue引入bootstrap——webpack
想要在vue中引入bootstrap,引入的时候需要按照如下的步骤进行. 1.引入jquery 2.引入bootstrap 阅读本文前,应该能够搭建环境,使用vue-cli进行项目的创建,可以参考 ...