python 学习笔记

 

显示类型

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> type([])
<class 'list'>
>>> type(1)
<class 'int'>

 上面试在REPL中操作的,, 如果在源码文件中要显示类型则要加上print

import types
print(type([]))
a = 123;
print(type(a))

 

常用数据结构

list

list 是可变长度的, 可以动态添加元素, 元素类型不要求统一, 有序,可重复

创建空list , 两种方式

alist = []
blist = list()

 直接创建带元素的list

alist = ['one', 'two', 3, True];

 

在末尾追加元素

alist.append('four') # api list.append(obj)

 

在指定索引位置增加元素

alist.insert(1, 'one point five') # api list.insert(index, obj)

查询某个索引位置的元素

element1 = alist[0]
element2 = alist[2]
print(element1)
print(element2)

 

修改某个索引位置的元素

alist[3] = 'three'

删除某个索引位置的元素, 知道索引位置的时候使用

alist = [1, 2 ,3, 4]
a = alist.pop(1)  # 删除api , 参数 要删除的位置, 返回值 被删除的元素
print(a)  # 2
print(alist) #[1, 3, 4]
alist.pop()  # 不给参数时 删除末尾
print(alist) # [1, 3]

 

删除除某个元素, 不知道索引位置的时候

alist = [1, 2, 2, 3]
alist.remove(2) # 删除api , 参数是想要删除的元素,如果元素值有重复的,只会删除最靠前的。 没有返回值
print(alist) #[1, 2, 3]

获取list 长度

size = len(alist)
print(size)

遍历list

for v in alist:
    print(v) # v 是元素值

遍历的时候还要拿到索引

for idx, val in enumerate(alist):
    print(idx, val) # idx是索引, val是元素值

 

 参考:http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops

 

获取某一个值的元素有几个

list = [1, 2 , 2, 4, 'a', 'a','b']
print(list.count(2)) #2
print(list.count('a')) #2

获取最大元素

llist = [1, 2 , 3, 4, True]
max = max(list) # api
print(max) # 4

混合类型的list 使用这个api 要小心, 比如 int 和 str混合会出错

list = [1, 2 , 3, 4, 'a']
max = max(list)
print(max)

运行报错
Traceback (most recent call last):
  File "c:\Users\myname\Documents\pythonporjects\test.py", line 5, in <module>
    max = max(list)
TypeError: '>' not supported between instances of 'str' and 'int'

 

 

tuple

元组的元素一旦确定不可更改,只能查询, 元组本身也不能增加或删除元素。 元素类型不要求统一, 有序, 可重复

创建空tuple, 两种方式

atuple = ()
btuple = tuple()

空tuple的用处不多 所以通常创建就初始化tuple的元素

atuple = (1, 2 , 2, 4, 'a', 'b')
print(atuple)

只有一个元素的tuple 应该这样

atuple = (1, )
print(atuple)

查询元素

atuple = (1, 2 , 2, 4, 'a', 'b')
print(atuple[0])
print(atuple[1])

如果尝试修改元素值则会报错

atuple[0] = 0
Traceback (most recent call last):
  File "c:\Users\myname\Documents\pythonporjects\test.py", line 8, in <module>
    atuple[0] = 0
TypeError: 'tuple' object does not support item assignment

 

获取tuple长度

t = (1, 2, 3)
size = len(t) #api
println(size)

 

获取某一个值的元素有几个

t = (1, 2 , 2, 4, 'a', 'a','b')
print(t.count(2)) # 2
print(t.count('a')) # 2

 

获取最大的元素, 参考 list 章节

tuple unpacking

t = (1 ,2)
a , b = t
print(a) # 1
print(b) # 2

参考 http://stackoverflow.com/questions/10867882/tuple-unpacking-in-for-loops

 

set

set 类似于数学中的集合, 元素无序,元素不可重复

创建空set 只有一种方式

aset = set()

 遍历set , 因为无序所以不存在索引

for el in aset:
    print(el)

 

遍历一个在遍历时增长长度的set

 

http://stackoverflow.com/questions/28584470/iterating-over-a-growing-set-in-python

创建空dictionary, 两种方式

adict = {}
bdict = dict()

 

遍历dictionary

只遍历key

for k in adict:
    print(k) # k

只遍历value

for v in adict.values():
    print(v)

同时遍历key 和 value

for key, value in adict.items():
    print(key, value)

参考 http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python

 

list tuple  string 可以被切片

 

时间和日期处理

 

#!/usr/bin/python
# -*- coding:utf-8 -*-
import time

a = time.time()
print("timestamp" , a)
a = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
print("now is", a)
a = time.localtime()
year = a.tm_year
print("year is", year)

更多参考 Python 日期和时间 | 菜鸟教程 http://www.runoob.com/python/python-date-time.html

 

 

参考 

http://www.runoob.com/python/python-tutorial.html

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

http://stackoverflow.com/questions/tagged/python

https://www.dotnetperls.com/python

posted @ 2017-08-22 14:46  Panda Fang  阅读(466)  评论(0编辑  收藏  举报