Python notes--"2 data Types"

Source: Internet
Author: User

Directory

? Pre-knowledge

? Data type

? Extended knowledge

One

Pre-knowledge

If the reader has the basis of other programming languages, such as: C language. You will find that Python does not need to declare its data type when using "variables". Even in Python, there is no concept of "variable"! Instead, it is called an object reference.

Most of these features are because Python is an object-oriented programming language. In Python, everything is the object! Therefore, the data type is also an object. You can deepen your understanding of this issue.

Data type objects and object references in Python:

It is visible that the data types in Python are for data objects in the heap. The object reference is also equivalent to the data store address, so it is not necessary to declare its data type. In Python, an object reference is also a type of identifier that can be named using an identifier naming convention.

Python identifier naming rules

    1. Start with a letter or underscore followed by letters, numbers, or underscores. Where letters or underscores can be letters or numbers in Unicode encoding.

    2. Identifiers cannot be duplicated with keywords, and identifiers are of unlimited length.

    3. Identifiers should be made meaningful and readable. such as: "MyName" or "my_name".

View keywords in python

Two

Data type

When we change the value of an object that is stored in the heap, if its reference (address) changes, we call the object's data type an immutable data type. Conversely, it is called a mutable data type.

Immutable data types

    1. Integer types and floating-point types

The above two data types represent integers and floating-point numbers, respectively. In addition, Python supports complex numbers. Integer types in Python are used by default in decimal notation, and other representations are:

Binary octal hexadecimal
Prefix 0b 0o 0x
The operation of these two types of data is usually a variety of arithmetic operations. Python can provide good support. These operators include: "+", "-", "*", "/", "//", "* *", "%". As you can see, in Python there are two types of division: "/" and "//". The former is exact division and the result is a float type. The latter is the floor except the result is of type int (value quotient). In addition, in an interactive environment, the "_" symbol can be used to save the results of the last operation.

    1. String type

In Python, the string type is even enclosed in single or double quotation marks. Multi-line content can be enclosed in three quotes (is there a comment?). )。

For strings, common operations include case conversions, string connections, and so on.

# character Case Conversion
2s1 = "Hello world!" # example String.
3S1 = S1.title () # capitalize the first letter.
4S1 = S1.upper () # All converted to uppercase.
5S1 = S1.lower () # All converted to lowercase.
6
7# string concatenation
8s1 = "Hello"
9S2 = "world!"
10S3 = s1 + S2 # S1,S2 is the string to be concatenated, S3 is the concatenated string.
11S4 = S1 * 2 + s2 * 2 # The same multiplication can stitch the same string multiple times.
12
13# Delete string whitespace
14s1 = "Hello world!"
15S1 = S1.lstrip () # Deletes the white space character at the beginning of the string.
16S1 = S1.rstrip () # removes whitespace characters at the end of a string.
17S1 = S1.strip () # removes whitespace characters at both ends of the string.

Because the quotation marks that represent strings do not belong to the contents of a string, what happens when the contents of a string contain quotation marks? In addition to using single quotation marks (double quotes) in a string represented by double quotation marks (single quotes), we can also use the escaped method, that is, ' and ' to represent single and double quotation marks. The commonly used escape word used by itself is summarized as follows:

Escape character \b \ r \ n \ \v \ \ooo \xhh
Meaning backspace newline carriage return Horizontal tab Vertical tab backslash backslash octal character hexadecimal character
Additionally, adding the prefix "R" to the string invalidates the escape character in the string as it is displayed. Examples of escaping are shown below:

# Print content: I ' m fine, thanks.
2print ("I ' m fine, thanks.")
3print (' I ' m fine, thanks. ')
4
AA Other Escape Character application example
6print ("he\rllo\b wor\tld") # Result: ll wor ld
7print ("\x68\x65\x6c\x6c\x6f") # Result: Hello
8print (r "he\rllo\b wor\tld") # Result: he\rllo\b wor\tld

If you know the C language, you must be familiar with the format of the string. Python has two different ways of formatting strings: printf-format string and

The printf-format string is formatted like the C language, using% for formatting operations. The syntax format is:

Format% values

Where format is a string, and% is a formatting operator, values correspond to the conversion character one by one in format, and when the number of conversions is more than one, values is a tuple or dictionary.

Application tiers: providing services to users

Four kinds of physical topologies:

Python identifier naming rules

    1. Start with a letter or underscore followed by letters, numbers, or underscores. Where letters or underscores can be letters or numbers in Unicode encoding.

    2. Identifiers cannot be duplicated with keywords, and identifiers are of unlimited length.

    3. Identifiers should be made meaningful and readable. such as: "MyName" or "my_name".

String Type (str)

In Python, there are two ways to format a string: The% format operator and the format function.

% formatting operators

First, let's look at a simple example:

From the above example, we can see:

The string can be formatted with a tuple and dict variable, and the result is still a string.

The% in the string corresponds to each real value in the subsequent tuple and dict, and is formatted with the content after the%.

For the type code selection, refer to the following table:

Type code and its description
S gets the return value of the __str__ method of the passed-in object and formats it to the specified location
R gets the return value of the __repr__ method of the passed-in object and formats it to the specified location
C Integer: Returns the Unicode character represented; character: Adds a character to a specified location
o Converts an integer to an octal representation and formats it to a specified location
X converts an integer to a hexadecimal representation and formats it to a specified location
D converts an integer, floating-point number to a decimal representation and formats it to a specified position
E, E converts integers, floating-point numbers into scientific notation, and formats them to a specified location
F, F converts an integer, floating-point number to a floating-point number representation and formats it to a specified position (the default is 6 digits after the decimal point)
G, g automatic adjustment converts an integer, floating-point number to float or scientific notation, and formats it to a specified position
%
When formatting flags exist in a string, a percent sign is required

You can use the universal S-type character when you do not know what type character to choose.

Format function

Let's also look at a simple example

From the above example, we can see:

The format () function accepts several parameters for formatting a string. The use of parameters is related to the invocation of a string.

Use {} and: For formatting control in strings. The ordinal number in {} represents the format () function parameter order.

Python notes--"2 data Types"

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.