Python3 教程

Python 3 教程

Python 3 - 新功能

Python 3 - 概述

Python 3 - 环境设置

Python 3 - 基本语法

Python 3 - 变量类型

Python 3 - 运算符

Python 3 - 决策

Python 3 - IF 语句

Python 3 - IF...ELIF...ELSE 语句

Python 3 - 嵌套 IF 语句

Python 3 - 循环

Python 3 - 嵌套循环

Python 3 - break 语句

Python 3 - continue 声明

Python 3 - pass 语句

Python 3 - 数字

Python 3 - 字符串

Python 3 - 列表(List)

Python 3 - Tuples(元组)

Python 3 - 字典(Dictionary)

Python 3 - 日期 & 时间

Python 3 - 函数

Python 3 - 模块

Python 3 - 文件 I/O

Python 3 - 异常处理

Python 3 - 面向对象

Python 3 - 正则表达式

Python 3 - CGI 编程

Python 3 - MySQL 数据库访问

Python 3 - 网络编程

Python 3 - 使用 SMTP 发送电子邮件

Python 3 - 多线程编程

Python 3 - XML 处理

Python 3 - GUI 编程 (Tkinter)

Python 3 - 使用 C 的扩展编程

Python3 参考

Python 3 - 数字 abs() 方法

Python 3 - 数字 ceil() 方法

Python 3 - 数字 exp() 方法

Python 3 - 数字 fabs() 方法

Python 3 - 数字 floor() 方法

Python 3 - 数字 log() 方法

Python 3 - 数字 log10() 方法

Python 3 - 数字 max() 方法

Python 3 - 数字 min() 方法

Python 3 - modf() 方法

Python 3 - 数字 pow() 方法

Python 3 - 数字 round() 方法

Python 3 - 数字 sqrt() 方法

Python 3 - 数字 choice() 方法

Python 3 - 数字 randrange() 方法

Python 数字 random() 方法

Python 3 - 数字 seed() 方法

Python 3 - 数字 shuffle() 方法

Python 3 - 数字 uniform() 方法

Python 3 - 数字 acos() 方法

Python 3 - 数字 asin() 方法

Python 3 - 数字 atan() 方法

Python 3 - 数字 atan2() 方法

Python 3 - 数字 cos() 方法

Python 3 - 数字 hypot() 方法

Python 3 - 数字 sin() 方法

Python 3 - 数字 tan() 方法

Python 3 - 数字 degrees() 方法

Python 3 - 数字 radians() 方法

Python 3 - 字符串 capitalize() 方法

Python 3 - 字符串 center() 方法

Python 3 - 字符串 count() 方法

Python 3 - 字符串 decode() 方法

Python 3 - 字符串 encode() 方法

Python 3 - 字符串 endswith() 方法

Python 3 - 字符串 expandtabs() 方法

Python 3 - 字符串 find() 方法

Python 3 - 字符串 index() 方法

Python 3 - 字符串 isalnum() 方法

Python 3 - 字符串 isalpha() 方法

Python 3 - 字符串 isdigit() 方法

Python 3 - 字符串 islower() 方法

Python 3 - 字符串 isnumeric() 方法

Python 3 - 字符串 isspace() 方法

Python 3 - 字符串 istitle() 方法

Python 3 - 字符串 isupper() 方法

Python 3 - 字符串 join() 方法

Python 3 - 字符串 len() 方法

Python 3 - 字符串 ljust() 方法

Python 3 - String lower() 方法

Python 3 - 字符串 lstrip() 方法

Python 3 - 字符串 maketrans() 方法

Python 3 - 字符串 max() 方法

Python 3 - 字符串 min() 方法

Python 3 - 字符串 replace() 方法

Python 3 - 字符串 rfind() 方法

Python 3 - 字符串 rindex() 方法

Python 3 - 字符串 rjust() 方法

Python 3 - 字符串 rstrip() 方法

Python 3 - 字符串 split() 方法

Python 3 - 字符串 splitlines() 方法

Python 3 - 字符串 startswith() 方法

Python 3 - 字符串 strip() 方法

Python 3 - 字符串swapcase() 方法

Python 3 - 字符串 title() 方法

Python 3 字符串 translate() 方法

Python 3 - 字符串 upper() 方法

Python 3 - 字符串 zfill() 方法

Python 3 - 字符串 isdecimal() 方法

Python 3 - 列表 len() 方法

Python 3 - 列表 max() 方法

Python 3 - 列表 min() 方法

Python 3 - 列表 list() 方法

Python 3 - 列表 append() 方法

Python 3 - 列表 count() 方法

Python 3 - 列表 extend() 方法

Python 3 - 列表 index() 方法

Python 3 - 列表 insert() 方法

Python 3 - 列表 pop() 方法

Python 3 - 列表 remove() 方法

Python 3 - 列表 reverse() 方法

Python 3 - 列表 sort() 方法

Python 3 - 元组 len() 方法

Python 3 - 元组 max() 方法

Python 3 - 元组 min() 方法

Python 3 - 元组 tuple() 方法

Python 3 - 字典 cmp() 方法

Python 3 - 字典 len() 方法

Python 3 - 字典 str() 方法

Python 3 - 字典 type() 方法

Python 3 - 字典 clear() 方法

Python 3 - 字典 copy() 方法

Python 3 - 字典的 fromkeys() 方法

Python 3 - 字典 has_key() 方法

Python 3 - 字典 items() 方法

Python 3 - 字典 keys() 方法

Python 3 - 字典 setdefault() 方法

Python 3 - 字典 update() 方法

Python 3 - 字典 values() 方法

Python 3 - 时间 altzone() 方法

Python 3 - 时间 asctime() 方法

Python 3 - 时间 clock() 方法

Python 3 - 时间 ctime() 方法

Python 3 - 时间 gmtime() 方法

Python 3 - 时间 localtime() 方法

Python 3 - 时间 mktime() 方法

Python 3 - time sleep() 方法

Python 3 - 时间 strftime() 方法

Python 3 - 时间 strptime() 方法

Python 3 - 时间 time() 方法

Python 3 - 时间 tzset() 方法

Python 3 - 文件方法

Python 3 - 操作系统文件/目录方法

Python 3 - 文件 close() 方法

Python 3 - 文件 flush() 方法

Python 3 - 文件 fileno() 方法

Python 3 - 文件 isatty() 方法

Python 3 - 文件 next() 方法

Python 3 - 文件 read() 方法

python-3-文件-readline-方法

Python 3 - 文件 readlines() 方法

Python 3 - 文件 seek() 方法

Python 3 - 文件 tell() 方法

Python 3 - 文件 truncate() 方法

Python 3 - 文件 write() 方法

Python 3 - 文件 writelines() 方法

Python 3 - os.access() 方法

Python 3 - os.chdir() 方法

Python 3 - os.chflags() 方法

Python 3 - os.chmod() 方法

Python 3 - os.chown() 方法

Python 3 - os.chroot() 方法

Python 3 - os.close() 方法

Python 3 - os.closerange() 方法

Python 3 - os.dup() 方法

Python 3 - os.dup2() 方法

Python 3 - os.fchdir() 方法

Python 3 - os.fchmod() 方法

Python 3 - os.fchown() 方法

Python 3 - os.fdatasync() 方法

Python 3 - os.fdopen() 方法

Python 3 - os.fpathconf() 方法

Python 3 - os.fstat() 方法

Python 3 - os.fstatvfs() 方法

Python 3 - os.fsync() 方法

Python 3 - os.ftruncate() 方法

Python 3 - os.getcwd() 方法

Python 3 - os.getcwdu() 方法

Python 3 - os.isatty() 方法

Python 3 - os.lchflags() 方法

Python 3 - os.lchmod() 方法

Python 3 - os.lchown() 方法

Python 3 - os.link() 方法

Python 3 - os.listdir() 方法

Python 3 - os.lseek() 方法

Python 3 - os.lstat() 方法

Python 3 - os.major() 方法

Python 3 - os.makedev() 方法

Python 3 - os.makedirs() 方法

Python 3 - os.minor() 方法

Python 3 - os.mkdir() 方法

Python 3 - os.mkfifo() 方法

Python 3 - os.mknod() 方法

Python 3 - os.open() 方法

Python 3 - os.openpty() 方法

Python 3 - os.pathconf() 方法

Python 3 - os.pipe() 方法

Python 3 - os.popen() 方法

Python 3 - os.read() 方法

Python 3 - os.readlink() 方法

Python 3 - os.remove() 方法

Python 3 - os.removedirs() 方法

Python 3 - os.rename() 方法

Python 3 - os.renames() 方法

Python 3 - os.rmdir() 方法

Python 3 - os.stat() 方法

Python 3 - os.stat_float_times() 方法

Python 3 - os.statvfs() 方法

Python 3 - os.symlink() 方法

Python 3 - os.tcgetpgrp() 方法

Python 3 - os.tcsetpgrp() 方法

Python 3 - os.tempnam() 方法

Python 3 - os.tmpnam() 方法

Python 3 - os.ttyname() 方法

Python 3 - os.unlink() 方法

Python 3 - os.utime() 方法

Python 3 - os.walk() 方法

Python 3 - os.write() 方法

Python 3 - Tkinter 按钮

Python 3 - Tkinter 画布

Python 3 - Tkinter 复选按钮

Python 3 - Tkinter 入口

Python 3 - Tkinter 框架

Python 3 - Tkinter 标签

Python 3 - Tkinter 列表框

Python 3 - Tkinter 菜单按钮

Python 3 - Tkinter 菜单

Python 3 - Tkinter 消息

Python 3 - Tkinter 单选按钮

Python 3 - Tkinter 比例尺

Python 3 - Tkinter 滚动条

Python 3 - Tkinter 文本

Python 3 - Tkinter 顶层

Python 3 - Tkinter 自旋盒

Python 3 - Tkinter PanedWindow

Python 3 - Tkinter 标签框架

Python 3 - Tkinter tkMessageBox

Python 3 - Tkinter 维度

Python 3 - Tkinter 颜色

Python3 - Tkinter 字体

Python 3 - Tkinter 锚点

Python 3 - Tkinter 浮雕样式

Python 3 - Tkinter 位图

Python 3 - Tkinter 游标

Python 3 - Tkinter pack() 方法

Python Tkinter grid() 方法

Python 3 - Tkinter place() 方法

上一节: Python 3 - 字符串

上一节

下一节: Python 3 - Tuples(元组)

下一节

Python 3 - 列表(List)

简述

Python中最基本的数据结构是sequence. 序列的每个元素都分配有一个数字 - 它的位置或索引。第一个索引为零,第二个索引为一,依此类推。

Python 有六种内置类型的序列,但最常见的是列表和元组,我们将在本教程中看到它们。

您可以对所有序列类型执行某些操作。这些操作包括索引、切片、添加、乘法和检查成员资格。此外,Python 具有用于查找序列长度以及查找其最大和最小元素的内置函数。

Python 列表

列表是 Python 中可用的最通用的数据类型,它可以写成方括号之间以逗号分隔的值(项)的列表。关于列表的重要一点是列表中的项目不必是同一类型。

创建列表就像在方括号之间放置不同的逗号分隔值一样简单。例如 -

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"];

与字符串索引类似,列表索引从 0 开始,列表可以被切片、连接等。

访问列表中的值

要访问列表中的值,请使用方括号与索引一起进行切片以获得该索引处可用的值。例如 -

#!/usr/bin/python3

list1 = ['physics', 'chemistry', 1997, 2000]

list2 = [1, 2, 3, 4, 5, 6, 7 ]

print ("list1[0]: ", list1[0])

print ("list2[1:5]: ", list2[1:5])

执行上述代码时,会产生以下结果 -

list1[0]: physics

list2[1:5]: [2, 3, 4, 5]

更新列表

您可以通过在赋值运算符的左侧给出切片来更新列表的单个或多个元素,并且可以使用 append() 方法添加到列表中的元素。例如 -

#!/usr/bin/python3

list = ['physics', 'chemistry', 1997, 2000]

print ("Value available at index 2 : ", list[2])

list[2] = 2001

print ("New value available at index 2 : ", list[2])

Note− append() 方法将在后续部分讨论。

执行上述代码时,会产生以下结果 -

Value available at index 2 : 1997

New value available at index 2 : 2001

删除列表元素

要删除列表元素,您可以使用del如果您确切知道要删除的元素,请声明。如果您不确切知道要删除哪些项目,可以使用 remove() 方法。例如 -

#!/usr/bin/python3

list = ['physics', 'chemistry', 1997, 2000]

print (list)

del list[2]

print ("After deleting value at index 2 : ", list)

执行上述代码时,会产生以下结果 -

['physics', 'chemistry', 1997, 2000]

After deleting value at index 2 : ['physics', 'chemistry', 2000]

Note− remove() 方法将在后续部分讨论。

基本列表操作

列表对 + 和 * 运算符的响应很像字符串;它们在这里也意味着连接和重复,除了结果是一个新列表,而不是一个字符串。

事实上,列表响应我们在前一章中对字符串使用的所有一般序列操作。

Python 表达式

结果

描述

len([1, 2, 3])

3

长度

[1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

Concatenation

['Hi!'] * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']

重复

3 in [1, 2, 3]

True

成员

for x in [1,2,3] : print (x,end = ' ')

1 2 3

迭代

索引、切片和矩阵

由于列表是序列,索引和切片对列表的工作方式与对字符串的工作方式相同。

假设以下输入 -

L = ['C++'', 'Java', 'Python']

Python 表达式

结果

描述

L[2]

'Python'

偏移量从零开始

L[-2]

'Java'

负数:从右数

L[1:]

['Java', 'Python']

切片获取部分

内置列表函数和方法

Python 包括以下列表函数 -

序号

功能说明

1

len(list)

给出列表的总长度。

2

max(list)

从列表中返回具有最大值的项目。

3

min(list)

从列表中返回具有最小值的项目。

4

list(seq)

将元组转换为列表。

Python 包括以下列表方法 -

序号

方法与说明

1

list.append(obj)

将对象 obj 添加到列表

2

list.count(obj)

返回 obj 在列表中出现的次数

3

list.extend(seq)

将 seq 的内容附加到列表

4

list.index(obj)

返回 obj 出现的列表中的最低索引

5

list.insert(index, obj)

将对象 obj 插入列表中的偏移索引处

6

list.pop(obj = list[-1])

从列表中删除并返回最后一个对象或 obj

7

list.remove(obj)

从列表中删除对象 obj

8

list.reverse()

反转列表的对象

9

list.sort([func])

对列表的对象进行排序,如果给定则使用比较函数

上一节: Python 3 - 字符串

上一节

下一节: Python 3 - Tuples(元组)

下一节

查看笔记 分享笔记

笔记内容:

称呼:

Email:

站点:

分享笔记 重置

分类导航

前端

Ajax 教程

Angular 教程

Aurelia 教程

Bootstrap 教程

ChartJS 教程

CSS 教程

ES6 教程

FontAwesome 教程

HTML 教程

HTML 字符集 教程

HTML 游戏 教程

JavaScript 教程

jQuery 教程

Less 教程

React 教程

Sass 教程

Stylus 教程

TypeScript 教程

Unity 教程

Vue.js 教程

WebAssembly 教程

XAML 教程

颜色 教程

服务端

C# 教程

C++ 教程

COBOL 教程

C语言 教程

Fortran 教程

Go 教程

Groovy 教程

Java 教程

JSP 教程

JVM 教程

Kotlin 教程

Lisp 教程

Lua 教程

Node.js 教程

Pascal 教程

Perl 教程

PHP 教程

Python 教程

Python 3 教程

Ruby 教程

Rust 教程

Scala 教程

Spring 教程

Spring Boot 教程

Spring Cloud 教程

VB.Net 教程

移动端

Android 教程

IOS 教程

Objective-C 教程

React Native 教程

Swift 教程

小程序 教程

数据库

Access 教程

DB2 教程

Mariadb 教程

Memcached 教程

MongoDB 教程

MySQL 教程

Neo4j 教程

PL/SQL 教程

PostgreSQL 教程

Redis 教程

SQL 教程

SQL Server 教程

SQLite 教程

T-SQL 教程

数据格式

Jackson 教程

JSON 教程

SVG 教程

XML 教程

开发工具

ActiveMQ 教程

Ant 教程

Apache HttpClient 教程

Apache POI PPT 教程

AWS 教程

Docker 教程

ElasticSearch 教程

ExpressJS 教程

GIT 教程

GitLab 教程

Google Maps 教程

Gradle 教程

Java NIO 教程

JavaFX 教程

JavaMail 教程

JDBC 教程

jMeter 教程

JPA 教程

jsoup 教程

Junit 教程

KoaJS 教程

Kubernetes 教程

Log4j 教程

Logstash 教程

Lucene 教程

Makefile 教程

Maven 教程

RESTful 教程

Sed 教程

SEO 教程

Servlet 教程

SLF4J 教程

Socket.IO 教程

Struts 教程

SVN 教程

TestNG 教程

UML 教程

UNIX / LINUX 教程

WebSocket 教程

WPF 教程

xStream 教程

区块链 教程

数据处理

Flink 教程

Flume 教程

Hadoop 教程

Hbase 教程

Hive 教程

Kafka 教程

Kibana 教程

MapReduce 教程

MATLAB 教程

MyBatis 教程

Pig 教程

R语言 教程

Solr 教程

Spark 教程

Storm 教程

Zookeeper 教程

大数据分析 教程

数据仓库 教程

数据挖掘 教程

计算机基础

HTTP 教程

IPv4 教程

IPv6 教程

Ubantu 教程

WebServices 教程

嵌入式系统 教程

操作系统 教程

数据结构和算法 教程

汇编语言 教程

物联网 教程

电子电路基础 教程

编译器设计 教程

网站开发 教程

计算机 教程

计算机基础 教程

计算机网络 教程

设计模式 教程

AI

CNTK 教程

Keras 教程

PyTorch 教程

TensorFlow 教程

人工智能 教程

机器学习 教程

Python 技术

Django 教程

Flask 教程

NumPy 教程

Pandas 教程

Pillow 教程

PyGTK 教程

PyQt5 教程

PySpark 教程

pytest 教程

Python -数据科学 教程

Python MySQL 教程

Python 取证 教程

Python 数据结构 教程

Python 文本处理 教程

Python 网络编程 教程

Python 网页抓取 教程

Python 设计模式 教程

RxPY 教程

SciPy 教程

Seaborn 教程

SymPy 教程

wxPython 教程

框架

Laravel 教程

Web 图标Icon 教程

Web2py 教程

WebGL 教程

WebRTC 教程

WordPress 教程

Yii 教程

Zend Framework 教程

SAP

Crystal Reports 教程