Python Basics-Basic data types (list)-Variable data

Source: Internet
Author: User
Tags truncated python list

Python Basics-Basic data type (number (numeric) string (string) list (list) Tuple (tuple) sets (collection) Dictionary (dictionary))

There are six standard data types in the Python3:
Number (numeric)
String (String)
List (lists)
Tuple (tuple)
Sets (collection)
Dictionary (dictionary)

Among the six standard data types of Python3:
Immutable data (four): Number, String (string), tuple (tuple), sets (set);
Variable data (two): List, Dictionary (dictionary).


Python3 Basic data types
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.
Variable = object

1. Assigning 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 and three variables pointing to the same memory location.

2. You can also specify multiple variables for multiple objects.
For example:
A, b, C = 1, 2, "Runoob"
For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "Runoob" is assigned to the variable C.



I. List (lists)
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 ([]).
List1 = [' Google ', ' Runoob ', 1997, 2000];
List2 = [1, 2, 3, 4, 5];
LIST3 = ["A", "B", "C", "D"];


1, like a string, the list can also be indexed and truncated, and the list is truncated to return a new list containing the required elements.
The syntax format for the list interception is as follows:
Variable [head subscript: Tail subscript]
Left-to-right: The index value starts with 0;
Right-to-left: The index value starts with 1.
The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation.

2. Update the list
You can modify or update data items in a list, or you can use the Append () method to add list items.
list = [' Google ', ' World ', 1997, 2000]
LIST[2] = 2001


3. Delete list elements
You can use the DEL statement to delete the elements of a list.
list = [' Google ', ' World ', 1997, 2000]
Del List[2]

4. Python list function

Serial Number function
1 Len (list) List element number
2 Max (list) returns the maximum value of the listing element
3 MIN (list) returns the minimum value of the list element
4 List (seq) converts tuples to lists



5. Python contains methods

Serial Number Method
1 List.append (obj) adds a new object at the end of the list
2 List.count (obj) counts the number of occurrences of an element in a list
3 List.extend (SEQ) appends multiple values from another sequence at the end of the list (the original list is expanded with a new list)
4 List.index (obj) find the index position of the first occurrence of a value from the list
5 List.insert (index, obj) inserts an object into the list
6 List.pop ([index=-1]]) removes an element from the list (the last element by default) and returns the value of the element
7 List.remove (obj) removes the first occurrence of a value in a list
8 List.reverse () reverse list of elements
9 List.sort (Cmp=none, Key=none, Reverse=false) sort the original list
10 List.clear () empty list
11 List.copy () copy list




Attention:
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.


Python Basics-Basic data types (list)-Variable data

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.