Python learning-Basic Data Types

Source: Internet
Author: User
Tags first string

Reference books: python3ProgramDevelopment Guide Version 2
Learning Platform: Windows

The only difference between the built-in data type and the standard database data type is that for the latter, we must first import the relevant modules and use the module name to limit the data type name.

Identifiers and keywords in Python

We learn everyProgramming LanguageThe identifiers (and the legendary Hello World) start with the keyword, and learning python is no exception.

Python identifiers must comply with three rules:
    1. 1. The identifier is case-sensitive, as long as it is a Unicode-encoded letter. In fact, there will be no problems in the actual programming process;
    2. 2. the python identifier cannot have the same name as the python keyword;
    3. 3. you should avoid using underscores (_) at the beginning and end of a name. Special Methods and variables are defined in Python, this is the name used;
Integral type

Python provides two built-in integral types: int and bool.

Boolean expression

0 and false indicate false, and any other integer and true indicate true. In numeric representation, true indicates 1, and false indicates 0;

Integer

The default value is decimal. The binary number is 0 B, and the octal value is 0 o. The hexadecimal number is 0x.

Floating Point Type

Python provides three floating point values: the built-in float and complex types, and the decimal. Decimal type from the standard library. These three data types are fixed. If we really need high precision, we can use the decimal. Decimal type from the decimal module.

Floating Point Number

Import the SYS module. The sys. float_info object has many floating point attributes.

Plural

The data type of the plural value is fixed. It stores a pair of floating point numbers, one representing the real part and the other representing the virtual part.

Decimal type

To create a decimal type, you must first import the decimal module. The decimal number is created using the decimal. Decimal () function. This function can take an integer or string as a parameter;

String

A string is represented by a fixed STR data type, which stores the Unicode Character Sequence. The STR data type can be called as a function to create a String object-Parameter
If it is null, an empty character is returned. If the parameter is not null, a string of this parameter is returned. If the parameter is a string, a copy of this string is returned. The STR () function can also be used as a conversion function.
All characters in a string modified by R are interpreted literally, so escape is not required.
The string is long and contains multiple lines. There are two methods:

    1. 1. t = "I Love You" + \
      "I love you, baby .".
    2. 2. S = ("I love you"
      "Together; I love you .").

Because the. py file uses UTF-8 unicode encoding by default.
The built-in function ord () converts a character to an integer of a character in Unicode encoding.
The built-in function CHR () converts the integer of a character in Unicode encoding to unicode encoding.

Character conversion comparison

Supported <, <=, = ,! =,> And> =

String step and step

You can use [] to access a single character in the Operation string.
Negative index is very useful, especially-1. This value always represents the last character of the string.
Extract Parts:
SEQ [start]
SEQ [start: end]
SEQ [start: end: Step]
SEQ can be any sequence, such as a list, string, or tuples.
When many strings are involved, using + for join and using + = for append operations is not very efficient. If you need to connect a large number of strings, it is usually best to use the str. Join () method.

String operators and Methods

A string is a fixed sequence. All functions used for a fixed sequence can be used for a string, including using in for membership testing.

Returns the number of characters in a string using Len ().

The join () method uses a sequence as a parameter (string list or string tuples) and stores it together in a separate string.

>>> test = [ "  jelly  " , "   think  " ,  " | " , "   idea jelly  " ,  "  www.jellythink.com  "  ] >> "  "  . join (TEST)   '  jelly think | idea jelly www.jellythink.com   ' 

The join () method can also be used with the built-in reversed () function to reverse the string. However, you can obtain the same result more accurately by step, for example, s [:-1].

The '*' operator provides the string copy function.

If the string parameter on the left of the member relationship operator in is a part of the string parameter on the right, or is equal, true is returned.

 
>>> Test ="Jellythink">>>"Jelly" InTesttrue
Str. index and str. Find

To locate the position of another string in a string, you can use either of the following methods:

    1. Use the str. Index () method. Returns the index location of the sub-string, or generates a valueerror exception when a failure occurs;
    2. Use the str. Find () method. This string returns the index location of the string or-1 if it fails;

Both functions have two optional parameters: the start position and the end position.

Strip

Strip () can strip white spaces on both sides, including lstrip () and rstrip (). You can use a string as a parameter to call the strip method, so that each corresponding character in the parameter is eliminated.

Replace

Str. replace () is used to replace a string. This method uses two strings as parameters, returns a copy of the string (all occurrences of the first string are replaced by the second string ).

Split

Use the str. Split () method to split the string. The string to be split is used as the first parameter, and the maximum number of child data segments to be split is the second parameter.

Use Str. Format () to format strings.

The Str. Format () method returns a new string. In the new string, the replacement field of the original string is replaced by the formatted parameter.
Each replacement field is identified by the field name included in the curly brackets. If the field name is a simple integer, it is used as the index location of a parameter passed to the str. Format () method. Therefore, in this case, the field named 0 is replaced by the first parameter, and the field named 1 is replaced by the second parameter.
If you try to connect strings and numbers, python will generate a typeerror exception, but you can easily do this using the str. Format () method.

>>>  "  {who} Turned {age} This year  " . format (WHO =  "  she  " , age = 88 )   '  she turned 88 this year   ' "   the {who} was {0} Last week  " . format (12, WHO =  "  boy  "  )   '  the boy was 12 last week   ' 

A field name can reference a set of data types, such as a list. In this case, we can include a data item identified by an index.

 
>>> Stock = ["Paper","Envelopes","Notepads","Pens"]>>>"We have {0 [1]} and {0 [2]} in stock". Format (stock)'We have envelopes and notepads in stock'

The key-value item stored in the python dictionary can also be used in the str. Format () method.

Additional knowledge Character encoding

Unlike the UTF-8, The UTF-16 text should start with a byte mark so that it is used to readCodeYou can determine whether the byte pair is big-Endian or little-Endian.
The Str. encode () method returns a byte sequence-actually a bytes object. By using this method, we can better understand the differences between different encoding formats.
The Str. encode () method has two points worth noting. The first parameter (encoding name) is case-insensitive, and the second parameter specifies the error handling method. If it is "Ignore", "replace ", you can encode any string in ASCII format.
The corresponding method of str. encode () is bytes. Decode () or bytearray. Decode (). This method returns a string in which bytes are decoded using the given encoding format.
In python, at least two Python packages can be used to detect the file encoding format.

CSV Processing

Python has a powerful CSV processing module.

In Neusoft-Dalian

========================================================== ==================================

If you like it, follow these steps:Jellythink | idea jelly

More original highlightsWww.jellythink.com

You can also follow Sina Weibo:Http://weibo.com/u/1887014677

========================================================== ==================================

Related Article

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.