内容简介:Python 入门:From Zero to Hero(上)
Python 最近很火,有很多人正在或者计划投身 Python 学习,所以我也打算就这个话题分享一些学习资源或者教程之类的 干货 。
在前面写过的 文章 人生苦短,何不学 Python 中,我简单介绍了一下 Python 的重要性以及它的学习方法。
那么今天上点干货,讲一讲 Python 入门学习的若干知识点。
在 medium 上看到一篇介绍 Python 入门学习的英文热文 Learning Python:from zero to hero, 感觉写的非常好,尤其是对于之前从来没接触过 Python 的人来说,有很好的科普性,让人看完之后立刻会对 Python 有总体性的认识。
思来想去,我还是想把它翻译出来,分享给众多的中文读者。
说明一下,我不会翻译的那么生硬,会按照自己的理解重新组织语言,让它变得更好懂。另外,原文中一些不太重要的东西已做省略,只保留干货部分 。
注意,这不是 Python 的详尽教程,但却涵盖了 Python 语言比较核心的内容。
全文比较长,分两次发出来。
1
基本概念
变量
在 Python 中定义变量和给变量赋值非常简单,就像下面这样:
one = 1
定义了一个变量 one, 并且把它的值赋为 1。
按照这种方法,你可以随意定义一个变量,并为它赋上你想要的值,如下面:
two = 2 some_number = 10000
除了整数,你还可以用 booleans (True / False), strings, float 等数据类型来定义变量,如:
# booleans true_boolean = True false_boolean = False # string my_name = "Leandro Tk" # float book_price = 15.80
流程控制
跟其他编程语言一样,Python 使用 if 进行条件判断和流程控制,如:
if True:
print("Hello Python If")
if 2 > 1:
print("2 is greater than 1")
使用 else 的情况:
if 1 > 2:
print("1 is greater than 2")
else:
print("1 is not greater than 2")
有多个条件分支时,可以使用 elif:
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")
循环与迭代
Python 中循环主要有两种方式: while 和 for 。
while 循环:
num = 1
while num <= 10:
print(num)
num += 1
当 while 后面的表达式为 true 时,执行后面的代码块,上面这段代码将打印 1-10。
另一个简单的例子 。
loop_condition = True
while loop_condition:
print("Loop Condition keeps: %s" %(loop_condition))
loop_condition = False
首次执行 时,loop_condition 为 true,while 循环体执行之后, loop_condition 变为 false,while 循环退出。
for 循环:
与 while 循环相同的功能,这段代码也是打印 1-10。
for i in range(1, 11): print(i)
2
列表 List
我们知道,当存储单个值时,用单个变量来表示,可要是想要存储一连串的值的时候,怎么办?
用 List。
有点类似于 C 语言中的数组,List 使用方法如下:
my_integers = [1, 2, 3, 4, 5]
根据 index 可以获取 List 中的元素,index 是从 0 开始的整数。
my_integers = [5, 7, 1, 3, 4] print(my_integers[0]) # 5 print(my_integers[1]) # 7 print(my_integers[4]) # 4
List 不仅用来存储整数,也可以用来存储字符串。
relatives_names = [ "Toshiaki", "Juliana", "Yuji", "Bruno", "Kaio" ] print(relatives_names[4]) # Kaio
前面介绍了根据 index 获取 List 元素的值,下面讲下如何向 List 中添加元素。
最常使用的是 append 方法,用法如下:
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 的使用非常简单,它的参数就是要添加的元素。
3
字典 Dictionary
前面讲过,List 的 index 是从 0 开始的整数,但是如果 index 不是整数怎么办呢?比如是字符串或者其他数据类型。
Dictionary 隆重登场。
Dictionary 是 key-value 对的集合,它的用法如下:
dictionary_example = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
这里 key 你可以看作是 List 中的 index, 它所对应的值是 value。
那么怎么获取 Dictionary 中的元素呢?同样是用 index,只不过这里的 index 不再是整数,而是它的 key。
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
print("And by the way I'm %s" %(dictionary_tk["nationality"])) # And by the way I'm Brazilian
上面创建了一个字典,key 分别是 name,nickname,nationality 。
另外,value 可以是任意类型,比如,我给字典新添加一个 key "age", 它对应的 value 为整数 24 。
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian",
"age": 24
}
print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
print("And by the way I'm %i and %s" %(dictionary_tk["age"], dictionary_tk["nationality"])) # And by the way I'm 24 and Brazilian
现在学习下如何向 Dictionary 添加元素。
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
dictionary_tk["age"] = 24
print(dictionary_tk) # { 'name': 'Leandro', 'nickname': 'Tk','nationality': 'Brazilian', 'age': 24}
就像上面这样,为指定的 key 添加一个 value 就可以了。
4
迭代 Iteration
迭代,通俗点说就是用 for 循环来遍历数据结构。
不同于 C 语言中用 for 加 index 的方法来完成迭代,Python 用 for...in 做迭代。
下面是个例子:
bookshelf = [
"The Effective Engineer",
"The 4 hours work week",
"Zero to One",
"Lean Startup",
"Hooked"
]
for book in bookshelf:
print(book)
就是把书架上的每一本书(书名)打印出来。这种 for...in 的用法非常的简单与直观。
上面的例子是 List,而对于 hash 类型的数据结构如 Dictionary,同样可以用此方法来做迭代,它使用 key 做参数:
dictionary = { "some_key": "some_value" }
for key in dictionary:
print("%s --> %s" %(key, dictionary[key]))
# some_key --> some_value
它完成的功能是把字典中的每一个 key 及其所对应的 value 打印出来。
除此之外,还有另一种实现相同功能的方式,那就是使 用 items 方法:
dictionary = { "some_key": "some_value" }
for key, value in dictionary.items():
print("%s --> %s" %(key, value))
# some_key --> some_value
这里,我们用了 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))
# My name is Leandro
# My nickname is Tk
# My nationality is Brazilian
# My age is 24
你看,这里的参数用的是 attribute, 照样可以工作。
(未完待续)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- TiDB入门(四):从入门到“跑路”
- MyBatis从入门到精通(一):MyBatis入门
- MyBatis从入门到精通(一):MyBatis入门
- Docker入门(一)用hello world入门docker
- 赵童鞋带你入门PHP(六) ThinkPHP框架入门
- 初学者入门 Golang 的学习型项目,go入门项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Computation and Programming Using Python
John V. Guttag / The MIT Press / 2013-7 / USD 25.00
This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!