### 基础知识

#### 1. 变量

``````one = 1
``````

``````two = 2
some_number = 10000
``````

``````# booleans
true_boolean = True
false_boolean = False

# string
my_name = "Leandro Tk"
``````

#### 2. 控制语句：条件判断

"If"条件语句检验条件真假。如果条件为真，执行if后面的条件。举个例子：

``````if True:
print("Hello Python If")
``````

2比1大，条件为真，所以执行print后面的程序。

``````if 1 > 2:
print("1 is greater than 2")
else:
print("1 is not greater than 2")
``````

1小于2，条件为假，所以执行else后面的语句。

``````if 1 > 2:
print("1 is greater than 2")
elif 2 > 1:
print("1 is not greater than 2")
else:
print("1 is equal to 2")
``````

#### 3. 循环/迭代

Python有很多不同的循环/迭代模式。今天说两个：while循环和for循环。

while循环：当条件为真，while模块语句执行。我们可以用while循环来输出1-10这几个数字。

``````num = 1
``````

while循环需要循环条件。如果循环条件为真，循环继续。上述代码表明，num值为11时，循环条件为假，循环停止。

``````loop_condition = True
``````

for循环：模块语句运用"num"变量时，for循环迭代变量。for循环代码可以跟while循环一样输出数字1-10。

``````for i in range(1, 11):
print(i)
``````

### 列表（List）：集合（Collection）|数组（Array）|数据结构（Data Structure）

``````my_integers = [1, 2, 3, 4, 5]
``````

Python句法比较容易理解：

``````my_integers = [5, 7, 1, 3, 4]
print(my_integers[0]) # 5
print(my_integers[1]) # 7
print(my_integers[4]) # 4
``````

``````relatives_names = [
"Toshiaki",
"Juliana",
"Yuji",
"Bruno",
"Kaio"
]
``````

``````bookshelf = []
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0]) # The Effective Engineer
print(bookshelf[1]) # The 4 Hour Work Week
``````

append函数特别简单，只需要将元素添加为append的参数（正如"The Effective Engineer"）。

### 字典（Dictioary）：键-值(Key-Value)数据结构

``````dictionary_example = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
``````

``````dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
``````

``````dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian",
"age": 24
}
``````

``````dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
dictionary_tk['age'] = 24
``````

### 迭代：数据结构中的循环

``````bookshelf = [
"The Effective Engineer",
"The 4-hour Workweek",
"Zero to One",
"Lean Startup",
"Hooked"
]
``````

``````dictionary = { "some_key": "some_value" }
for key in dictionary:
print("%s --> %s" %(key, dictionary[key]))
``````

``````dictionary = { "some_key": "some_value" }
for key, value in dictionary.items():
print("%s --> %s" %(key, value))
``````

``````dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian",
"age": 24
}
for attribute, value in dictionary_tk.items():
print("My %s is %s" %(attribute, value))
``````

### 类和对象

#### Python面向对象的程序设计模式：ON

``````class Vehicle:
pass
``````

``````car = Vehicle()
print(car) # <main.Vehicle instance at 0x7fb1de6c2638>
``````

``````class Vehicle:
def init(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
``````

``````tesla_model_s = Vehicle(4, 'electric', 5, 250)
``````

``````class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity

def number_of_wheels(self):
return self.number_of_wheels

def set_number_of_wheels(self, number):
self.number_of_wheels = number``````

``````class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity

def make_noise(self):
print('VRUUUUUUUM')``````

### 封装：隐藏信息

_维基百科上说：“封装可以用来隐藏数据成员和成员函数。封装意味着对象2的内部实现通常被隐藏，并由定义替代。”

#### 公共实例变量

``````class Person:
def init(self, first_name):
self.first_name = first_name
``````

``````tk = Person('TK')
print(tk.first_name) # => TK
``````

``````class Person:
first_name = 'TK'
``````

``````tk = Person()
print(tk.first_name) # => TK
``````

``````tk = Person('TK')
tk.first_name = 'Kaio'
print(tk.first_name) # => Kaio
``````

#### 非公共实例变量

We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work). —  PEP 8因为Python（没有一大堆不必要的工作时）当中没有一个变量完全私有不公开，所以我们这里使用了非公共(non-public)这个词，而没用使用私有(private)这个词。PEP 8

`class Person: def __init``(self, first_name, email): self.first_name = first_name self._email = email_`

`tk = Person('TK', 'tk@mail.com') print(tk._email) # [[tk@mail.com][20]][5]_`

``````class Person:
def __init__(self, first_name, email):
self.first_name = first_name
self._email = email

def update_email(self, new_email):
self._email = new_email

def email(self):
return self._email``````

``````tk = Person('TK', 'tk@mail.com')
print(tk.email()) # => tk@mail.com
# tk._email = 'new_tk@mail.com' -- treat as a non-public part of the class API
print(tk.email()) # => tk@mail.com
tk.update_email('new_tk@mail.com')
print(tk.email()) # => new_tk@mail.com``````
1. 我们引入了一个新对象，包括"first_name"和"email"tk@mail.com
2. 我们用一种方法得到了非公共变量，输出了"email"。
3. 我们试图在类之外设置一个新的"email"。
4. 我们需要将非公共变量视作API的非公共部分。
5. 运用实例方法更新了非公共变量。
6. 大功告成！可运用助手方法(the helper method)在类中更新。

#### 公共方法

``````class Person:
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age

def show_age(self):
return self._age``````

#### 非公共方法

``````class Person:
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age

def _show_age(self):
return self._age``````

``````class Person:
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age

def show_age(self):
return self._get_age()

def _get_age(self):
return self._age

tk = Person('TK', 25)
print(tk.show_age()) # => 25``````

### 继承：行为和特点

__`class Car: def __init``(self, number_of_wheels, seating_capacity, maximum_velocity): self.number_of_wheels = number_of_wheels self.seating_capacity = seating_capacity self.maximum_velocity = maximum_velocity`_

`my_car = Car(4, 5, 250) print(my_car.number_of_wheels) print(my_car.seating_capacity) print(my_car.maximum_velocity)`

Python中，我们将父类(parent class)作为参数应用到子类(child class)中，好比“电动汽车”类可以从“车类”继承行为和特点。

`class ElectricCar(Car): def **init**(self, number_of_wheels, seating_capacity, maximum_velocity): Car.**init**(self, number_of_wheels, seating_capacity, maximum_velocity)`

`my_electric_car = ElectricCar(4, 5, 250) print(my_electric_car.number_of_wheels) # => 4 print(my_electric_car.seating_capacity) # => 5 print(my_electric_car.maximum_velocity) # => 250`

### 大概就这么多。

• 变量如何工作
• 条件语句
• while循环 & for循环
• 列表：集合|数组
• 字典键-值集合
• 数据结构如何迭代
• 对象和类
• 属性作为对象数据
• 方法作为对象行为
• 如何运用Python中的getters setters和property decorator
• 封装：隐藏信息
• 继承：行为和特点