Python data types and syntax

Source: Internet
Author: User

Basic data types

Python has five standard data types:

    • Numbers (digital)

    • String (String)

    • List (lists)

    • Tuple (tuple)

    • Dictionary (dictionary)

The shell seems to have no obvious data type to differentiate

Python supports four different numeric types:

    • int (signed integral type)

    • Long (longer integer [can also represent octal and hexadecimal])

    • Float (float type)

    • Complex (plural)

The following highlights the three data types of strings, lists, tuples and dictionaries

string

The focus is on the interception function:

#!/bin/python#string testingstr= "AABBCCDD" Print (str) print (str[0]) print (Str[2:5]) print (str*2) print (str+ "Join test ")

Output:

Aabbccdd

A

Bbc

Aabbccddaabbccdd

Aabbccddjoin Test

Kerry

Eva

111

222

The shell string is truncated as follows:

Start with the first few characters on the left, and the number of characters
Echo ${var:0:5}
Where 0 means the first character on the left begins, and 5 indicates the total number of characters

Only the number, not the end index value, and the shell can also intercept according to #,%,##,%%.

the first character on the left side of the shell is denoted by 0, and the first character on the right is denoted by 0-1 .

In Python, the first one on the left is 0, the first on the right is-1

< Span style= "Color:rgb (70, 70, 70); Font-family: Song body; font-size:14px; line-height:21px; Background-color:rgb (211, 235, 239); " > Starting from the first few characters on the right, and the number of characters echo ${var:0-7:3}

List

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

A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting).

The list is identified by []. Is the most common composite data type of Python. See this code to understand.

The list of values can also be used to split the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.

The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation. The following example

[Email protected] ~]$ VI testall.py #!/bin/pythonname=["Kerry", "Eva", 111,222]name2=["Eva", 333]for element in Name:pri NT (Element) print ("All elements:"), Nameprint ("The second element was:"), Name[2]print ("The 1,3,sub is:"), Name[1:3]// Open interval Print ("Double:"), Name*2print ("Join:"), name+name2


Python data types and syntax

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.