#!/usr/bin/python
#coding=utf-8
#循环设计
#范围函数
S = 'abcdefghijk'             #a b c d e f g h i j k
for i in range(0, len(S), 2):#0 1 2 3 4 5 6 7 8 9 10 11print S[i]
print len(S)
#遍历数组
S = 'abcdefghijk'
for (index, char) in enumerate(S):print indexprint char
#多个等长的序列
ta = [1, 2, 3]
tb = [9, 8, 7]
tc = ['a', 'b', 'c']
for (a, b, c) in zip(ta, tb, tc):print(a, b, c)
#分解聚合
ta = [1,2,3]
tb = [9,8,7]
# cluster
zipped = zip(ta,tb)
print (zipped)
# decompose
na, nb = zip(*zipped)
print(na, nb)
#循环对象
f = open('test.txt')
f.next()
f.next()
f.next()
f.next()
f.next()
f.next()
f.next()
for line in open('test.txt'):print line
#生成器
def gen():a = 100yield aa = a*8yield ayield 1000
for i in gen():print i
def gen():for i in range(4):yield i
G = (x for x in range(4))
#表推导
L = []
for x in range(10):L.append(x**2)
print L
L = [x**2 for x in range(10)]
print L
#函数对象
#lambda函数
func = lambda x,y: x + y
print func(3, 4)
def func(x, y):return x + y
print func(3, 4)
#函数作为参数传递
def test(f, a, b):print 'test'print f(a, b)
test(func, 3, 5)
def U(a, b, c):print 'U'print a(b, c)
U(func, 5, 6)
#map()函数,map()的功能是将函数对象依次作用于表的每一个元素
re = map((lambda x:x+3),[1, 2, 3, 5, 6])
print re
re = map((lambda x,y: x+y),[1,2,3],[6,7,9])
print re
#filter()函数,如果函数对象返回的是True,则该次的元素被储存于返回的表中
def func(a):if a > 100:return Trueelse:return False
print filter(func, [10, 56, 101, 500])
#reduce()函数,reduce可以累进地将函数作用于各个参数
print reduce((lambda x, y: x + y), [1, 2, 3, 4,9])
#错误处理
re = iter(range(5))
try:for i in range(100):print re.next()
except StopIteration:print 'here is end ',i
print 'HaHaHaHa'
'''
re = iter(range(5))
for i in range(100):print re.next()
print 'HaHaHaHa'
try:print(a*2)
except TypeError:print("TypeError")
except:print("Not Type Error & Error noted")
def test_func():try:m = 1/0except NameError:print("Catch NameError in the sub-function")
try:test_func()
except ZeroDivisionError:print("Catch error in the main program")
print 'Lalala'
raise StopIteration('this is error')
print 'Hahaha'
'''
#动态类型
a = 3
a = 'au'
print a
a = 5
b = a
a = a + 2
print a
print b
L1 = [1, 2, 3]
L2 = L1
L1 = 1
print L1, L2
L1 =[1, 2, 3]
L2 =L1
L1[0] = 10
print L2
#从动态类型看函数的参数传递
def f(x):x = 100print x
a = 1
f(a)
print a
def f(x):x[2] = 100print x
a =[1, 2, 3]
f(a)
print a
xl =[1, 3, 5]
yl =[9, 12, 13]
L = [x**2 for (x,y) in zip(xl, yl) if y > 10]
print L
#通过参数传递,判断数字、字符串、list、tuple、词典等数据类型是否为可变数据对象
a = 2
b = '啊哈'
c = [1, 2, 3]
d = (1, 2, 3)
e = {'tom': 11, 'sam': 57, 'lily': 100}
def num(x):       #数字x = 100print x
num(a)
print a
def str(x):        #字符串x = 'ade'print x
str(b)
print b
def list(x):        #listx[0] = 100print x
list(c)
print c
def tuple(x):       #tuplex[0] = 100print xtuple(d)raise StopIteration()
print d
def dic(x):         #词典x['tom'] = 100print x
dic(e)
print e

转载于:https://blog.51cto.com/309173854/1869517

python 学习之 PythonAdvance2相关推荐

  1. pygame是python的一个库吗,python学习pygame,,基本库导入impor

    python学习pygame,,基本库导入impor 基本库导入 import pygame import sys from pygame.locals import * 初始化 pygame.ini ...

  2. python科学计数法转换_对比Python学习Go 基本数据结构

    公众号文章不方便更新,可关注底部「阅读原文」博客,文章随时更新. 本篇是「对比 Python 学习 Go」[1] 系列的第三篇,本篇文章我们来看下 Go 的基本数据结构.Go 的环境搭建,可参考之前的 ...

  3. python学习------tab补全

    python学习------tab补全   python也可以进行tab键补全 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/usr/bin/env  ...

  4. Python学习day5作业-ATM和购物商城

    Python学习day5作业 Python学习day5作业 ATM和购物商城 作业需求 ATM: 指定最大透支额度 可取款 定期还款(每月指定日期还款,如15号) 可存款 定期出账单 支持多用户登陆, ...

  5. Python学习系列(六)(模块)

    Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...

  6. 最新Python学习项目Top10!

    作者 | Mybridge 译者 | Linstancy 整理 | Jane 出品 | AI科技大本营 [导读]过去一个月里,我们对近1000个Python 学习项目进行了排名,并挑选出热度前10的项 ...

  7. 200页!分享珍藏很久的Python学习知识手册(附链接)

    这是之前入门学习Python时候的学习资料,非常全面,从Python基础.到web开发.数据分析.机器学习.深度学习.金融量化统统都有,该手册是HTML版本,左侧是目录,可以点击,右侧是对目录知识点的 ...

  8. Python学习系列(五)(文件操作及其字典)

    Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件      在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...

  9. Python学习01 Hello World

    Python学习之Hello World 准备工作 去官网http://www.python.org/ 下载python的安装包: http://www.python.org/download/ 当前 ...

最新文章

  1. StackOverFlow上你没看过的7个Java最佳答案
  2. c语言如何判断数据是否符合正态分布_如何判断机器学习数据集是否是线性的?...
  3. Eclipse + Apache Axis2 发布RESTful WebService(一)基础知识
  4. CommonLang3中的StringUtils最全解析
  5. 【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)
  6. Linux 命令之 arch --显示主机的硬件结构类型
  7. Eclipse在选项卡上展示某个具体的视图
  8. python添加自定义模块_Python中添加自定义模块的方法
  9. stringWithUTF8String return null (返回null)的解决办法
  10. Camshift算法原理及其Opencv实现
  11. 用Kotlin开发Android的Hello Kotlin!!
  12. Spring里用到了哪些设计模式
  13. 极客大学产品经理训练营:需求评审 第13课总结
  14. 乌班图vim怎么编译c语言,在Ubuntu上利用vim进行程序编写及运行
  15. a标签点击中文文件名乱码_a标签文件下载文件名乱码问题
  16. 小米手机怎么设置鸿蒙开机动画,小米9开机动画太酷炫了!还不知道怎么设置赶紧来看看!...
  17. cpuz测试分数天梯图_怎么看CPU性能排行 CPU天梯图2018年5月最新版 (全文)
  18. 亿阳信通图像处理工程师
  19. excel中插入的图表保存时提示 无法保存 html,excel表格保存不了的解决方法步骤...
  20. /etc/sysconfig/network: 没有那个文件或目录

热门文章

  1. BS-GX-017基于SSM实现的在线考试管理系统
  2. LeetCode周赛191
  3. Idea 启动项目 很慢,总会到某个点进行延迟卡顿。
  4. swift菜鸟入门视频教程-03-字符串和字符
  5. [ C++ ] 理解const
  6. 【我看Hibernate】Hibernate 介绍及其简单应用
  7. 在 64 位版本的 Windows 上,如何在 32 位版本的 ASP.NET 1.1 和 64 位版本的 ASP.NET 2.0 之间切换...
  8. SQLSERVER中的网络配置
  9. libpython3.7m so静态库_Linux下编译安装python3.7
  10. stl clocklist 查找元素_C++|通俗理解STL