[Python ③] basic python data types, variables and constants, python

Source: Internet
Author: User
Tags floor division

[Python ③] basic python data types, variables and constants, python
Basic Data Type

In Python, the following types of data can be processed directly:

Integer

Python can process Integers of any size, including negative integers. The writing method in the program is the same as that in mathematics, for example: 6,-666, 8888 ......

The computer uses binary. Therefore, it is convenient to use hexadecimal to represent integers.0xPrefix and 0-9, a-f, such

>>> 0xaa6643622>>> 0xaf56dc11491036

 

Floating Point Number

Floating Point number, that is, decimal number. Floating point numbers can be written in mathematics, such0.681,-6.58 ...... HoweverA large or small floating point number must be represented by the E notation (Scientific Notation), replacing 10 with e, and 3.5x109 is 3..5e9。 

Integers and floating-point numbers are stored in different computers. Integer operations are precise, while floating-point operations may have rounding errors.

Boolean Value

A boolean value onlyTrue,FalseTwo values, Boolean value can be usedand,orAndnotOperation.

String
>>> 'Let\'s go!'"Let's go!"

Escape characters\Can escape many characters, such\nLine feed,\tTab, Character\It also needs to be escaped, so\\The character is\。

>>> Print ('newline \ nTab \ t \ ') newline Tab \

Python is also allowedr''Indicates''Internal strings are not escaped by default.

>>> Print (r'newline \ nTab \ t \ ') newline \ nTab \ t \\

Python is also allowed'''...'''The format of '\ n' indicates multiple lines of content, which simplifies a bunch of' \ n' and likes one.

>>> Str = ''' the spring-blown petals are not produced for future fruits, but just for the sake of Instant happiness. -- The stream collection ''' >>> str 'petals blowing in spring, \ n is not born for the future fruit, \ n is just for the moment. \ N -- the stream collection of Tagore >>> print (str) Spring is blowing the petals, not for the future fruit, just for the sake of a moment. -- Tagore stream collection
Null Value

A null value is a special value in Python.None.NoneCannot be understood0Because0It makes sense.

 

Variable

Python variables do not need to be declared. You can enter them directly:

 

>>> py=6.88>>> print(py)6.88>>> print(type(py))<class 'float'>

 

 

 

Then you have a variable py in your memory. Its value is 6.88, and its type is float (floating point number ). Before that, you do not need to make any special declaration, but the data type is automatically determined by Python.

 

Here, we learned a built-in function type () that can query the type of a variable.

 

Constant

 

A constant is a variable that cannot be changed. For example, a common number π is a constant. In Python, variable names in all uppercase are usually used to represent constants, PI, and so on.

 

Small Division

The division of integers in python3.X is different from that in 2.x.

In 3.x(Real Division: returns true values regardless of integer or floating-point type ):

>>> 5/22.5>>> 5.0/22.5>>> 5/2.02.5>>> 5.0/2.02.5

 

2. X(Traditional Division ):

>>> 5/22>>> 5.0/22.5>>> 5/2.02.5>>> 5.0/2.02.5

 

>>> From _ future _ import division # precise division >>> 5/22. 5

 

Python OPERATOR:

(1): single object OPERATOR: positive (+), negative (-)

(2): binocular operators: +,-, *,/, % ,**,//

 

About floor Division(Based on the Python Version, the Python Version <= 2.6 is the traditional division: INTEGER: removes the decimal part, and returns an integer ):

>>> 5//22

 

Summary of the day

Understand the basic data types, constants, and variables of python. Assign values to variables, and the difference between division in python2.X and 3.x.

 


Python, what are Python constants and variables?

There is no constant in the world of python, and only variables that you take the initiative not to modify are disguised as constants.

How does one define the data type in python, that is, declare the variable type?

This is not a declaration type problem, because a and B are already int when you call, so the number to be divided is only an integer. If you declare C in time, the result is only 0.0.
Suggestion:
Def test (a, B ):
A1 = float ()
B1 = float (B)
C = a1/b1
Return c

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.