共同点:
1.它们都是python的核心类型,是python语言自身的一部分
核心类型与非核心类型
多数核心类型可通过特定语法来生成其对象,比如"dave"就是创建字符串类型的对象的表达式;
非核心类型需要内置函数来创建,比如文件类型需要调用内置函数open()来创建。
类也可以理解成自定义的非核心类型。
2.边界检查都不允许超越索引边界
>>> a = 'dave' >>> a[3] 'e' >>> a[4]
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
3.索引方法都使用[ ]
>>> a = 'dave' >>> print a[0] d >>> a = ['d','a','v','e'] >>> print a[0] d >>> a = ('d','a','v','e') >>> print a[0] d
4.大部分都支持迭代协议
不同点:
1.有序性
字符串,列表,元组是序列,元组可理解成不可变的列表
字典是python中唯一的映射类型
序列有有序性,字典没有顺序
2.可变性
列表,字典具有可变性.
字符串,元组,数字具有不可变性,即复制后对应存储空间的内容不可变,除非这个对象被销毁.
例如:
>>> s = 'dave' >>> id(s)
140105068006304
>>> s = 'python' + s[1:] >>> s
'pythonave'
>>> id(s)
140105067997536
第一行的s指向内存空间140105068006304,存储了字符串'dave'
第二行的s变成了'pythonave',但它只是重新指向了一个存储着'pythonave'字符串的内存地址.原来140105068006304的内存空间可能还是'dave',也可能被系统清理销毁.
3.序列操作
序列(字符串,列表,元组)支持,映射(字典)不支持
注意:序列操作生成的类型不变
>>> seq = 'dave' >>> print seq[1:]
ave
>>> seq = ['d','a','v','e'] >>> print seq[1:]
['a', 'v', 'e']
>>> seq = ('d','a','v','e') >>> print seq[1:]
('a', 'v', 'e')
4.列表解析表达式
表达式中支持序列(字符串,列表,元组),也支持映射(字典)
和序列操作不同的是,列表解析表达式生成的只能是一个列表.
>>> str = [ a * 2 for a in 'dave' ] >>> print str
['dd', 'aa', 'vv', 'ee']
>>> L1 = [1,2,3] >>> L2 = [4,5,6] >>> L3 = [(a+b) for (a,b) in zip(L1,L2)] >>> print L3
[5, 7, 9]
>>> tup = (1,2,3) >>> tups = [ a * 2 for a in tup] >>> print tups
[2, 4, 6]
>>> dic = {'a':'dave','b':'emily'} >>> dics = [ i *2 for i in dic.items()] >>> print dics
[('a', 'dave', 'a', 'dave'), ('b', 'emily', 'b', 'emily')]
字典的items(),keys(),values()方法都是返回的列表,这里的for i in a写for i ina.keys(),效果等同。
>>> dic = {'a':'dave','b':'emily'} >>> dicts = [ i * 2 for i in dic] >>> print dicts
['aa', 'bb']
5.嵌套
除了字符串外,列表,元组,字典可以相互多层嵌套
嵌套与可变性是不冲突的。
tup = (4, 5, a)中所记忆,录入的b是个变量指针,或者叫内存地址,是个数字。
>>> a = [1,2,3] >>> tup = (4,5,a) >>> print tup[2]
[1, 2, 3]
>>> a[0] = 'x' >>> print tup[2]
['x', 2, 3]
a指向一个列表,列表具有可变性,所以b[0]='x'的操作后,对元组的索引可以看到变化
>>> a = '123' >>> tup = (4,5,a) >>> print tup[2]
123
>>> a = 'xxx' >>> print tup[2]
123
a指向一个字符串,字符串具有不可变性,所以a = 'xxx'的操作后,a这个指针本身就改变了,重新指向另一段内存空间,而tup元组中记忆的内存地址所指空间并没有变,还是字符串'123'
上面2段,元组tup始终保持不可变性,要变化也是其元素作为指针所指向内存的变化.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 Chinese version
Chinese version, very easy to use

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.

Dreamweaver Mac version
Visual web development tools
