Getting Started with Python (ii): Data types in Python, print statements, annotations

Source: Internet
Author: User

A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:

One, integer

Python can handle integers of any size, including, of course, negative integers, and in a Python program, integers are represented in exactly the same way as mathematically, for example: 1,100,-8080,0, and so on.

Computer because of the use of binary, so, sometimes hexadecimal notation is more convenient, hexadecimal with 0x prefix and 0-9,a-f, such as: 0XFF00,0XA5B4C3D2, and so on.

Second, floating point number

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x10^9 and 12.3x10^8 are equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x10^9 is 1.23e9, or 12.3e8, 0.000012 can be written 1.2e-5, and so on.

Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.

Three, string

strings are arbitrary text enclosed in ' or ', such as ' abc ', 'xyz ', and so on. Note that the' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters.

Four, Boolean value

The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to indicate a Boolean value directly with True, false (note case), or by Boolean operations.

Boolean values can be operated with and, or, and not.

The and operation is associated with an operation, and only the result of all true,and operations is True.

An OR operation is an operation, as long as one of the true,or operation results is True.

The not operation is a non-operation, which is a single-mesh operator that turns true to False,false to true.

Five, null value

The null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.

The print statement can output the specified text to the screen. For example, the output of ' Hello, world ', implemented in code as follows:

>>> print ' Hello, world '

Attention:

1. When we write code in a python interactive environment, >>> It is a prompt for the Python interpreter, not part of the code.

2. When we write code in a text editor, never add >>> yourself.

The print statement can also be followed by multiple strings, separated by a comma "," that can be connected to a string of outputs:

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

Print prints each string sequentially, encounters a comma "," and outputs a space, so the output string is spelled like this:

Print also prints integers, or calculates the result:

>>> Print 300
#运行结果
>>> Print 100 + 200
#运行结果

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

>>> print ' 100 + 200 = ', 100 + 200
+ #运行结果

Note: for the + 200,python interpreter automatically calculates the result 300, but, ' 100 + 200 = ' is a string rather than a mathematical formula, and Python treats it as a string, consider the above printed result.

At any time, we can add comments to the program. Annotations are used to illustrate the code, to yourself or others, and when the program runs, the Python interpreter ignores comments directly, so there is no comment that doesn't affect the execution of the program, but it affects whether someone can read your code.

Python comments begin with the  #  text followed by a comment until the end of the line

# This line is all a comment ...
print ' Hello ' # This is also a comment

Note there is also a clever use, that is, some code we do not want to run, but do not want to delete, you can temporarily block out the comment:

# temporarily do not want to run the following line of code:
# print ' Hello, Python. '

Getting Started with Python (ii): Data types in Python, print statements, annotations

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.