基础教程介绍了基本概念,特别是对象和类。
进阶教程对基础教程的进一步拓展,说明Python的细节。希望在进阶教程之后,你对Python有一个更全面的认识。
之前我们说了,列表是Python里的一个类。一个特定的表,比如说nl = [1,3,8],就是这个类的一个对象。我们可以调用这个对象的一些方法,比如 nl.append(15)。
我们要介绍一个新的类,词典 (dictionary)。与列表相似,词典也可以储存多个元素。这种储存多个元素的对象称为容器(container)。
基本概念
常见的创建词典的方法:
>>>dic = {'tom':11, 'sam':57,'lily':100}
>>>print type(dic)
词典和表类似的地方,是包含有多个元素,每个元素以逗号分隔。但词典的元素包含有两部分,键和值,常见的是以字符串来表示键,也可以使用数字或者真值来表示键(不可变的对象可以作为键)。值可以是任意对象。键和值两者一一对应。
比如上面的例子中,‘tom'对应11,'sam对应57,'lily'对应100
与表不同的是,词典的元素没有顺序。你不能通过下标引用元素。词典是通过键来引用。
>>>print dic['tom']
>>>dic['tom'] = 30
>>>print dic
构建一个新的空的词典:
>>>dic = {}
>>>print dic
在词典中增添一个新元素的方法:
>>>dic['lilei'] = 99
>>>print dic
这里,我们引用一个新的键,并赋予它对应的值。
词典元素的循环调用
dic = {'lilei': 90, 'lily': 100, 'sam': 57, 'tom': 90}
for key in dic:
print dic[key]
在循环中,dict的每个键,被提取出来,赋予给key变量。
通过print的结果,我们可以再次确认,dic中的元素是没有顺序的。
词典的常用方法
>>>print dic.keys() # 返回dic所有的键
>>>print dic.values() # 返回dic所有的值
>>>print dic.items() # 返回dic所有的元素(键值对)
>>>dic.clear() # 清空dic,dict变为{}
另外有一个很常用的用法:
>>>del dic['tom'] # 删除 dic 的‘tom'元素
del是Python中保留的关键字,用于删除对象。
与表类似,你可以用len()查询词典中的元素总数。
>>>print(len(dic))
总结
词典的每个元素是键值对。元素没有顺序。
dic = {'tom':11, 'sam':57,'lily':100}
dic['tom'] = 99
for key in dic: ...
del, len()

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.