Python data type

Source: Internet
Author: User

  

1. Data type

There are four types of Python: integers, long integers, floating-point numbers, and complex numbers.

    • integers, such as 1
    • Long integers are relatively large integers.
    • Floating-point numbers such as 1.23, 3E-2
    • Complex numbers such as 1 + 2j, 1.1 + 2.2j

2. natural string, by adding R or R before the string. such as R "This was a line with \ n \" will be displayed, not newline.

Note: If R or R is not preceded, \ n means line break, and if there is R or R, it will print normally \ n

This was a line with \ n
>>> A = ' This is boyfriend \ n this is me '
>>> Print (a)
This is boyfriend
This is Me
>>> A = R ' This is boyfriend \ n this is me '
>>> Print (a)
This is boyfriend \ n this is me
>>>

3. Blank Line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.

Blank lines are different from code indentation, and empty lines are not part of the Python syntax. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.

Remember: blank lines are also part of your program code.

4.Print output

Print default output is newline, if you want to implement no line break need to add end= ""at the end of the variable: or end= '

>>> x = ' a '; y = ' B '
>>> Print (x)
A
>>> Print (y)
B
>>> print (x,end = ");p rint (y,end =")
Ab
>>> print (x);p rint (y)
A
B
>>>

5.import and From...import

Import the appropriate modules in Python using Import or from...import.

Import the entire module (somemodule) in the format: import somemodule

Import a function from a module in the format: from somemodule import somefunction

Import multiple functions from a module in the format: from somemodule import Firstfunc, Secondfunc, Thirdfunc

Import all functions in a module in the format: from somemodule import *

Importing the SYS moduleImport Sys Print(‘================python Import mode==========================‘);Print (‘The command line arguments are:‘) for i in sys argv: print (i) print ( ' \n python path is ' ,sys. Path Importing the Argv,path member of the SYS moduleFrom Sys import argv< Span class= "Hl-code" >,path # import specific members Span class= "Hl-code" > print ( ' ================python from Import=================================== print ( Span class= "hl-quotes" > ' path: ,path) # because the path member has already been imported, So you don't need to add Sys.path

6. Assigning values

Assigning values to multiple variables

Python allows you to assign values to multiple variables at the same time. For example:

= = =1  

The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.

You can also specify multiple variables for multiple objects. For example:

A, b,=1,2,"Runoob"

7.
standard data types, Determine its type with type (a variable name)

>>> A = 111
>>> isinstance (A,int)
True

You can also use Isinstance (A,int)

There are six standard data types in the Python3:

    • Number (numeric)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Sets (collection)
    • Dictionary (dictionary)
Number (numeric)

Python3 supports int, float, bool,complex (plural).

In Python 3, there is only one integer type int, represented as a long integer, and no long in Python2.

Like most languages, the assignment and calculation of numeric types is straightforward.

The built-in type () function can be used to query the object type that the variable refers to.

>>>A,B,C,D= 20, 5.5, True, 4+3j>>> Print(Type(A),Typeb Type ( c),  Type (d <class ' int ' > <class< Span class= "PLN" >  ' float ' > <class  ' bool ' ><class  ' complex '  


the difference between 8.isinstance and type is:
ClassA:    PassClassB(A): passisinstance (a< Span class= "pun" > (),  A)  # returns truetype (a ()) == A # returns trueisinstance b (),  A)  # returns truetype (b< Span class= "pun" > ())  == A # returns False   

The difference is:

    • Type () does not consider a subclass to be a parent class type.
    • Isinstance () considers a subclass to be a parent class type.

Note: There is no Boolean in Python2, which represents False with the number 0 and 1 for True. To Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to the numbers.








Python data type

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.