An Informal Introduction to Python
1. Number
>>> -7//3
-3
>>> 7//-3
-3
_ 表示最后的计算结果
2. str
str是immutable的
word = "helloA"
word[1] # "e"
word[-1] #"A"
word[1:3] #"el"
word[3:1] #""
word[1:] #"elloA"
word[100] #exception
word[-100] #exception
len(word) #6
3. Unicode
"我的博客".encode("utf-8")
4. List
List和Str几乎一样,除了List是可以改变的。特别注意有些List的函数返回是None。
L=[1,2,3,4]
L=L.append(5) #none
More Control Flow Tools
1. range()
range(5) #0, 1, 2, 3, 4
range(5, 10) #5, 6, 7, 8, 9
range(0, 10, 3) #0, 3, 6, 9
range(-10, -100, -30) #-10, -40, -70
2. 定义函数
def fib(n):
rst = []
a, b = 0 ,1
while b < n:
rst.append(b)
a, b = b, a+b
return rst
print(fib(1000))
如果一个函数没有return语句,那么这个函数返回None。
3. 默认参数
def f(a, L=[]):
L.append(a)
return L
def g(a, L=None):
if(L == None):
L = []
L.append(a)
return L
print(f(1)) #[1]
print(f(2)) #[1,2]
print(f(3)) #[1,2,3]
print(g(1)) #[1]
print(g(2)) #[2]
print(g(3)) #[3]
4. Lambda
def getFun(n):
return lambda x: x+n
f=getFun(10)
print(f(21)) #31
Data Structures
1. List
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
List comprehension
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
Del
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
2. Tuple
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
3. Set
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)
{'orange', 'bananna', 'pear', 'apple'}
>>> fruit = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket) # create a set without duplicates
>>> fruit
{'orange', 'pear', 'apple', 'banana'}
>>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists
>>> fruit
{'orange', 'apple'}
>>> 'orange' in fruit # fast membership testing
True
>>> 'crabgrass' in fruit
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in either a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
Set Comprehension
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
4. Dictionary
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
Dictionary Comprehension
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
5. Looping Techniques
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
6. More on Conditions
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
7. Comparing Sequences and Other Types
(1, 2, 3) < (1, 2, 4)
[1, 2, 3] < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4) < (1, 2, 4)
(1, 2) < (1, 2, -1)
(1, 2, 3) == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
分享到:
相关推荐
Some of the python tutorial - 《Python学习笔记》
python笔记 Python笔记学习笔记 :memo: 介绍 有时候想找一个东西(写法),但当下却忘记关键字,所以整理一篇学习笔记,要找资料的时候也比较方便。 有些是网路上找的范例,然后自己再修修改改,或者去参考一些...
python_tutorial_notes 我关于python的笔记
python-tutorial::person_running:一些python教程-《 Python学习笔记》
菜鸟教程的python3教程:https://www.runoob.com/python3/python3-tutorial.html 穆雪峰的python3教程:https://www.liaoxuefeng.com/wiki/1016959663602400 python3官方中文手册:https://docs.python.org/zh-cn/3.6/
pythonTutorial 当你完成创建一个名为hello_yourname.py的文件并让它打印“Hello World” 如果成功完成,您应该能够看到以下内容: $ python hello_yourname.py Hello World 现在,告诉 git 你是谁: git config --...
翻译自斯坦福CS231n课程笔记Python Numpy Tutorial,由课程教师Andrej Karpathy授权进行翻译。本篇教程由杜客翻译完成。重新排版后供大家学习。侵删
#课堂笔记 Virtualenvs(演示) Virtualenvs 是独立的完整 python 环境, pip 可用于在其中安装 python 模块。 通常以普通用户身份运行 在其中安装 python 模块不需要 root 权限。 创建虚拟环境 [mike@localhost ...
完整的python教程,用于数据分析和机器学习本教程不假定您具有python的任何先验知识或任何其他编程语言背景。 整个教程都是用jupyter笔记本编写的,我觉得这是此类事情的最佳平台。 如果要在本地运行它们: 下载并...
斯坦福无监督特征学习和深度学习教程(新版)使用 Python 教程网站: 查看此 repo 的 ipython 笔记本的好方法: 为简化起见,下面列出了所有笔记本的链接:
鲨鱼Python教程其中包含ive提出的Python笔记简介,主要是为了我的朋友Tom Perkins帮助他学习Python!
Python教程 在这里,您可以找到我们Python教程的Jupyter笔记本,该教程属于Northeastern生物医学工程学生物工程基本工具部门的一部分。 本模块中的所有课程均组织为Jupyter笔记本。 这意味着您实际上可以运行代码...
PythonTutorial python基本语法 RobotFrameworkBasic RobotFramework基本语法和demo Python学习进阶路线 列表,字典,推导式 迭代器和生成器 学习map、reduce、filter等函数,函数式编程 装饰器 设计模式
适用于NLP,ML,AI的Python教程 (C)2016-2020年,( 另请参阅: 。 请参阅各个文档以及代码文件夹中的文件中的许可详细信息。 此文件夹中的文件是我在计算语言学,自然语言处理(NLP),机器学习(ML)和人工...
Python BioImage分析教程最初于2016年创建在2017年更新并转换为Jupyter笔记本在2018年更新并转换为python 3 作者:乔纳斯·哈特曼(Jonas Hartmann)(EMBL海德堡吉尔穆尔小组)目的和概述本教程讲解了Python中生物...
适用于GEOS-Chem用户的Python / xarray教程 安装 立即免费在云上试用 ,在免费提供的云平台上启动预配置的笔记本环境。 如果您无法加载该页面,请使用Chrome浏览器。 如果加载失败,请刷新页面。 如果页面加载...
包含与O'Reilly 2017 NYC AI大会上提供的“Deep Reinforcement Learning Tutorial”相关的Jupyter笔记本
本文介绍了python OpenCV学习笔记之直方图均衡化,分享给大家,具体如下: 官方文档 – https://docs.opencv.org/3.4.0/d5/daf/tutorial_py_histogram_equalization.html 考虑一个图像,其像素值仅限制在特定的值...
本文介绍了python OpenCV学习笔记直方图反向投影的实现,分享给大家,具体如下: 官方文档 – https://docs.opencv.org/3.4.0/dc/df6/tutorial_py_histogram_backprojection.html 它用于图像分割或寻找图像中感兴趣...
HPC 教程中的 Python Python 是 HPC 社区的通用语言,其工具多种多样,例如可视化大量数据、创建创新的用户界面和运行大型分布式作业。 不幸的是,Python 以速度慢且不适合 HPC 计算而著称。 HPC Python 专家和 ...