Python列表知识补充

1、import this  Python之禅,圣经。

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

2、title()  使字符串第一个字母大写

>>> s = "cairui"
>>> print(s.title())
Cairui

3、列表的负索引

-1代表最后一个元素,-2代表倒数第二元素

>>> s = ['cairui',123,456,789,'lei']
>>> print(s[-1])
lei
>>> print(s[-2])
789
>>> 

4、range

>>> range(1,5)
[1, 2, 3, 4]
>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]
>>> 
>>> i1 = list(range(2,11,2))
>>> print i1
[2, 4, 6, 8, 10]

5、min(列表)  取最小值  max(列表) 取最大值  sum(列表) 求和

6.用切片打印整个列表

>>> players = [1,2,3,4,5]
>>> print(players[:])
[1, 2, 3, 4, 5]
>>> 

  

7、append  在列表末尾添加元素  insert(索引,内容)在第几个索引位置添加元素  

>>> s = ['a',123,456,789]
>>> s.append('rui')
>>> print(s)
['a', 123, 456, 789, 'rui']

['a', 123, 456, 789, 'rui'] >>> s.insert(0,'rui') >>> print(s) ['rui', 'a', 123, 456, 789, 'rui'] >>> 

 

转载于:https://www.cnblogs.com/caicairui/p/7550724.html

<think>好的,用户想要获取Python编程语言的基础知识汇总,特别是提到“Python基础知识大全”教程。我需要先整理一下Python的核心知识点,并参考提供的引用内容来确保信息准确。 首先,用户给出的引用中有四个来源。引用1提到有一个PDF资源,地址可能需要检查是否有效。引用2讨论了type函数和自定义异常,引用3展示了自定义异常的例子和多进程导入,引用4详细介绍了字典的操作、用户输入和循环。我需要综合这些内容,同时补充其他基础知识点,比如变量、控制结构、函数、模块等。 接下来,用户可能希望得到一个结构清晰的回答,分点列出各个基础部分。需要确保使用正确的中文术语,并且按照系统指令中的格式要求,数学公式用$...$,独立公式用$$...$$,但看起来Python基础可能涉及较少的数学公式,不过如果有的话要注意格式。 用户示例中的快速排序部分有代码块,所以我也需要在适当的位置插入Python代码示例,比如变量定义、条件语句、循环、函数定义等。同时,根据引用中的内容,比如字典的使用、异常处理、模块导入等,需要详细说明这些部分。 然后,需要生成相关问题,参考示例中的结构,问题应该围绕Python基础知识的扩展,比如数据类型、控制流、函数、模块管理、异常处理等。确保问题能够帮助用户进一步深入学习。 另外,引用中提到的资源下载地址可能需要确认是否仍然有效,但用户可能更关心内容而不是下载链接,所以重点放在知识点的整理上。同时,注意引用标识的正确添加,比如在提到type函数时引用[^2],在字典部分引用[^4]。 最后,检查回答是否符合系统指令:使用中文,结构清晰,代码正确,引用正确。确保没有使用Markdown格式,段落之间用换行分隔,代码块用三个反引号包裹,并标明语言。 现在开始组织内容,分基础语法、数据类型、流程控制、函数、模块、异常处理、字典操作等部分,每个部分结合引用中的示例,并添加代码块。最后生成相关问题,如数据类型、循环控制、函数参数、模块管理、异常处理应用等。</think>我将根据提供的参考资料和Python基础知识体系,为您整理Python编程语言的核心知识点汇总,包含代码示例和实践要点。 ### 一、基础语法结构 1. **变量与数据类型** ```python # 基本类型示例 name = "Python" # 字符串 version = 3.9 # 整数 ratio = 3.14 # 浮点数 is_oop = True # 布尔值 ``` 2. **类型检测与转换** 使用$type()$函数检测类型[^2]: ```python num = 10 print(type(num)) # <class 'int'> print(isinstance(3.14, (int, float))) # True ``` ### 二、核心数据结构 1. **字典操作**(引用[^4]详细示例) $$ \text{字典结构} = \{ \text{键}_1:\text{值}_1, \text{键}_2:\text{值}_2, ... \} $$ ```python user = {"name": "Alice", "age": 25} user["email"] = "alice@example.com" # 添加键值对 ``` 2. **列表与元组** ```python fruits = ["apple", "banana", "orange"] # 可变列表 coordinates = (40.7128, -74.0060) # 不可变元组 ``` ### 三、流程控制 1. **条件语句** ```python score = 85 if score >= 90: print("A") elif score >= 80: print("B") # 输出B else: print("C") ``` 2. **循环结构** ```python # While循环示例 count = 0 while count < 5: print(f"当前计数:{count}") count += 1 ``` ### 四、函数与模块 1. **函数定义** ```python def calculate_area(radius): """计算圆面积""" return 3.14 * radius ** 2 print(calculate_area(5)) # 输出78.5 ``` 2. **模块导入**(引用[^3]) ```python import math from datetime import date print(math.sqrt(16)) # 4.0 print(date.today()) # 当前日期 ``` ### 五、异常处理(引用[^3]示例扩展) ```python try: age = int(input("请输入年龄:")) except ValueError: print("请输入数字!") else: print(f"您输入的年龄是:{age}") ``` ### 六、面向对象编程 ```python class Animal: def __init__(self, name): self.name = name def speak(self): raise NotImplementedError class Dog(Animal): def speak(self): return "汪汪!" my_dog = Dog("阿黄") print(my_dog.speak()) # 汪汪! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值