Chapter III Python data types

Source: Internet
Author: User

3.1 Python simple data type

3.1.1 String (str)

The strings in Python are usually composed of single quotes, double quotes, three single quotes, or a string of characters surrounded by three double quotes.

1. Escaping a string

\ n line break \ t tab

\ r enter \ \ ' \ ' character

Single quotation marks in a single quote string

\ "Double quotation marks in a double-quoted string

2. String Operations

In Python, you can use the "+", "*" Operators.

+ Connection String * Concatenate a single string multiple times

3. String handling functions

Common String Functions
String manipulation Describe
String.capitalize () Capitalize the first letter of a string
String.count () Get the number of a substring in a string
String.find () Get the starting position of a substring in a string, none returns-1
String.isalnum () Detects if a string contains only 0-9a-za-z
String.isalpha () Detects if a string contains only a-za-z
String.isdigit () Detects if a string contains only numbers
String.islower () Detect if the string is both lowercase letters
String.isspace () Detects whether all characters in a string are white-space characters
String.istitle () Detects whether a word in a string is capitalized in the first letter
String.isupper () Detect if a string is all uppercase
String.Join () Connection string
String.Lower () Converts all characters of a string to lowercase
String.Split () Split string
String.swapcase () Capitalize characters in a string, lowercase, and uppercase
String.title () Capitalize the first letter of a word in a string
String.upper () Converts all characters in a string to uppercase
Len (String) Gets the length of the string

4. Chinese character processing

Encode (encoding= ' utf-8 ', errors= ' strict ')

3.1.2 Integer (int)

3.1.3 Floating point (float)

3.4 Python structure data type

3.4.1 (list)

A list is the most common type of data that can be put together in a large amount of data and can be centrally processed.

It is not only easy to do data processing, but also to reduce the number of variables declared.

A list is a collection of data surrounded by square brackets "[]", separated by "," between different members. The list can contain any data type.

list.append (x)
list.count (x) Returns the number of occurrences of the parameter X in the list
list.extend (L) append another list to the list L
list.index (x) Returns the ordinal number of the parameter X in the list (error if x does not exist)
List.insert (index,object)
list.pop ()
list.remove (x)

Delete the specified member in the list (only the first one is deleted)

Specify that the member does not exist error

list.reverse ()
list.sort ()

3.4.2 tuples (tuple)

Tuples can be seen as a special list, unlike lists where tuples cannot be changed once they are established. You cannot change the data items in them, nor can you add and delete data items.

Therefore, if you want a set of data that cannot be changed, put them into a tuple, and any attempt to modify the tuple will occur incorrectly.

Create a tuple of only one element, followed by an element with a ",".

3.4.3 Dictionary (dict)

Dictionaries are a special type of data types in Python, and each member in the dictionary exists in the form of a key: value pair.

A member can only be accessed through a key in a dictionary, not through its location.

Dictionary Operation function Table
Dic.clear () Empty dictionary
Dic.copy () Copy Dictionary
Dic.get (K,[default]) Gets the value corresponding to the key K, which does not exist returns the default
Dic.items () Get an iterator that consists of a key and a value
Dic.keys () Get the key iterator
Dic.pop (k) Delete K:V member pairs
Dic.update (adict) Update a member from another dictionary (no existing is established, overwrite exists)
Dic.values () Iterator that gets the value
Dic.fromkeys (Iter,value) Creates a dictionary with the given key in a list or tuple, which defaults to value
Dic.popitem () Remove any k:v from the dictionary and return it
Dic.setdefault (K,default) If a key value of K is present in the dictionary, the corresponding value is returned: Otherwise, a dictionary K:default member is created in the dictionary

3.5 sequence

3.5.1 sequence Slices

Suppose there is a sequence of length n, with a left-to-right ordinal of 0....n-1. Then there is another ordinal representation: Right to left for -1.....-n

Common methods of slicing
alst[:] Fetch all member Data items
Alst[0:] Fetch all member Data items
ALST[:-1] Take all member data items except the last member
Alst[2:5] Get [2,3,4]
ALST[::2] Take one member at every 1, get [0,2,4,6]
Alst[0:5:2] Take one item from 0 to 4 every other, get [0,2,4]
ALST[::-1] Take all members from right to left
Alst[5:0:-2] From 5 to 0 (not including 0), take one item every 1, get [5,3,1]

Chapter III Python data types

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.