The basics of Python (iv)

Source: Internet
Author: User

Security Code: Indifferent Childe


In this article, we say Python's built-in object types and their operations.

Python has a lot of built-in types, why should we use built-in types? Is Python programming as much as possible to use custom types or as many built-in types as possible?

The built-in type has a better understanding of Python itself, and a bit better performance when it comes to execution. Therefore, it is generally best to use built-in types unless the built-in types cannot provide special object handling. Because built-in types make their built-in object programs easier to write, in addition, built-in types, built-in objects are extension components, and built-in objects tend to be more efficient at execution than the kind of data structures they develop.


Related terms for Python objects:

All the data stored in a Python program is carried out around the concept of the object:

All data stored in the program is an object.

Each object has an identity, a type, and a value.

For example, school= "Cheqiao" creates a string object with "Cheqiao" whose identity is a pointer to its location in memory (its address in memory), and school is the name that references that particular location.

The type of the object is also called the category of the object, which describes the internal representation of the object and the methods and operations it supports.

When you create an object of a specific type, you sometimes refer to that object as an instance of that type.

Once an instance is created, its identity and type cannot be changed

If the object value is modifiable, it is called a mutable object;

If the object value is not modifiable, it is called an immutable object

If an object contains references to other objects, it is called a container.

Most objects have a large number of unique data properties and methods

Property: The value associated with the object. When an object is instantiated, the variable name of the variable that can be used inside the object is called the object's property.

Method: A function that will perform certain operations on the object when called.

Use the dot (.) operator to access properties and methods.


The identity and type of the object:

Python built-in function ID () returns the identity of an object, the location of the object in memory

The IS operator is used to compare the identities of two objects;

Type () is used to return the types of an object;

The object type itself is also an object, called the class of the object

The definition of the object is unique and is the same for all instances of a type

All types of objects have a specified name that can be used to perform type checking, such as list, Dict

If A is b:statements if a = = B:statements if Type (a) is type (b): statements


Python Core Data type:

Object type
Example
Digital
3077,3.14,300000
String
' baidu.com ', ' spam '
List
[' One ', ' I ', ' three ']
Dictionary
{' Course ': ' Linux ', ' tutor ': ' Baidu ', ' age ': 42}
Meta-group
(+, ' spam ', ' eggs ')
File
Myfile=open ('/tmp/tfile ', ' R ')
Collection
Set (' abc '), {' A ', ' B ', ' C '}
Other types
Class-Type, None, Boolean
Programming Unit Type
Functions, modules, classes
Implementation-related types
Compiled code stack trace

Python is a strongly-typed language, and many times we need to explicitly convert the type.

such as a number and a letter phase operation, this is a throw exception. Because the two are not the same type, there is no way to operate between the different types in Python.

In [1]: a = ' 123 ' in [2]: Type (a) out[2]: Strin [3]: Print 1 + a------------------------------------------------------------ ---------------TypeError Traceback (most recent) <IPYTHON-INPUT-3-30ABB1F25EC3 > in <module> ()----> 1 print 1 + atypeerror:unsupported operand type (s) for +: ' int ' and ' str '

At this point, if we make an explicit conversion of the type, we can complete the operation.

In [1]: a = ' 123 ' in [2]: Type (a) out[2]: Strin [3]: Print 1 + a------------------------------------------------------------ ---------------TypeError Traceback (most recent) <IPYTHON-INPUT-3-30ABB1F25EC3 > in <module> ()----> 1 print 1 + atypeerror:unsupported operand type (s) for +: ' int ' and ' str ' in [4]: b = int (a) In [5]: type (b) out[5]: Intin [6]: Print 1 + b124

The common built-in functions for type explicit conversions in Python include the following:

STR (), repr (), or format (): Used to convert a non-string to a string

The result of STR () is the same as the result of print;

Repr () indicates the exact value of an object;

Format () converts it to a string using a specific format

Int (): Convert to Integer

Float (): Convert to floating point

List (s): Convert string S to list

Tuple (s): convert string s to Narimoto group

Set (s): Converts the string s into a collection

Frozenset (s): convert string s to immutable collection

Dict (d): Creates a dictionary based on the specified key-value pair. The d here must be (Key,value) a tuple sequence such as D = ((' A ', 1), (' B ', 2), (' C ', 3)) or d = [(' A ', 1), (' B ', 2), (' C ', 3)]

Ord (x): Converts the character x to an integer value

Hex (x): Converts an integer x to a hexadecimal string

Bin (x): Converts an integer x into a binary string

Oct (x): Convert integer x to octal string

This article from "Indifferent bo" blog, please be sure to keep this source http://itchentao.blog.51cto.com/5168625/1887192

The basics of Python (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.