Python Basic data type

Source: Internet
Author: User
Tags truncated

Variables in Python do not need to be declared. Each variable must 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 type, and what we call "type" is the type of object in memory that the variable refers to.

1. Standard data type

There are six standard data types in the Python3:

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

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.

1.2String (String)

The strings in Python are enclosed in single quotation marks (') or double quotation marks ("), and the special characters are escaped with a backslash (\).

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 sign (+) is the string's connector, and an asterisk (*) indicates that the current string is copied, followed by the number of copies

Note: 1, unlike the C string, a Python string cannot be changed.

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

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

1.3List (list)

The list is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).

A list is a comma-delimited list of elements written between square brackets ([]).

As with strings, lists can also be indexed and truncated, and a new list containing the required elements is returned when the list is truncated.

The syntax format for the list interception 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 sign (+) is the list join operator, and the asterisk (*) is a repeating operation.

Note: 1. Unlike the python string, the elements in the list can be changed

2, List has a lot of methods, such as append (), pop () and so on, which will be discussed later

Summary:

    • 1. The list is written between square brackets, and the elements are separated by commas.
    • 2, like a string, a list can be indexed and sliced.
    • 3, list can use the + operator for stitching.
    • 4, the elements in the list can be changed.
1.4Tuple (tuple)

Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified . Tuples are written in parentheses (()), and the elements are separated by commas.

The elements in a tuple can also be of different types:

Tuples are similar to strings, can be indexed, and subscript indexes start at 0, and 1 is where they start at the end of the list. It is also possible to intercept (see above, not repeat here).

In fact, a string can be thought of as a special tuple.

Attention:

1. Although the elements of a tuple are immutable, it can contain mutable objects, such as list lists.

2. Constructs a tuple with 0 or 1 elements is special, so there are some additional syntax rules:

=()    # empty tuple =(A,)# An element that needs to be added with a comma after the element      

String, list, and tuple all belong to sequence (sequence).

3. As with strings, the elements of a tuple cannot be modified.

4. Tuples can also be indexed and sliced, in the same way.

5. Note A special syntax rule that constructs a tuple that contains 0 or 1 elements.

6. Tuples can also be spliced using the + operator.

1.5Set (Collection)

A collection (set) is a sequence of unordered, non-repeating elements.

The basic function is to test the membership and remove duplicate elements.

You can use braces ({}) or set () functions to create a collection, Note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.

Student= {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}Print(Student) # output set, duplicate elements are automatically removed# member TestIf(' Rose ' InchStudent) : Print(' Rose in the collection ')Else :Print(' Rose is not in the set ')# set can perform set operationsA= Set(' Abracadabra ')B= Set(' Alacazam ')Print(A)print (a -  B)  # A and B difference set  print (a | b )  # A and B in the same set print (a & B)  # A and B intersection print (a ^ B)  # Elements in A and b that do not exist at the same time                 

The result of the above example output:

{' Jack ', ' Rose ', ' Mary ', ' Jim ', ' Tom '}Rose In the collection{' R ', ' B ', A, C, ' d '}{' R ', ' B ', ' d '}{A, L, ' Z ', ' B ',  ' m ' ,  ' d '    ' R ' ,  ' C ' }{ ' a ' ,  ' C ' }{ ' l ' ,  ' z ' ,  ' B ' ,  ' m ' ,  ' d ' ,  ' R ' }    
1.6Dictionary (dictionary) (dictionary)

The Dictionary (dictionary) is another very useful built-in data type in Python.

A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. the difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

A dictionary is a type of mapping that is identified by the dictionary with "{}", which is an unordered key (key): The value pair collection .

The key (key) must use the immutable type. (Tuple, String)

In the same dictionary, the key must be unique.

Special: Tinydict = {' name ': ' Runoob ', ' name ': 1, ' site ': ' www.runoob.com '};print (tinydict)

Output {' site ': ' www.runoob.com ', ' Name ': 1}

Like Java, the back will overwrite the front, and, from the back to the forward output??

1.7Python Data type Conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

function Description

int (x [, Base])

Convert x to an integer

Float (x)

Convert x to a floating-point number

Complex (real [, Imag])

Create a complex number

STR (x)

Convert an object x to a string

REPR (x)

Convert an object x to an expression string

eval (str)

Used to evaluate a valid Python expression in a string and return an object

Tuple (s)

Converting a sequence s to a tuple

List (s)

Convert the sequence s to a list

Set (s)

Convert to mutable Collection

Dict (d)

Create a dictionary. D must be a sequence (key,value) tuple.

Frozenset (s)

Convert to immutable Collection

Chr (x)

Converts an integer to one character

UNICHR (x)

Converts an integer to a Unicode character

Ord (x)

Converts a character to its integer value

Hex (x)

Converts an integer to a hexadecimal string

Oct (x)

Converts an integer to an octal string

Python Basic data type

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.