2020.09.21星期一  预科班D11

学习内容:

一、基本数据类型及内置方法

1、整形int及浮点型float

+ - * / // ** % > < >= <=

2、字符串类型

(1)按索引值(正向取+反向取):只能取

name = "jack"
print(name[0]) # j
print(name[-1]) # k
name[0] = "A" # 报错

(2)切片(顾头不顾尾)

msg = "hello world"
print(msg[2:5]) # llo
print(msg[2]+msg[3]+msg[6]) # llw

(3)长度len

msg = "hello world"
print(len(msg)) # 11

(4)成员运算in和not in

msg = "hello world"
print("heo" in msg) # False
print("hel" not in msg) # False

(5)移除空白strip

msg = "    hello   "
res = msg.strip()
print(len(res)) # 5
# 练习
inp_usr = input("username>>>:").strip()
inp_pwd = input("password>>>:").strip()
if inp_usr == "jack" and inp_pwd == "123":
print("登陆成功!")
else:
print("登陆失败!")

(6)切分spilt

info = "root:x:0:0::/root:/bin/bash"
res = info.spilt(':')
print(res[-2]) # /root

(7)循环

msg = "hello"
for x in msg:
print(x)
#h
#e
#l
#l
#o

3、列表类型

l = ['a', 1111, 'c']
l[0] = 'A'
print(l) # A 1111 c

(1)按索引存取值(正向存取+反向存取):既可存也可取

l = ['a', 1111, 'c']
l[3] = 666
print(l) # a 1111 c 666

(2)切片(顾头不顾尾,步长)

l = [111, 222, 333, 444, 555]
print(l[0:3]) # 111 222 333

(3)长度

l = ['a', 1111, 'c']
print(len(l)) # 3

(4)成员运算in和not in

l = ['a', 1111, 'c']
print('c' in l) # True
print(1111 in l) # True

(5)追加

l = ['a', 1111, 'c']
l.append('d')
l.append('d')
print(l) # a 1111 c d d
l = ['a', 1111, 'c']
l.insert(1,6666)
print(l) # a 6666 111 c

(6)删除

l = [111, 222, 333, 444, 555]
del l(2)
print(l) # 111 222 444 555

(7)循环

l = [111, 222, 333, 444, 555]
for x in l:
print(x)

4、字典型dict

(1)按key存取值:可存可取

dic={"k1":111, "k2":2222}
dic["k1"] = 666
print(dic) # {"k1":666, "k2":2222}
dic={"k1":111, "k2":2222}
dic["k3"] = 7777
print(dic) # {"k1":111, "k2":2222, "k3":7777}

(2)长度

dic={"k1":111, "k2":2222}
print(len(dic)) # 2

(3)成员运算in和not in

dic={"k1":111, "k2":2222}
print("k1" in dic) # True
print(2222 in dic) # False

(4)删除

dic={"k1": 111, "k2": 2222}
del dic["k1"]
print(dic) # {"k2": 2222}

(5)循环

dic={"k1":111, "k2":2222}
for x in dic:
print(x, dic(x))

二、文件处理

文本编辑

name = input("请输入你的用户名:")
f = open("user.txt", mode='w')
# print(f)
f.write(name)
f.close()
f = open("user.txt", mode='w')
# print(f)
f.write("hello")
f.close()
f.write("world") # world报错,因为文件已关闭

文本查看

f = open("user.txt",mode='r')
data = f.read()
print(data)
f.close() # 一次性读取,对内存要求高,速度快
f = open("user.txt",mode='r')
for line in f:
print(line,end="")
f.close() # 按行读取,对硬盘要求高,速度略慢

文件的修改

f = open("user.txt", mode='r+')
f.seek(6)
f.write("wo")
f.close() # 你好hello 改为 你好wollo
#先以读的形式将user.txt全部读入内存,在内存中修改文件内容且保存在内存中
f = open("user.txt",mode='r', encoding='utf-8')
data = f.read()
res = data.replace("你好","666")
print(res)
f.close()
#以写的形式打开文件,将文件中内容清除掉(原文件内存中有备份)
f = open("user.txt", mode='w',encoding='utf-8')
#将新内容覆盖原文件
f.write(res)
f.close()

最新文章

  1. 参考bootstrap中的popover.js的css画消息弹框
  2. 如何通过JS调用某段SQL语句
  3. Markdown编辑器:Typora
  4. 浅谈算法和数据结构: 十 平衡查找树之B树
  5. jsp中头的导入两种方式区别
  6. 酷狗 KRC 文件的解析
  7. collections中可命名元组和队列
  8. fabric批量操作远程操作主机的练习
  9. MVC--View Razor(1)
  10. html&amp;css&amp;js随笔-问题集锦
  11. nginx : TCP代理和负载均衡的stream模块
  12. ubuntu FTP服务安装
  13. CSDN改版问题多多
  14. ios根据文本自适应 然后 搭建类似如下效果
  15. 2017全球互联网技术大会回顾(附PPT)
  16. day9 函数练习题
  17. P1852 [国家集训队]跳跳棋
  18. class , field , method
  19. PyQt5 中调用MySql接口失败 ( QSqlDatabase 组件) 在Linux环境下如何修改
  20. BZOJ 1070 修车 【费用流】

热门文章

  1. MongoDB最新4.2.7版本三分片集群修改IP实操演练
  2. vscode下终端返回中文乱码
  3. js实现将时分秒转化成毫秒,将秒转化成时分秒
  4. IA-32/centos7开机流程
  5. Tmux安装和使用
  6. LG P4161 [SCOI2009]游戏/LG P6280 [USACO20OPEN]Exercise G
  7. MapReduce 的 shuffle 过程中经历了几次 sort ?
  8. MPI实现Jacobi
  9. python yaml文件数据按原有的数据顺序dump
  10. openCV - 3. Mat对象