python基础教程

1、python ?和??

  • 使用方法:对象(变量和函数)+?
    • ?显示该对象的基本信息
    • ??如果是函数会显示源码
def print_info():
    """
        show information
    """
    print 'Hello ChinaHadoop'

In [3]: print_info?
Signature: print_info()
Docstring: show information
File:      ~/<ipython-input-1-b6ccb9d452c1>
Type:      function

In [4]: print_info??
Signature: print_info()
Source:
def print_info():
        """
            show information
        """
        print 'Hello ChinaHadoop'
File:      ~/<ipython-input-1-b6ccb9d452c1>
Type:      function

2、python魔术命令

  • %timeit
    多次执行一条或多条语句(%%timeit)返回平均时间

    In [9]: %timeit [x for x in range(10)]
    The slowest run took 8.26 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000000 loops, best of 3: 722 ns per loop
  • %time
    返回执行一条语句的时间,%%time用于多条语句

    In [10]: %%time
    ....: l = []
    ....: for x in range(100000):
    ....:     l.append(x)
    ....: 
    CPU times: user 14 ms, sys: 4 ms, total: 18 ms
    Wall time: 18.1 ms
  • %rest
    删除当前空间的全部变量

3、xrang和range区别

  • xrange返回生成器,range返回数组。用法一致,xrange速度快。
In [12]: range(1,5)
Out[12]: [1, 2, 3, 4]

In [13]: xrange(1,5)
Out[13]: xrange(1, 5)

4、引用

  • 变量复制传参是值传递
  • 列表字典的传递是引用传递
In [14]: def fun2(lst):
   ....:         lst[0] = 5
   ....:         print lst
   ....:     

In [15]: lst1 = range(5)

In [16]: print lst1
[0, 1, 2, 3, 4]

In [17]: fun2(lst1)
[5, 1, 2, 3, 4]

In [18]: print lst1
[5, 1, 2, 3, 4]

5、深拷贝和浅拷贝

  • 浅拷贝只拷贝父对象,不拷贝对象内部子对象
  • 深拷贝同时拷贝父对象和子对象
In [21]: a = [[1, 2, 3], [4, 5, 6]]

In [22]: b = a

In [23]: c = copy.copy(a)

In [24]: d = copy.deepcopy(a)

In [25]: print 'a-id:', id(a)
a-id: 139997471227488

In [26]: print 'b-id:',id(b)
b-id: 139997471227488

In [27]: print 'c-id:',id(c)
c-id: 139997442508272

In [28]: print 'd-id:',id(d)
d-id: 139997442507408

In [29]: a.append(15)

In [30]: a[1][2] = 10

In [31]: print 'processed...'
processed...

In [32]: print a
[[1, 2, 3], [4, 5, 10], 15]

In [33]: print b
[[1, 2, 3], [4, 5, 10], 15]

In [34]: print c
[[1, 2, 3], [4, 5, 10]]

In [35]: print d
[[1, 2, 3], [4, 5, 6]]

6、元组

  • 元组的元素不能修改
tup1 = (1, 2, 3)
print tup1

 嵌套元组
tup2 = ((1, 2, 3), (4, 5))
print tup2
  • 转换,tuple
In [36]: l = [1, 2, 3]

In [37]: print tuple(l)
(1, 2, 3)

In [38]: str = 'Hello ChinaHadoop'

In [39]: print tuple(str)
('H', 'e', 'l', 'l', 'o', ' ', 'C', 'h', 'i', 'n', 'a', 'H', 'a', 'd', 'o', 'o', 'p')

In [40]: print tuple(str,)
('H', 'e', 'l', 'l', 'o', ' ', 'C', 'h', 'i', 'n', 'a', 'H', 'a', 'd', 'o', 'o', 'p')

In [41]: print tuple([str])
('Hello ChinaHadoop',)
  • 访问元组 tup2[index]
In [43]: tup2 = ((1, 2, 3), (4, 5))

In [44]: tup2
Out[44]: ((1, 2, 3), (4, 5))

In [45]: 
Out[45]: (1, 2, 3)
  • 合并元组 tup1 + tup2 , (tup1,) + tup2
In [50]: tup1
Out[50]: (1, 2, 3)

In [51]: tup2
Out[51]: ((1, 2, 3), (4, 5))

In [52]:  tup1 + tup2
Out[52]: (1, 2, 3, (1, 2, 3), (4, 5))

In [59]: (tup1,) + tup2
Out[59]: ((1, 2, 3), (1, 2, 3), (4, 5))
  • 拆包 a, _, c = tup3
 In [66]: tup3 = (tup1,) + tup2

In [67]: tup3
Out[67]: ((1, 2, 3), (1, 2, 3), (4, 5))

In [68]: a, b, c = tup3

In [69]: a
Out[69]: (1, 2, 3)

In [70]: b
Out[70]: (1, 2, 3)

In [71]: c
Out[71]: (4, 5)

# 函数返回多个值 _表示忽略这个值
def return_multiple():
    t = (1, 2, 3)
    return t

a, _, c = return_multiple()
print c

# 元组列表迭代
tuple_lst = [(1, 2), (3, 4), (5, 6)]
for x, y in tuple_lst:
    print x+y

6、列表

  • 创建列表
lst_1 = [1, 2, 3, 'a', 'b', (4, 5)]
print lst_1
  • tuple转list用list()
In [72]:  tup = tuple('helloword')

In [73]:  tup
Out[73]: ('h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd')

In [74]:  lt = list(tup)

In [75]:  lt
Out[75]: ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd']
  • 添加删除元素,append()在末尾添加,insert(position,value)
In [76]: lst_4 = range(10)

In [77]: # 末尾添加

In [78]: lst_4.append(11)

In [79]: print lst_4
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]

In [80]: # 指定位置插入

In [81]: lst_4.insert(5, 12)

In [82]: print lst_4
[0, 1, 2, 3, 4, 12, 5, 6, 7, 8, 9, 11]
  • 删除元素,pop()删除制定位置元素并返回
  • 删除指定的值,注意’12’在这里是“值”不是“位置”
In [84]: item = lst_4.pop(6)

In [85]: print item
5

In [86]: print lst_4
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 11]

In [87]: lst_4.remove(12)

In [88]: print lst_4
[0, 1, 2, 3, 4, 6, 7, 8, 9, 11]
  • 列表合并, + 和 expend() 方法
In [93]: lst_1 = [1, 2, 3, 'a', 'b', (4, 5)]

In [94]: print lst_1
[1, 2, 3, 'a', 'b', (4, 5)]

In [95]: lst_2 = range(1, 9)

In [96]: print lst_2
[1, 2, 3, 4, 5, 6, 7, 8]

In [97]: lst_3 = lst_1 + lst_2

In [98]: print lst_3
[1, 2, 3, 'a', 'b', (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]

In [99]: lst_1.extend(lst_2)

In [100]: print lst_1
[1, 2, 3, 'a', 'b', (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]
  • 切片操作
 In [101]: lst_5 = range(10)

In [102]: print lst_5
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [103]: print lst_5[1:5]
[1, 2, 3, 4]

In [104]: print lst_5[5:]
[5, 6, 7, 8, 9]

In [105]: print lst_5[:5]
[0, 1, 2, 3, 4]

In [106]: print lst_5[-5:]
[5(-5), 6(-4), 7(-3), 8(-2), 9(-1)]

#包含前面的不包含后面的[) 左闭右开
In [107]: print lst_5[-5:-2]
[5, 6, 7]

#2为步长
In [108]: print lst_5[::2]
[0, 2, 4, 6, 8]

In [109]: print lst_5[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

7、常用的序列函数

  • enumerate返回索引和值
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course'] # (0, 'Welcome')
for i, item in enumerate(lst_6):
    print '%i-%s' %(i, item)
  • sorted 原数组不变,返回结果给新数组
In [110]: import random

In [111]: lst_5 = range(10)

In [112]: random.shuffle(lst_5)

In [113]: print lst_5
[3, 4, 0, 2, 9, 6, 1, 5, 8, 7]

In [114]: #sorted

In [115]: lst_5_sorted = sorted(lst_5)

In [116]: print lst_5
[3, 4, 0, 2, 9, 6, 1, 5, 8, 7]

In [117]: print lst_5_sorted
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • zip压缩,将多个序列对应位置的元素组成元组
In [118]: lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course']

In [119]: lst_7 = range(5) #[0, 1, 2, 3, 4]

In [120]: lst_8 = ['a', 'b', 'c']

In [121]: zip_lst = zip(lst_6, lst_8, lst_7)

In [122]: print zip_lst
[('Welcome', 'a', 0), ('to', 'b', 1), ('Python', 'c', 2)]

In [123]: zip(*zip_lst)
Out[123]: [('Welcome', 'to', 'Python'), ('a', 'b', 'c'), (0, 1, 2)]

8、字典

  • 创建字典
In [124]: empty_dict = {}

In [125]: dict1 = {'a': 1, 2: 'b', '3': [1, 2, 3]}

In [126]: print empty_dict
{}

In [127]: print dict1
{'a': 1, 2: 'b', '3': [1, 2, 3]}
  • 插入元素 name_dict[index]=value
In [128]: dict1[4] = (4, 5)

In [129]: print dict1
{'a': 1, 2: 'b', 4: (4, 5), '3': [1, 2, 3]}
  • 删除元素 del name_dict[index] name_dict.pop(‘key’)
In [130]: del dict1[2]

In [131]: print dict1
{'a': 1, 4: (4, 5), '3': [1, 2, 3]}

In [132]: a_value = dict1.pop('a')

In [133]: print a_value
1

In [134]: print dict1
{4: (4, 5), '3': [1, 2, 3]}
  • 合并字典 dict1.update(dict2)
In [135]: dict2 = {4: 'new1', 5: 'news'}

In [136]: dict1.update(dict2)

In [137]: print dict1
{'3': [1, 2, 3], 4: 'new1', 5: 'news'}
  • 多个列表创建字典 dict_4 = dict(zip(l1, l2))
In [153]: dict_3 = {}

In [154]: l1 = range(10)

In [155]: l2 = list(reversed(range(10)))

In [156]: for i1, i2 in zip(l1, l2):
   .....:         dict_3[i1] = i2
   .....:     

In [157]: print dict_3
{0: 9, 1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 3, 7: 2, 8: 1, 9: 0}

In [158]: dict_4 = dict(zip(l1, l2))

In [159]: print dict_4
{0: 9, 1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 3, 7: 2, 8: 1, 9: 0}

9、集合

  • 并集(|)交集(&)差集(-)异或(^)
In [160]: a = set(range(10))

In [161]: print a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [162]: b = set(range(5,15))

In [163]: print b
set([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

In [164]: a | b
Out[164]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

In [165]: a & b
Out[165]: {5, 6, 7, 8, 9}

In [166]: a - b
Out[166]: {0, 1, 2, 3, 4}

In [167]: a ^ b
Out[167]: {0, 1, 2, 3, 4, 10, 11, 12, 13, 14}
  • 子父集判断 issubset是否是子集,issuperset判断是否为父集
In [168]: a.issubset(b) # a是b的子集吗?
Out[168]: False

In [169]: a.issuperset(b) #a是b的父集吗?
Out[169]: False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值