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()

预科班D11的更多相关文章

  1. 预科班D8

    2020.09.16星期三 预科班D8 学习内容: 一.注释 单行注释 ==>用 # 写在上一行或者该行后面 多行注释 ==>用 ''' '''或""" &q ...

  2. 预科班D6

    2020.09.14星期一 预科班D6 学习内容: 自习 发布小游戏 1.配置网络 #查看当前ip ifconfig #关闭NetworkManager systemctl stop NetworkM ...

  3. 预科班D2

    2020.09.08星期二 预科班D2 学习内容: 一.复习 1.平台: 平台=操作系统+计算机硬件 2.跨平台性 3.文件 文件是指操作系统提供给上层使用者操作硬盘的一种功能.

  4. 预科班D9

    2020.09.17星期四 预科班D9 学习内容: 一.列表与字典的嵌套 大前提:将所有同学的信息存起来,取值需求 1.取第二个学生的性别 stus_info = [ {"name" ...

  5. 预科班D12

    2020.09.22星期二 预科班D12 学习内容: 一.修改文件的两种方式 1.方案一 思路:(1)先以r形式打开源文件    (2)将源文件内容一次性读入内存中,在内存中修改完毕    (3)以w ...

  6. angularJS(2)

    angularJS(2) 今天先讲一个angularJs的表单绑定实例: <div ng-app="myApp" ng-controller="formCtrl&q ...

  7. AngularJs之二

    今天先讲一个angularJs的表单绑定实例: <div ng-app="myApp" ng-controller="formCtrl"> < ...

  8. Wooyun隐写术总结

    之前还没有见到drops上有关于隐写术的总结,我之前对于隐写术比较有兴趣,感觉隐写术比较的好玩.所以就打算总结总结一些隐写术方面的东西.写的时候,可能会有错误的地方,请不吝赐教,谢谢. 本篇章中用到的 ...

  9. mysql学习【第3篇】:使用DQL查询数据

    狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! mysql学习[第3篇]:使用DQL查询数据 DQL语言 DQL( Data Query Lan ...

随机推荐

  1. java 二分查找的注意事项

    二分查找也是最简单的算法之一了.但是最近发现一般的写法会有问题. public int search(int[] nums, int target) { int left = 0; int right ...

  2. 解决 supervisor : 无法加载文件 C:\Users\charles\AppData\Roaming\npm\supervisor.ps1

    在使用vsCode中运行cnpm install时报错. 解决方法 1.在win10 系统中搜索框 输入 Windows PowerShell,选择 管理员身份运行 2.使用,win+R打开了powe ...

  3. go thrift demo

    接口:https://gowalker.org/github.com/apache/thrift/lib/go/thrift 参考文件:https://cong.im/2018/05/14/other ...

  4. nova start 虚机的代码流程分析

    nova start 虚机的代码流程分析,以ocata版本为分析基础1.nova api服务接受用户下发的 nova start启动虚机请求其对应的http restfull api接口为post / ...

  5. 记一次因为Gradle与Lombok不兼容导致编译时的内存溢出 Expiring Daemon because JVM heap space is exhausted

    1.现象 版本 Gradel:6.1.1 / 6.5.1 Lombok:1.8.6 / 1.8.10 截图 解决过程 调大idea的堆内存 不行 × idea安装目录中找到 idea64.exe.vm ...

  6. 个人项目(C语言)

    GitHub地址:https://github.com/dachai9/personal-project.git 1. WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数 ...

  7. Java多线程_ReentrantLock

    ReentrantLock是重入锁,它与synchronized很像,它是synchronized的加强版,因为它具有一些synchronized没有的功能.下面我们看看两者的区别:synchroni ...

  8. jmeter将上一个接口的返回值作为下一个接口的请求参数

    接口响应结果,通常为HTML.Json格式的数据,对于HTML的响应结果的提取,可以通过正则表达式,XPath提取. 对于Json格式响应结果,可以通过正则表达式.JSON Extractor插件.B ...

  9. centos6.8上安装部署 jhipster-registry

    必备环境:jdk8,git,maven 1.安装nodejs #由于采用编译的方式很容易出现一些意外的惊喜,所以我们这儿直接用yum命令安装 #1.查看nodejs版本(命令中不要加 -y 如果版本不 ...

  10. Unable to add window -- token null is not for an application错误的解决方法 android开发

    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...