莫愁 Python基础

改变tab

选中——command+[

for循环
for i in range(1,10,2)
#range范围:前闭后开
#步长为2
    print(i)
升级模块

sudo pip3 install -U NumPy

写入文件
text = 'This is my first line.\nThis is another line.'
#/n换行符
my_file = open('my file.txt','w')
#如文件存在,open会打开它;否则,新建
#'w'为写入模式,'r'为只读,'a'为追加
my_file.write(text)
my_file.close()
#文件读取/写入后要关闭
读取文件
file = open('my file.txt','r')
content = file.readlines()
#readline为只读一行
print(content)
列表和元组
list.append()
list.insert(1,0)
#列表位置1,添加0
list.remove(写value,而非序号)
print(list[-1])
#打印列表最后一位 
print(list[:3])
#打印第0到第3位
print(list[5:])
#打印第5位之后的数
print(list[-3:])
#打印倒数后三位
print(list.index(value)) 
#打印索引
print(list.count(value))
#统计某个value出现了几次
list.sort()
#从大到小排序
list.sort(reverse=True)
#从小到大排序
多维列表

多维列表索引
print(multi_dim_list[0][0])

字典

字典没有顺序,列表有顺序
d = { key1:value1, key2:value2, key3:value3, ....}

删除字典中的键值对

del d[key]

增加字典中的键值对

d[key4] = value4
import官方模块
import time as t
#引用时写 t.
from time import time,locatime
#引用时可不用写time.
form time import *
#引用时可不用写time
import自己的模块

1.保存自己写的模块
2.将模块放入python的site-package文件夹中或要调用的文件的同一文件夹中

continue和break
错误处理try
try:
    file = open('e','r+')
except Exception as e:
    print('There is no file named as e.')
    response = input('Do you want to create a new file? ')
    if response == 'Y'
        file = open('e','w')
    else:
        pass
else:
    file.write('s')
file.close()
屏幕快照 2019-08-01 下午2.07.48.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • python中的分支结构只有if语句 1,if语句 1)结构1:满足某个条件才执行某段代码a:语法if 条件语句:...
    扎克chen阅读 233评论 0 1
  • 操作系统命令三剑客-awk 一: 三剑客命令awk概述gawk : pattern scanning and pr...
    ManBu_x阅读 317评论 0 1
  • 第十一天 护网 日常登入EDR等网站查看主机是否上线漏洞威胁等 python while循环语句 https://...
    c7188eaf395a阅读 98评论 0 0
  • 知识梳理:1) awk命令概念介绍: 语法 原理 BEGIN 处理文件 END2) awk命令操作说明: 作用 查...
    Linux63阅读 228评论 0 0
  • 0、前言 在正式开始之前,按照套路,先得介绍点adb的基础知识。学会了adb的使用,下面的大部分内容其实基本不用看...
    netppp阅读 3,572评论 0 0