Python (2): Data Type, print statement, comment, and pythonprint in Python

Source: Internet
Author: User

Python (2): Data Type, print statement, comment, and pythonprint in Python

As the name suggests, a computer is a machine that can be used for mathematical computation. Therefore, a computer program can naturally process various numerical values. However, computers can process a variety of data, such as text, graphics, audio, video, and web pages. Different data types need to be defined. In Python, the following types of data can be processed directly:

I. Integer

Python can process Integers of any size, including negative integers. in Python, the expression of integers is the same as that in mathematics, such as 1,100,-80, and 0.

Because the computer uses binary, it is convenient to use the hexadecimal notation to represent integers. The hexadecimal notation is indicated by the 0x prefix and 0-9, a-f, for example, 0xff00, 0xa5b4c3d2, and so on.

Ii. Floating Point Number

A floating point is also a decimal point. It is called a floating point because the decimal point position of a floating point is variable according to the scientific notation. For example, 1.23x10 ^ 9 and 12.3x10 ^ 8 are equal. Floating point numbers can be written in mathematics, such as 1.23, 3.14,-9.01, and so on. However, for large or small floating point numbers, we must use scientific notation to represent 10 with e, and 1.23x10 ^ 9 is1.23e9, Or12.3e8, 0.000012 can be written1.2e-5And so on.

Integers and floating-point numbers are stored in computers in different ways. Integer operations are always accurate (is Division accurate? Yes !), Floating-point operations may have rounding errors.

Iii. String

The string is any text enclosed by ''or" ", such'Abc',"Xyz"And so on. Please note that,''Or""It is only a representation, not a part of the string. Therefore, the string 'abc' only contains three characters: a, B, and c.

Iv. Boolean Value

The Boolean value is exactly the same as that of the Boolean algebra. A boolean value can only be True or False, either True or False. in Python, values of True and False can be used to represent boolean values (case-sensitive) or computed using Boolean operations.

Boolean values can be calculated using and, or, and not.

The and operation is an operation. Only True is used for all operations, and True is used for all operations.

Or is an or operation. If either of them is True, or is True.

The not operation is not an operation. It is a single-object operator that converts True to False and False to True.

5. null values

A null value is a special value in Python, expressed as None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.

 

 

PrintThe statement can output the specified text to the screen. For example, to output 'hello, world', use the Code as follows:

>>> print 'hello, world'

Note:

1. When we write code in a Python interactive environment,>>>Is the prompt of the Python interpreter, not part of the code.

2. When writing code in a text editor, do not add the code yourself >>>.

The print statement can also keep up with multiple strings separated by commas (,) to form a string of output:

>>> print 'The quick brown fox', 'jumps over', 'the lazy dog'
The quick brown fox jumps over the lazy dog

Print prints each string one by one. When the comma "," is used, a space is output. Therefore, the output string is combined as follows:

 

Print can also print an integer or calculate the result:

>>> print 300
300 # running result
>>> print 100 + 200
300 # running result

Therefore, we can print the results of computing 100 + 200 more beautifully:

>>> print '100 + 200 =', 100 + 200
100 + 200 = 300 # running result

Note:For 100 + 200, the Python interpreter automatically calculates the result 300. However, '2014 + 100 = 'is a string rather than a mathematical formula. Python regards it as a string. Think about the printed result above.

 

 

At any time, we can addNote. Annotations are used to describe the code. They can be viewed by yourself or others. When the program is running, the Python interpreter directly ignores annotations. Therefore, whether Annotations do not affect the execution result of the program, but it affects whether others can understand your code.

Python annotations # Start with, followed by the text until the end of the line is calculated as a comment

# This line is all annotated...
Print 'hello' # this is also a comment

Another clever use of annotations is that some code we don't want to run, but we don't want to delete it, so we can temporarily block it with annotations:

# Do not want to run the following line of code for the moment:
# print 'hello, python.'

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.