Python Basic
来源:互联网 发布:hadoop 数据清洗工具 编辑:程序博客网 时间:2024/05/21 10:04
Python Basic
此系列文章为Dataquest 网站学习后的复习整理资料,仅供参考。更多细节请查看dataquest.io
python basic 我仅列举部分有用函数,具体用法请查看python官方文档。
print()type() #查看类型str() #转换为strint() #转换为整型
列表list:
list =[]list.append()len(list)
关于list的slicing
li[start : end : step]
具体用法介绍博客很多,不班门弄斧。
文件的打开
我们使用open(a,b)打开文件,其中a为文件名,注意需为字符串类型,b为打开的方式,此时返回一个file类
对其可使用read()
f = open("test.txt", "r")g = f.read()
str的split()
sample = "john,plastic,joe"split_list = sample.split(",")# 返回一个列表: ["john", "plastic", "joe"]
循环语句不多介绍
布尔类型
变量之间可以进行比较如:
==
!=
>
<
IF语句
不多介绍 值得关注的是
if (判断1) &(判断2):if(判断1) | (判断2)
可配合in使用
if a in lista:
str.replace(a,b)#a为原文 b为替换后的文字str.lower(upper)import my_module as mfrom math import function1, function2from my_module import *import csvf = open("my_data.csv")csvreader = csv.reader(f)my_data = list(csvreader)for idx, value in enumerate(['foo', 'bar']): print(idx, value)# 0 foo and 1 bar 遍历index与内容
对于class
__str__可以告诉python如何以str的方式解释实例
set(list) 返回set对象 为unique
set对象有add() remove()方法用于添加删减
list(set) 返回list对象
try: int('')except Exception as exc: print(type(exc)) print (str(exc))#当python产生except时实际上产生了一个Exception类 以上将分别输出#<class 'ValueError'>#invalid literal for int() with base 10: ''#错误类型与错误信息#我们可以在except中使用pass以忽略错误信息
对于dict我们可以使用.items()来同时遍历key与value
正则表达式
.匹配任何字符串
^a 匹配以a开头
a$ 匹配以a结尾
[]匹配中括号中任意一项
对于特殊字符,在前面加上\来转义
| 表示或 匹配左边或右边
[0-9]匹配所有数字
[a-z]所有小写
{}指示重复的次数
re module用于正则表达式
re.search(regex,string)re.sub(pattern,repl,string)#re.sub("yo", "hello", "yo world") 将返回hello worldre.findall(regex,string)#返回一个列表,包含所有匹配项
time module
time.time()返回一个timestamp类
time.gmtime(timestamp)返回一个struct_time类, 其有tm_year,tm_month,tm_mday,tm_hour,tm_min等属性
datetime module
datetime module 有datetime class
有year,month,day,hour,minute,second,microsecond属性
current_datetime = datetime.datetime.now()
timedelta 类用于进行时间上的计算
today = datetime.datetime.now()diff = datetime.timedelta(days = 1)tomorrow = today + diffyesterday = today - diff
datetime.datetime.strftime()用于输出格式化的日期
march3 = datetime.datetime(year = 2010, month = 3, day = 3)pretty_march3 = march3.strftime("%b %d, %Y")
相反的datetime.datetime.strptime()可以从字符串中提取返回一个datetime实例
march3 = datetime.datetime.strptime("Mar 03, 2010", "%b %d, %Y")
具体字符含义请见相关文件
datetime_object = datetime.datetime.fromtimestamp(a)#a为timestamp实例,可将其转为datetime实例
- Python Basic
- Python Basic
- python basic
- python basic
- Python Basic
- Python Basic
- Python----Basic
- Python Basic
- Python 2 basic
- python regular expression basic
- Basic OOP in Python
- python Basic Authentication
- PAT basic 1036 python
- Python basic interview questions
- Python basic note 01
- Python 编码Basic Auth
- basic algorithm in python
- Python Basic Examples
- JDK之Iterator
- 如何解决npm should be run outside of the node repl,in your normal shell
- 快速上手MySql && MySql GUI工具 SQLyog Community (4)
- Linux系统目录结构
- android TextView 实现图文混排
- Python Basic
- linux tomcat web新开端口
- spring框架Proxy-代理模式(动态生成代理对象)
- android4.4简单摘要
- JVM运行时数据区
- 笔记
- Java学习轨迹【面向对象(2)】
- 转动的风扇
- 内部类