# Python数据分析笔记之Python语法基础

XAMPP案例 771浏览

• 缩进
• 对象
• 注释
• 函数和对象方法调用
• 变量和参数传递
• 动态引用
• 属性和方法
• 引入import
• 二元运算符
• 可变与不可变对象
• 数值类型
• 字符串
• 布尔值
• 类型转换
• 控制流
• 三元表达式

Python语法基础

Python的语言设计强调的是可读性，简洁和清晰

Python使用空白字符(tab和空格)来组织代码，而不是像其他语言，比如R，C++，JAVA和Perl那样使用括号。看一个排序算法的for循环：

``````for x in array:
``````    if x in pivot:
``````        less.append(x)
``````    else:
````        greater.append(x)````

python的语句不需要用分号结尾。但是分号可以用来给同一行的语句切分：

``a = 5; b = 6; c = 7``

``````# a = 1
``````# for i in range(10):
````b = 2````

``````result = f(x, y, z)
````g()````

``obj.some_method(x, y, z)``
obj是对象，some_method是这个对象的方法

``result = f(a, b, c, d=5, e='foo')``
a,b,c是位置参数，d,e是关键字参数

``a = [1, 2, 3]``

``b = a``

``a.append(4)``

``````def append_element(some_list, element):
````    some_list.append(element)#将一个元素添加到some_list中````
``````In [1]: data = [1, 2, 3]
``````
``````In [2]: append_element(data, 4)
``````
``````In [3]: data
````Out[3]: [1, 2, 3, 4]````

``````In [1]: a = 5
``````
``````In [2]: type(a)#查看数据类型
``````Out[2]: int
``````
``````In [3]: a = 'foo'
``````
``````In [4]: type(a)
````Out[4]: str````

Python的对象通常都有属性(存储在对象内部的对象)和方法(可以访问对象内部数据的附属函数)。可以用obj.attribute_name访问属性和方法

``````#some_module.py
``````PI = 3.14159
``````
``````def f(x):
``````    return x + 2
``````
``````def g(a, b):
````    return a + b````

``````import some_module
``````result = some_module.f(5)
````pi = some_module.PI````

``````from some_module import f, g, PI
````result = g(5, PI)````

``````import some_module as sm
``````from some_module import PI as pi, g as gf
````import pandas as pd````

Python中大多数对象，比如列表，字典，Numpy数组和用户定义的类型都是可变的。其他的，例如字符串和元组，不可变

``a_tuple = (3, 5, (4, 5))#元组``

``fval = 7.243``fval2 = 6.78e-5``

``3 / 2``

``3 // 2``

Python中的字符串比C语言中简单且灵活许多，可以用单引号或双引号来写字符串(这也和C语言不一样)

``````a = 'one way of writing a string'
````b = "another way"````

``````c = """
``````    This is a longer string
``````    that
``````    spans
````    """  ````

``````In [1]: a = 'I really'
``````
``````In [2]: b = 'really like'
``````
``````In [3]: c = 'you'
``````
``````In [4]: a + b + c
````Out[4]: 'I really really like you'````

``````In [1]: '{0:s} 价值 {1：d} rmb'.format('iPhone12', 5000)
````Out[1]: 'iPhone12 价值 5000 rmb‘````

{0:s}表示第1个参数是字符串，{1:d}表示第2个参数是整数。大致说一下，后面还会遇到。

str,bool,int和float也可以用来类型转换

``````In [1]: s = '3.14159'
``````
``````In [2]: fval = float(a)
``````
``````In [3]: type(fval)
``````Out[3]: float
``````
``````In [4]: int(fval)
``````Out[4]: 3
``````
``````In [5]: bool(fval)
````Out[5]: True````

if, elif 和else

if语句检查条件，满足则执行后面语句

``````if x < 0:
````    print('negative)````

if后面可以加elif,所有条件不满足最后可以加else

``````if x < 0:
``````    print('negative')
``````elif x == 0:
``````    print('0')
``````else:
````    print('>0')````

for循环

for 循环在一个集合中迭代

``````sequence = [1, 2, None, 4, None, 5]
``````for value in sequence:
``````    if value is None:
````        continue````

``````sequence = [1, 2, None, 4, None, 5]
``````for value in sequence:
``````    if value is None:
````        break````

while循环

while循环指定了条件和代码，当条件为False时退出循环

``````total = 0
``````while total > 100:
````    total += 1   ````

pass

``````if x > 0:
````    pass````

range

range函数返回一个迭代器，产生均匀分布的整数序列

``````In [1]: list(range(10))
````Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]````

range三个参数(起点,终点,步伐大小)

``````In [1]: list(range(0, 20, 2)
````Out[1]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]````
``````In [2]: list(20, 0, -2)
````Out[2]: [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]````

Python中的三元表达式可以将if-else语句放到一行里

``````x = 5
````a = 'positive' if x > 0 else 'negative'````