Python3 Basic data types

Source: Internet
Author: User

? Variables in Python do not need to be declared, each variable needs to be assigned before it is used, and the variable will not be created until the variable is assigned.

In Python, a variable is a variable, it has no data type, and what we call "type" is the type of object in memory that the variable refers to.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable. For example:

# !/usr/bin/python  = += 100.10"mr.ding"print(NUM1)  Print(num2)print(name)

Executing the above program will output the following results:

100100.1mr.ding

Assign values to multiple variables:

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

A = b = c = 1

The above example creates an integer object with a value of 1, a value from the back forward, and three variables pointing to the same memory address.

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

" mr.ding "

In the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "Mr.ding" is assigned to variable C.

Standard data types

There are six standard data types in the Python3:

? Number (numeric)

? String () strings)

? List (lists)

? Tuple (tuple)

? Set (SET)

? Dictionary (dictionary)

Of the six standard data types in Python:

Immutable Data (3): umber (numeric), string (), tuple (tuple),

variable data (3): List, set (collection), Dictionary (dictionary)

Number (numeric)

Python 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.

Print (Type (a), type (b), type (c), type (d))<class'int'> <class  'float'> <class'bool'> < class ' Complex '>

In addition, isinstance can be used to judge:

>>> a = 11>>> isinstance (A, int) True

The difference between isinstance and type is:

class A:     Pass class B (A):     Pass Isinstance (A (), a)   # returns Truetype (A ()) = = a      #  returns Trueisinstance (B (), a)    # returns Truetype (B ()) = = 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.

Numeric operations

Instance:

>>>5 + 4  #  addition #  subtraction 2.3>>> 3 * 7  #  multiplication 21 >>> 2/4  #  division, get a floating-point number #  division, get an integer 0#  #  exponentiation 32

Attention:

1. Python can assign values to multiple variables at the same time, such as a, B = 1, 2.
2. A variable can point to different types of objects by assigning values.
3. The Division of numeric values contains two operators:/returns a floating-point number,//returns an integer.
4. In mixed calculations, Python converts an integer to a floating-point number.

Numeric type instances

String (String)

The strings in Python are enclosed in single quotation marks or double quotes, with backslashes \ Escaping special characters.
The syntax for intercepting a string is as follows:

Variable [head subscript: Tail subscript]

The index value starts with 0, and 1 is the starting position from the end.

The Plus + is the string's connector, and the asterisk * indicates that the current string is copied, followed by the number of copies. Examples are as follows:

#!/usr/bin/pythonStr='mr.ding'Print(str)#Output StringPrint(Str[0:-1])#outputs all characters from the first to the penultimatePrint(Str[0])#The first character of the output stringPrint(Str[2:5])#outputs the characters from the third start to the fifthPrint(str[2:])#output all characters from the beginning of the thirdPrint(STR * 2)#output String two timesPrint(str +"TEST")#Connection String

Execute the above program output results as follows:

Mr.DingMr.DinM.Di.DingMr.DingMr.DingMr.DingTEST

Python uses a backslash (\) to escape a special character, and if you do not want the backslash to escape, you can add an R to the string before it, representing the original string:

Print ('mrd\ning') mrding Print (R'mrd\ning') mrd\ning

In addition, a backslash (\) can be used as a continuation character, indicating that the next line is a continuance of the previous row. You can also use "" "," "" "" "" "or" "" ... "across multiple lines.

Note that Python does not have a separate character type, and one character is a string of length 1.

' mr.ding ' Print (Name[0], name[6]) M g Print (Name[-1], name[-3]) G I

Unlike the C string, the Python string cannot be changed. Assigning a value to an index location, such as word[0] = ' m ', results in an error.

Attention:

1, the backslash can be used to escape, using R can make the backslash does not escape.
2. Strings can be concatenated with the + operator and repeated with the * operator.
3. The string in Python is indexed in two ways, starting at 0 from left to right and starting from right to left with-1.
4. The string in Python cannot be changed.

Python3 Basic 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.