4.Python
- python是一种接近伪代码的脚本语言。python开发效率高,简单易读,所以有这样一种说法:
Life is Short, You Need Python (Bruce Ecke)
- 原生的python和perl语言一样,对文本处理(字符串操作,正则表达式等)有很好的支持,而且比perl语言更加简单易读,所以目前在生物信息分析中应用非常广泛。
- python和R语言一样有非常好的软件生态,这极大地扩展了python的应用场景。
- 我们能遇到的相当大一部分数据分析的问题,都可以直接用前人已经实现好的python package解决。熟悉这些常用的工具可以让我们从繁琐的编码中解放出来,用很少的代 码量解决实际的问题。
- 和学习R语言一样,为了能用好这些已有的工具,我们首先需要熟悉原生python的语法和常用的数据结构。
- 本教程默认使用python3
python非常注意规范的书写语法,以缩进为例,python强制要求使用tabs/spaces来缩进。推荐使用tab或四个空格来缩进。
# use a tab
for i in range(3):
print(i)
# use 2 spaces
for i in range(3):
print(i)
# use 4 spaces
for i in range(3):
print(i)
创建一个python脚本
welcome.py
,在文件中写入如下内容:print('welcome to python!')
在相同目录下运行:
python welcome.py #use python to run welcome.py
你也可以将python的脚本文件做成一个可执行文件,直接执行, 即在python脚本的第一行添加 python的路径:#! /usr/bin/env pythonprint('welcome to python!')现在就可以不需要指明python解释器,直接运行python脚本了:
chmod +x welcome.py #set the python script as executable
./welcome.py
print("The \n makes a new line")
print("The \t is a tab")
print('I\'m going to the movies')
firstVariable = 'Hello World!'
print(firstVariable)
print(firstVariable.lower())
print(firstVariable.upper())
print(firstVariable.title())
print (1+1)
print (130-2.0)
print (126/3)
print (2*3)
print (2**3)
print (10%3)
Comparison Operator | Function |
---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal |
!= | not equal |
num = 3
if num % 3 == 0:
print("if statement satisfied")
Logical Operator | Description |
---|---|
and | If both the operands are True then condition becomes True. |
or | If any of the two operands are True then condition becomes True. |
not | Used to reverse the logical (not False becomes True, not True becomes False) |
# both the conditions are true, so the num will be printed out
num = 3
if num > 0 and num < 15:
print(num)
my_num = 5
if my_num % 2 == 0:
print("Your number is even")
elif my_num % 2 == 1:
print("Your number is odd")
else:
print("Are you sure your number is an integer?")
a = 1
b = 2
b, a = a, b
print(a, b)
请务必注意,python的索引都是从0开始的,而不是1!
z = | [3, | 7, | 4, | 2] |
---|---|---|---|---|
index | 0 | 1 | 2 | 3 |
- Accessing Values in List:
# Defining a list
z = [3, 7, 4, 2]
# The first element of a list is at index 0
z[0]
# Access Last Element of List
z[-1]
- Slicing Lists:
# first index is inclusive (before the :) and last (after the :) is not.
# not including index 2
z[0:2]
# everything up to index 3
z[:3]
# index 1 to end of list
z[1:]
- Minimum, Maximum, Length, and Sum of a list:
print(min(z), max(z), len(z), sum(z))
- Add to the End of List:
x = [3, 7, 2, 11, 8, 10, 4]
y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester']
x.append(3)
y.append('James')
print(x)
print(y)
- list comprehension:
#Use for loops
a = []
for i in range(10):
a.append(i + 10)
print(a)
#Use list comprehension
a = [i + 10 for i in range(10)]
print(a)
字典是另 一种可变容器模型,可存储任意类型对象。
字典的每个键值
key->value
对用冒号 :
分割,每个键值对之间用逗号 ,
分割,整个字典包括在花括号 {}
中。键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
- 定义和获取字典中的值:
dict = {'a': 1, 'b': 2, 'b': '3'};
dict['b']
- 修改字典:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
- Dict comprehension:
#Use for-loops:
a = {}
for i in range(10):
a[i] = chr(ord('A') + i)
print(a)
#Use dict comprehension:
a = {i:chr(ord('A') + i) for i in range(10)}
print(a)
- 在电脑上安装Anaconda,在jupyter notebook中运行本教程中的相关代码,观察输出.
我们建议安装Anaconda,并使用Jupyter Notebook运行代码,体会python的代码风格和规范。
- Anaconda是一个管理和安装python包的管理软件,它也包含一些非常有用的工具如jupyter notebook
Operating System | Download Link | Notes |
---|---|---|
Mac | | |
Linux | 注意需要添加环境变量 | |
Windows | |
用
conda
安装python package,以h5py
为例:conda install h5py
用
conda
更新h5py
至最新版本:conda update h5py
- 打开 jupyter notebook
jupyter notebook
或者使用软件版的Anaconda中集成的jupyter软件打开。
- 使用 jupyter notebook
- 保存,增加,删除,复制,粘贴代码框,上下移动代码框,运行,终止代码框,重启kernel(将会清空内存),切换代码框版式;
- 使用shift+enter运行代码框,使用enter换行;
- 可以搭配插件nbextenstion使用,提供更多功能。
- 展示图片:

jupyter_image
- 展示dataframe(与pandas配合):

jupyter_dataframe
- 方便的可视化(与matplotlib,seaborn等配合):

jupyter_matplotlib
- 支持markdown:

jupyter_markdown
Last modified 1yr ago