Explanation of five data types in the basic python tutorial

Source: Internet
Author: User
This article mainly introduces the details of the five data types in the basic python tutorial. here we will introduce the Python data types in detail. For more information, see Five types of Python data

When learning a language, you must first obtain the data types it owns. Python has five main data types, the following describes my understanding and thoughts on these five data types.

1. number

There are four types of numbers in Python: int (integer), float (floating point number), long (long integer), and complex (plural)

In particular, there is a function of the float type, round (), which can be rounded up: round (a, B): This operation is performed on the float type value, valid number B is retained after the decimal point. The default value is 1.

The complex type is also quite special.

2. string

For example, s = 'string' s = ''string'' s1 = ''string''. The three effects are the same. in Python, quotation marks, double quotation marks, and three quotation marks indicate the correct usage of strings.

Strings in Python can be directly added: s + s1 # A new string 'stringstring' is returned'

Next we can perform the "slice" operation on the string. The so-called slice is equivalent to cutting a piece of bread. For example, if we want to take the 2nd to 5th characters in the s string, it is troublesome in other languages. in Python, we can perform this operation conveniently.

For example, s [a: B: c] a indicates the starting position of the slice. if the value is 0 or positive, the index starts from left to right (from 0 by default ), if the value is negative, the index starts from right to left (from-1 by default)
B indicates the position at which the slice ends, but does not include the position at which the slice ends ". The default value is "no" until the index ends.
C indicates the step size. the default value is 1. if it is a negative number, it is truncated from right to left.

If there is no colon, it is a normal index operation: s [0] # s

The default value of c is 1: s [] # trin (pay attention to "ignore head tail ")
S [-3:]: starts from the third character on the right (no more than 0th characters !!!), Truncate to the right until the end # ing
S [-3:-1]: starts from the third character on the right and truncates from the left. B defaults to: until the end # irts

Now I have an understanding of simple slice operations. let's talk about several common functions (there are actually a lot of operation functions, but some of them are often not used. if you need them, you can get to know them again)

Len (): returns the string length. Len (s) # pytnon is different from C. You do not need to add 1 to the string length. Here is 6.

Replace (a, B): replace string a with string B.

3. List)

Example: s = ['string', 'Python', 2001, 52.5], s1 = []

Python contains the List type in square brackets, which can contain the string type and the number type, separated by commas.

Access operation in List: s [1] # returns a python string. Comparable to the string type

List also has the update and delete operations: s [1] = 2002 # The first element in List s (starting from 0) the 'python' string is replaced with 2002.

Del s [1] # The first element of list s is deleted.

The following describes the functions and methods of several operations:

1. append () # append an element after the list
2. extend () example: a. extend (B) # add the elements in list B to the end of List
3. pop () # bring up the last element in the list

1. sort () # sorts the list, but it seems that you want to specify the sorting rule.
2. count () # count the number of times an element appears
3. index () # elements of I at the index

4. Tuple)

Example: s = ('string', 'Python', 2001) s1 = 'string', 'Python', 2001
The tuples in python are interesting. it is correct to add parentheses without parentheses. Separated by commas (,). The default value is tuples.

There is a special rule in tuples: elements in tuples cannot be modified.

The access operation can be similar to the string type.

The following are examples of tuples and lists:

1. (, 3) + (, 3) # (,) addition operation
2. [1] * 3 # [, 1] multiplication operation
3. 1 in [, 3] # true indicates the operation.
4. for I in (1, 2, 3)
Print I #1
#2
#3 Cyclic operations

5. Dictionary)

Example above: dict = {'ABC': 123, 'ji': 'KP ):
5}

For typical key-value data, note the following: the key value must be unique, but the value can be unique. Enclose in curly brackets. End with a semicolon after braces.

Access: dict ['ABC'] #123
Modify: dict ['ABC'] = 153 # modify 123 of 'ABC'
Delete: deldict ['ABC']

Special points:

1. the same key cannot appear twice. if multiple values are assigned, the subsequent values will prevail.
2. the key must be unchangeable. the available number, string, and tuples act as the key, but the list does not work!

Method introduction:

1. clear () # clear the dictionary
2. get () # value example: get ('ABC') #123 get ('ashudya') # none
3. keys () # return a list containing all the key values in the dictionary.
4. value () # return a list containing all value values in the dictionary.
5. fromkeys () # put a list in the dictionary as a key.

Fromkeys ([, 3], 0) #0 is value

The dictionary can be expressed as: {1: [0], 2: [0], 3: [0]}

Five data types are the cornerstone of learning Python, and it is not very difficult to grasp it. First, start with a simple one. if necessary, you can study it in depth!

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more details about the five types of data in the basic python tutorial, refer to the PHP Chinese website!

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.