# 变量¶

In [1]:
# 数值示例
x = 5
print (x)

5

In [2]:
# 字符串示例
x = "hello"
print (x)

hello

In [3]:
# 变量可以相互使用
a = 1
b = 2
c = a + b
print (c)

3


In [4]:
# int 变量
x = 5
print (x)
print (type(x))

# float 变量
x = 5.0
print (x)
print (type(x))

# text 变量（即为str）
x = "5"
print (x)
print (type(x))

# boolean 变量
x = True
print (x)
print (type(x))

5
<class 'int'>
5.0
<class 'float'>
5
<class 'str'>
True
<class 'bool'>


In [5]:
# int 变量
a = 5
b = 3
print (a + b)

# string 变量
a = "5"
b = "3"
print (a + b)

8
53


# 列表¶

In [6]:
# 创建列表
list_x = [3, "hello", 1]
print (list_x)

[3, 'hello', 1]

In [7]:
# 向列表中添加元素
list_x.append(7)
print (list_x)

[3, 'hello', 1, 7]

In [8]:
# 访问列表中特定位置的元素
print ("list_x[0]: ", list_x[0])
print ("list_x[1]: ", list_x[1])
print ("list_x[2]: ", list_x[2])
print ("list_x[-1]: ", list_x[-1]) # 索引值为-1时，表示访问最后的元素
print ("list_x[-2]: ", list_x[-2]) # 索引值为-2时，表示访问倒数第二个元素

list_x[0]:  3
list_x[1]:  hello
list_x[2]:  1
list_x[-1]:  7
list_x[-2]:  1

In [9]:
# 切片
print ("list_x[:]: ", list_x[:])
print ("list_x[2:]: ", list_x[2:])
print ("list_x[1:3]: ", list_x[1:3])
print ("list_x[:-1]: ", list_x[:-1])

list_x[:]:  [3, 'hello', 1, 7]
list_x[2:]:  [1, 7]
list_x[1:3]:  ['hello', 1]
list_x[:-1]:  [3, 'hello', 1]

In [10]:
# 列表的长度
len(list_x)

Out[10]:
4
In [11]:
# 替换列表中的元素
list_x[1] = "hi"
print (list_x)

[3, 'hi', 1, 7]

In [12]:
# 组合列表
list_y = [2.4, "world"]
list_z = list_x + list_y
print (list_z)

[3, 'hi', 1, 7, 2.4, 'world']


# 元组¶

In [13]:
# 创建元组
tuple_x = (3.0, "hello")
print (tuple_x)

(3.0, 'hello')

In [14]:
# 添加元素到元组
tuple_x = tuple_x + (5.6,)
print (tuple_x)

(3.0, 'hello', 5.6)

In [15]:
# 试图改变元组的值（事实上不可行）
tuple_x[1] = "world"

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-86abc19d8025> in <module>()
----> 1 tuple_x[1] = "world"

TypeError: 'tuple' object does not support item assignment

# 字典¶

In [0]:
# 创建字典
goku = {"name": "Goku",
"eye_color": "brown"}
print (goku)
print (goku["name"])
print (goku["eye_color"])

In [0]:
# 改变键的值
goku["eye_color"] = "green"
print (goku)

In [0]:
# 添加新的键值对
goku["age"] = 24
print (goku)

In [0]:
# 字典的长度
print (len(goku))


# If 语句¶

In [0]:
# If 语句
x = 4
if x < 1:
score = "low"
elif x <= 4:
score = "medium"
else:
score = "high"
print (score)

In [0]:
# 带有布尔值的if语句
x = True
if x:
print ("it worked")


# 循环语句¶

In [0]:
# for循环
x = 1
for i in range(3): # 从 i=0 到 i=2
x += 1 # 等价于 x = x + 1
print ("i={0}, x={1}".format(i, x)) # 打印多个变量

In [0]:
# 循环遍历列表中的元素
x = 1
for i in [0, 1, 2]:
x += 1
print ("i={0}, x={1}".format(i, x))

In [0]:
# while循环
x = 3
while x > 0:
x -= 1 # 等价于 x = x - 1
print (x)


# 函数¶

In [0]:
# 创建函数
x += 2
return x

# 调用函数
score = 0
print (score)

In [0]:
# 具有多输入的函数
def join_name(first_name, last_name):
joined_name = first_name + " " + last_name
return joined_name

# 调用函数
first_name = "Goku"
last_name = "Mohandas"
joined_name = join_name(first_name=first_name, last_name=last_name)
print (joined_name)


# 类¶

In [0]:
# 创建函数
class Pets(object):

# 类初始化
def __init__(self, species, color, name):
self.species = species
self.color = color
self.name = name

# 用于打印
def __str__(self):
return "{0} {1} named {2}.".format(self.color, self.species, self.name)

# 示例函数
def change_name(self, new_name):
self.name = new_name

In [0]:
# 创建类的实例：对象
my_dog = Pets(species="dog", color="orange", name="Guiness",)
print (my_dog)
print (my_dog.name)

In [0]:
# 调用类的函数
my_dog.change_name(new_name="Charlie")
print (my_dog)
print (my_dog.name)


# 其它资源¶

In [0]: