Python basics: Input and output and data types

Source: Internet
Author: User

1, input and output

(1) Output function print ()

To be clear, print is a function, so you have to have parentheses, and there is no python2 inside. Inside the parentheses is the content to be exported, the general string and so on need to quote, single and double quotation marks can be, but not cross-use. By the following experiment, it is probably clear that if you want to use quotation marks in the output string, if there is no quotation mark, the value can be output, can also be calculated, but anyway, the parentheses are necessary.

Note the following: A comma is a delimiter.

(2) Enter function input ()

The input function requires the user to enter a string and save it in a variable. Note that this string does not need to be quoted.

The variables are mentioned here, so let's explain them first:

Variables: In Python, variables are where the data is stored. Variable composition: uppercase and lowercase letters, numbers, underscores. But you cannot start with a number.

From the above introduction to input, you can see that age is a string type, because input requires the user to enter a string, rather than a numeric value. So the above 25 is actually a string "25" instead of a value of 25. We enter the 25 after the carriage return nothing is displayed, a command prompt >>>; this is because you entered the 25 has been saved in the variable age, in order to see the content of age, There are two ways to see it from the picture above. But the display is not the same on the interface, the reason for this is to understand.

Here we are all using the interactive mode, enter a row to execute a line, but the actual work we are all using a file to write a lot of code, one execution, do not let the user participate in the interaction, in this case, you only write an age will not print the value of which, you need to use the OUTPUT function print. In interactive mode, for example, you can learn simple knowledge, but later code a lot, you need to slowly skillfully use other text to edit.

Here's an example.

You write the code with the editor (Sublime recommended), and the file suffix name ends with a. py. Such as

The file name here is aa.py. How do you do it under the D-disk?

There are two age in the code, but only one value is printed. You will not get the value of the variable directly in the code, which is not the same as the interactive.

Also note that in the interactive mode above, print (age) Output: 25, and age returns after output: "25". From this you can see that if it is a string, print will output itself without the quotation marks, but in another case it will output quotes to give you a clearer picture of what age is. From the following picture, it is clear that:

This figure assigns num a value of 2, not a string of 2, but a value of 2. Be sure to make sure that input is a string.

What if you want to enter a number instead of a string? There is a function int (), whose function is to convert the numeric value in string form to a real number, such as converting "25" to 25.

The first part of the above diagram turns the string input into numeric, so it can be computed, and the latter part is not using the INT function, which results in an error. typeerror:unsupported operand type (s) for +: ' int ' and ' str ' This means that the sum of integer and string types is not supported.

The above diagram explains the function int in more depth, but there is a place to note:

(1) If a string contains characters other than a number, the INT function cannot convert such a string to a numeric type.

(2) Another point is int (str1) here if you do not save the result of the conversion in other variables, after the execution of this statement str1 is still a string, but not into a numeric value.

2, data type

In Python, there are several types of data that can be processed directly: integer, float, String, Boolean, empty.

(1) int : Python can handle integers of any size, and mathematically, you can write: -200,0,120000 and so on.

Here, in fact, after installing Python, we have an interpreter that specifically explains Python code, and when the >>> is present, the program that follows it is interpreted by the interpreter. The interpreter is like a calculator, and the results are obtained by entering mathematical operations.

Here are a few things to note:

A) Division ( / ) always returns a floating-point number.

b) Unlike other languages, 4/7 does not return 0.

c) to calculate the remainder you can use (%)。

   d)使用 ** 运算符计算幂乘方。

   e)整数运算永远是精确的

  (2)浮点型:就是浮点数了,也叫小数,你看3.14可以写为:0.000314*104,小数点是不是在浮动?

   表示:3.14,0.02,但是太大的或者极小的小数可以用科学技计数法:把10用e替代,1.23x109就是1.23e9。

From the above calculation can be seen:

A) floating-point arithmetic may have rounding errors.

   b)整数和浮点数的混合计算中,整数会被转换为浮点数。

(3) string : The string is enclosed in double or single quotation marks, but the string does not include the quotation marks themselves. Classes such as: "ABS", ' AAAA '.

Here's a question, so what does the quote itself say? This leads to the escape characters in many languages, in/representation.

Excerpt from online

In the interactive interpreter, the output string is enclosed in quotation marks, and special characters are escaped with backslashes. Although it may not look the same as the input, two strings are equal. If there are only single quotes in the string and no double quotes, use double quotation marks, otherwise quoted in single quotation marks. The print () function produces a more readable output that omits quotes and prints out special characters that are escaped.

If there are a lot of quotes, each to escape inconvenient, then there is also a concise way: is the first quotation mark before adding ar

String literals can be divided into multiple lines. One way is to use three quotes: """...""" or ‘‘‘...‘‘‘ . Line end newline characters are automatically included in the string, but can be added at the end of \ a row to avoid this behavior. The following example: You can use a backslash to end a continuous string of lines, which indicates that the next line is logically a subsequent content of the line, and produces the following output (note that there is no first line to start):

Strings can be + concatenated (glued together) by operators, and can be * duplicated by representations;

The adjacent two string literals are automatically concatenated, and are used only for two string literals and not for string expressions, which is especially useful when you want to slice a long string;

If you want to concatenate multiple variables or concatenate a variable and a string literal, use the+ ;内置函数 len() 返回字符串长度:

(4) boolean value : False,true. A Boolean value of only two values, either, or, True False True False in Python, can be used directly to represent a True False boolean value (note case), or it can be computed by Boolean operations:

and, or, not: with, or, not

And: Binocular operator, only true results are true at the same time

Or: Binocular operator, only false results are false at the same time

Not: Monocular operator, with the opposite result

The above operators can also be used in combination.

(5) null value : null is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

Most of the content to learn online information,

The main is: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

http://www.pythondoc.com/

Python basics: Input and output and 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.