Python入门篇之对象类型
Python使用对象模型来存储数据。构造任何类型的值都是一个对象
所有的Python对象都拥有三个特性:身份、类型、值
身份:
每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以使用内建函数id()来得到。这个值可以被认为是该对象的内存地址
类型:
对象的类型决定了该对象可以保存什么类型的值,可以进行怎样的操作,以及遵循什么样的规则,可以使用内建函数type()查看Python对象的类型:
代码如下:
>>> type([1,2])
>>> type(24)
>>> type({1,2,3})
>>> type('a string')
type()返回的是对象而不是简单的字符串。
值:对象表示的数据项
以上的三个特性在对象创建的时候就被赋值,除此之外,其他两个特性都是只读的
标准类型/基础数据类型:
数字、整型、布尔型、长整形、浮点型、复数型、字符串、列表、元组、字典
其他内建类型:
类型、Null对象(None)、文件、集合/固定集合、函数/方法、模块、类
None,Python的Null对象
Python有一个特殊的类型,称为Null对象或者NoneType,只有一个值:None,它不支持任何运算也没有任何内建的方法,类似于C语言的void,None类型的值
和C中的Null值非常相似
None没有什么有用的属性,它的布尔值总是False
布尔值
所有标准对象均可以用于布尔测试,同类型的对象之间可以比较大小。每一个对象天生具有布尔True或false值
空对象、值为0的任意数字或Null对象None的布尔值都是False
下列对象的布尔值都是False:
None
False(布尔类型)
所有的值为0的数
0(整型)
(浮点型)
0L(长整型)
0.0+0.0j(复数)
“”(空字符串)
[](空列表)
()(空元组)
{}(空字典)
值不是上面列出来的任何值的对象的布尔值都是True
标准类型运算符:
比较运算符用来判断同类型对象是否相等,所有的内建类型均支持比较运算,比较运算返回布尔值True或False
代码如下:
>>> 2==2
True
>>> 2.34
True
>>> 'abc'=='xyz'
False
>>> 'xyz'>'abc'
True
>>> 'xyz'
False
>>> [3,'abc']==['abc',3]
False
>>> [3,'abc']==[3,'abc']
True
多个比较操作可以在同一行上进行,求值顺序为从左到右.例如:
代码如下:
>>> 3
True
>>> 4>3==3 #等价于(4>3)and(3==3)
True
>>> 4
False
标准类型值比较运算符:
对象身份比较
每个对象都天生具有一个计数器,记录它自己的引用次数。这个数目表示有多少个变量指向该对象
Python提供了is和is not运算符来测试两个变量是否指向同一个对象
a is b 等价于 id(a)==id(b)
代码如下:
>>> foo2=foo1
>>> foo1 is foo2
True
>>> foo1 is not foo2
False
>>> id(foo1)==id(foo2)
True
>>>
布尔类型
布尔逻辑运算符and,or,not都是Python关键字,这些运算符的优先级按从高到低的顺序如下:
标准类型布尔运算符:
代码如下:
>>> x,y=3.1415926,-1024
>>> x
True
>>> not(x
False
>>> (x2.71828)
True
>>> (x2.71828)
False
>>> not(x is y)
True
标准类型内建函数
Python提供了一些内建函数用于这些基本对象类型:
cmp(), repr(), str(), type()和等同于repr()函数的单反引号('')运算符
type()接受一个对象作为参数,并返回它的类型。它的返回值是一个类型对象。
代码如下:
>>> type(4)
>>> type('hello!')
>>> type(type(4))
cmp()用于比较两个对象obj1和obj2,如果obj1
代码如下:
>>> a,b=-4,12
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b=-4
>>> cmp(a,b)
0
>>> a,b='abc','xyz'
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b='abc'
>>> cmp(a,b)
0
str()和repr()(及''运算符)
内建函数str()和repr()或反引号运算符('')可以方便的以字符串的方式获得对象的内容、类型、数值属性等信息。
str()函数得到的字符串可读性好,而repr()函数得到的字符串通常可以用来重新获得该对象
代码如下:
>>> str(4.53-2j)
'(4.53-2j)'
>>> str(1)
'1'
>>> str(2e10)
'20000000000.0'
>>> str([0,5,9,9])
'[0, 5, 9, 9]'
>>> repr([0,5,9,9])
'[0, 5, 9, 9]'
>>> '[0,5,9,9]'
'[0,5,9,9]'
str()和repr()和''运算在特性和功能方面都非常的相似,repr()和''做的事情完全一样,返回的是一个对象的官方字符串表示,可以通过求值运算(使用eval()内建函数)重新得到该对象,但是str()函数则有所不同,它可以生成一个对象的可读性好的字符串表示,返回结果无法用于eval()求值,但是很适合用于print语句输出。
标准类型运算符和内建函数

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
