<jupyter_start><jupyter_text>Python教程  1. 语法  1.1 Python 保留字段<jupyter_code>import keyword
print(keyword.kwlist)<jupyter_output>['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']<jupyter_text>1.2 注释：python 中可以进行单行注释和多行注释<jupyter_code># 这是一个单行注释

'''
这是一个多行注释
'''

print('Hello world')<jupyter_output>Hello world<jupyter_text>1.3 缩进：python中强制采用四个空格作为缩进，作用等同于{}<jupyter_code>for i in range(3):
    print(i)
for i in range(3):
print(i)<jupyter_output><empty_output><jupyter_text>1.4 多行语句：多行语句使用反斜杠(\\)实现<jupyter_code>item_one = 1
item_two = 2
item_three = 3

total = item_one + \
        item_two + \
        item_three
total<jupyter_output><empty_output><jupyter_text>1.5 格式化输出<jupyter_code>a = 1
b = 'apple'
print('a = %d and b = %s' % (a, b))
print('a = {} and b = {}'.format(a, b))
print('a = {1} and b = {0}'.format(a, b))<jupyter_output>a = apple and b = 1<jupyter_text>2. 数据类型 python中有六个标准的数据类型： + Number(数字)+ String(字符串)+ List(列表)+ Tuple(元组)+ Sets(集合)+ Dict(字典)**所有变量不需要提前定义，直接赋值即可**  2.1 数字&字符串<jupyter_code>a = 1
b = 0.2
c = 'Hello'

print('type(a) = {}, a = {}'.format(type(a), a))
print('type(b) = {}, b = {}'.format(type(b), b))
print('type(c) = {}, c = {}'.format(type(c), c))<jupyter_output>type(a) = <class 'int'>, a = 1
type(b) = <class 'float'>, b = 0.2
type(c) = <class 'str'>, c = Hello<jupyter_text>2.2 元组：元组用来存储一族数据，定义以后不可修改<jupyter_code>tupleA = (1, 2, 3, 4)
tupleA = (1, 2, 3, 'apple')
print(tupleA[0])
print(tupleA[0:2])
tupleA[0] = 1<jupyter_output><empty_output><jupyter_text>2.3 列表：列表用来存储一族数据，可以修改，是一种最常用的结构<jupyter_code>listA = [1, 2, 3, 4]
print('listA中第一个元素为:%s' % (listA[0]))
print('listA中前两个元素为:%s' % (listA[0:2]))

listA[0] = 'apple'
listA<jupyter_output>listA中第一个元素为:1
listA中前两个元素为:[1, 2]<jupyter_text>2.4 集合：用来存储一族互不相同的数据，可以做集合运算+ ``|``: 并集+ ``&``: 交集+ ``-``: 差集<jupyter_code>a = {1, 2}
b = {2, 3}

print('a和b的并集为：{}'.format(a | b))
print('a和b的交集为：{}'.format(a & b))
print('a和b的差集为：{}'.format(a - b))<jupyter_output>a和b的并集为：{1, 2, 3}
a和b的交集为：{2}
a和b的差集为：{1}<jupyter_text>2.5 字典：用于保存键值对，实现"switch"的功能<jupyter_code>data = {'name':'马旭辉', 'gender':'男'}
print(data['name'])<jupyter_output>马旭辉<jupyter_text>3. 扩展包 3.1 安装扩展包```pip install numpypip3 install numpyconda install numpy``` 3.2 环境管理由于python中各种扩展包之间存在依赖关系强烈建议使用conda进行环境管理，开发环境可以安装anaconda会自带这些工具+ 新建一个环境``conda create -n myenv_name python = 3.5``+ 启动一个环境``activate myenv_name(windows);source activate myenv_name(linux)`` 3.3 导入扩展包<jupyter_code>import time
import numpy as np

print(time.asctime())
print(np.array([1, 2, 3, 4]))<jupyter_output>Fri Oct 27 21:50:49 2017
[1 2 3 4]<jupyter_text>4. 条件控制```if condition_1:    statement_block_1elif condition_2:    statement_block_2else:    statement_block_3```<jupyter_code>a = 5

if a>3:
    print('a > 3')
elif a==3:
    print('a == 3')
else:
    print('a < 3')
a = 100
if a:
    print('a != 0')
else:
    print('a == 0')<jupyter_output>a != 0<jupyter_text>5. 循环语句+ for+ while  5.1 while```while condition:    do ...```<jupyter_code>count = 0

while count<10:
    count += 1
    print(count)<jupyter_output>1
2
3
4
5
6
7
8
9
10<jupyter_text>5.2 for：用于循环指定次数或遍历列表或元组等<jupyter_code>a = ['R', 'Python', 'Java', 'Ruby', 'Javascript']

# 一个非常不好的用法

# for i in range(len(a)):
#     print(a[i])

# 应该这样写

for item in a:
    print(item)
    
# 或许你需要用到索引

for i,item in enumerate(a):
    print('%d : %s' % (i, item))
for i in range(5):
    print(i)<jupyter_output>0
1
2
3
4<jupyter_text>6. 函数```def 函数名(参数列表):    函数体```<jupyter_code>def myadd(a, b):
    result = a + b
    return result

print(myadd(1, 2))<jupyter_output>3
{'fraction_non_alphanumeric': 0.20646700238536972, 'fraction_numerical': 0.03896103896103896, 'mean_word_length': 5.739285714285714, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '267986', 'license': 'MIT', 'n_tokens_mistral': 2053, 'n_tokens_neox': 2058, 'n_words': 326, 'path': 'python_basic.ipynb', 'repo_name': 'jeffmxh/Python_Notes', 'size': '3773'}