Practice python (4)-reveal common built-in functions, data types, and python data types from simple case Columns

Source: Internet
Author: User

Practice python (4)-reveal common built-in functions, data types, and python data types from simple case Columns


In the previous article, we talked about the print statement. Which types of print can be printed to the screen?

  • INTEGER (int)
  • Long (long)
  • Float)
  • String (str)
  • Boolean (bool)

These are the most common ones.


Before that, I have to mention a knowledge-variable. What is a variable? It is generally understood that it is a changing amount. Its value will change the amount, which corresponds to a constant (number. In fact, this is not accurate.

A variable name is like a real-world name. When a value is assigned to a name, TA is stored in the memory and called a variable ). In most languages, this behavior is called "assigning values to variables" or "storing values in variables. assigning values to variables is an equal sign (= ), however, in python, for example, a = 4, the value of a is 4, and then I make a = 6, and the value of a is 6.However, note that the value assignment is not equivalent to the equal sign in mathematics, and is equal to '='. It is used as a judgment in python.In python, the = 4 operation is interpreted as being inaccurate in assigning values to variables. 4 does not store values in variables, it is more like posting a name on a value. This involves a keyword promoted by python: memory space



  • Assign a value before using the variable.
  • The variable name can be a letter, number, or underscore, but the variable cannot start with a number.
  • Case Sensitive
  • The '=' symbol indicates a value. The name on the left and the value on the right are values.
  • The variable name should be a professional name as far as possible.
  • Variable is an immutable object (this will be mentioned later, first understand that it is an immutable object)


The following variable names are used for ease of explanation and are free to use. For simple operations, I use the python2 IDLE.

I. Basic Knowledge:

1. INTEGER (int ):

An integer. It is a number without decimal digits, such as 1, 2,-1, and-2.


Here we use a BIF (built-in function) -- type (), which allows you to view the type to identify the type.

There is also a function with the same function as the type () function: isinstance (), which is used as follows:

So why is there only one parameter in type, and isinstance uses two parameters? Why? How can I know its usage?

Here we need to use a BIF -- help () to view the official python documentation, such:

(Because the output content is very long, only one useful point is taken.) only the line indicating the object indicates the object. In python, everything is an object, so type () the usage is that you can put everything in and feed back the type of this object to you.

Take a look at the isinstance () official help documentation:

This means that two parameters are required. The first parameter is an object of the type to be viewed, the second parameter is a type, and a boolean type is returned (see point 5th ), in this way, you can identify the type.


2. long Integer (long ):

Long integer is also an integer, but because it is longer than the integer, it is called a long integer, such as 123552466L, followed by a 'L' is a long integer,There is no longer a long integer in python3Because in fact there are no birds to use, most of the time it is still directly used for shaping



3. float ):

That is, the number type with decimal places, such as 1.5, 1.0, and 1.555.

It corresponds to the floating point type with a fixed point type (double), which is also a decimal number type, which is rarely used.

4. string type (str ):

It is enclosed by quotation marks, such as 'python' and 'Hello'. It can be single or double quotation marks, but cannot be mixed in single or double quotes.


5. boolean (bool ):

Burburn is the name of a person. At that time, the person defined the light on as 1, and the light off as 0, which is like binary 0/1, but always only in these two States, it is impossible to be in a state where no lamp is turned on, no lamp is turned off, or the light is turned on and off, and this principle is used by other researchers in the principle of computation, so that only one of the two states can be carried forward.

The same is True in programming. There are two keywords: True, False, True, False, and False, these two keywords play a major role in judgment.

Another point is that True and False are actually equal to 1 and 0 respectively:

However, in programming, if True and False are used as values of 1 and 0 to participate in computing, this is not what a qualified programmer can do. Just remember this, it can be easily understood in future programming.

Good. I have learned about common types and several built-in functions and usage.


Data types can be converted to each other.Python is flexible. It is not similar to JAVA. JAVA can only convert data types to data types with long lengths, if the data type length is long, it cannot be converted into a short data type (mandatory conversion is required, so it is not mentioned here ).

You can see the following simple examples:





Start printing something:

Print a file path:

Here is a concept. In fact, you should note that the result of print is different from that of no print, that is, why is it in the interactive interface (IDLE) is the result of directly giving the variables and print variables in a different way? Because a \ n line break is automatically added to print, the following code can be separated during programming, and the expression represents one thing. The print + expression is the same thing, for example, '1 + 2' means this. print '1 + 2' is used to print '1 + 2, in addition, I personally think that print in python will automatically process the code and print it to the screen according to the default encoding settings. However, note that print will not change the object itself, just print it to the screen based on your own mechanism.

Note that in Windows, the path Delimiter is dual \, which is required for Windows.

Print another one:

It is a little different from what we expected. Before that, we need to know several special symbols:

\ Escape character; newline continuation (will be mentioned later in multiple strings)

\ N indicates line feed

\ T indicates a tab, that is, the position of a tab key.

\ Represents \ character itself

# Comments character. You can also comment out code that is not currently in use.

So here we mistakenly treat '\ n' and' \ t' as special characters, and thus a line break and a tab


Escape it and use \ to convert it into itself, so that it has no special meaning:

Practical application:

Print -- Let's go (which contains a quotation mark)

Consistent operation: The color has changed because the keyword color is highlighted, and the color here is obviously incorrect.

Sure enough, the error is reported in red. The error indicates a syntax error, which will be explained in detail later in exception handling.

The outer layer can be enclosed in double quotation marks:

Or escape with an escape character:

But there is another problem. If the path strength is too long, is there another way to manually add escape characters?

So it involves a new usage. Yes, my routine is to gradually get rid of the concept to be mastered in actual problems.

R --- original string

Original string: All strings are used literally without escaping special or printable characters.

Add r before the string. This usage is actually used when encoding is mentioned earlier. Check the Chinese character in Chinese before and use Unicode to write the character in U '], you can flip the front.

But there is another problem. According to the concept of the original string, I can write it like this:

But an error is reported. The first one just said that it is because of the character conflict between quotation marks. Adding the quotation marks to the r cannot be solved. The second is added to the backslice at the end, or an error is returned. The solution is as follows:

Escape independently when the original string cannot be resolved:

But it is still not convenient, right? What if it will be used in the future? It's a little troublesome to escape each one again.

So there is another ultimate killer: multi-line string, or long string ''', three quotation marks

No matter what is in a multi-line string, it is a string and does not make any meaning. As the name suggests, you can also comment multiple lines.

However, note that if the multi-line string has a reverse line at the end, it will look like this:


It is useless to keep hitting the car, because the \ at the end of the line breaks into the meaning of continued writing, or you can also understand it as an escape character \ to escape the original anti-three quotation marks. So it has never been completed. You can manually add the reversed quotation marks to complete the calculation.



Finally, there is another problem, because the character strings after print can print anything in the string. If I don't want to do this, I want to execute the code in the character string and get a result?

For example:

What should I do if I want this result?

There are two built-in functions to solve this problem, exec () and eval ()

However, these two functions are rarely used. Remember to use them in this way.

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: 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.