Python Complex data types

Source: Internet
Author: User

What are the complex data types?
What are the characteristics?
What are the usage scenarios?
What is the difference between a list and a tuple? Why are there two types of data?
Why can lists and tuples hold data that cannot be typed?
What is a factory function?

String
Characteristics:
1. There is no character type in Python so the definition string can be enclosed in double or single quotes
2. String is immutable type
3. Three quotation marks can contain complex strings

        >>> a= "        ... 1 ...        2 ...        3 ...        "        >>> A        

Use:
1. Accessing by index
>>> a= ' ABCD '
>>> A[0]
A
>>>
2. Slicing
>>> a= ' ABCD '
>>> A[1:3]
' BC '
3. String "Multiply"
>>> a= ' ABCD '
>>> a*2
' ABCDABCD '
4. Last character
>>> A[-1]
' d '
List
Characteristics:
1. The list can contain different types of objects
>>>list=[1,1.1, ' abc ', 21+3J]
[1, 1.1, ' abc ', (21+3J)]
2. The list is a mutable type
Create a list object to manipulate it without allocating additional memory
Use:
1. The list can contain loops
>>> list=[x*2 for x in range (4)]
>>> List
[0, 2, 4, 6]
2. List Build Stack

        >>> stack = [3, 4, 5]        >>> stack.append (6)        >>> stack.append (7)        >>> Stack        [3, 4, 5, 6, 7]        >>> stack.pop ()        7        >>> Stack        [3, 4, 5, 6]        >>> Stack.pop ()        6        >>> stack.pop ()        5        >>> Stack        [3, 4]


3. List build queue, the double-ended queue used here deque

        >>> from collections import deque        >>> queue = deque (["Eric", "John", "Michael"])        >>> Queue.append ("Terry")           # Terry arrives        >>> queue.append ("Graham")          # Graham arrives        >> > Queue.popleft () # The first to                 arrive now leaves        ' Eric '        >>> queue.popleft ()                 # the second T O Arrive now leaves        ' John '        >>> queue                           # Remaining queue in order of arrival        deque ([' Michael ' , ' Terry ', ' Graham ')


Meta-group
Characteristics:
1. Similar to list, use parentheses
2. Immutable types
3. Cannot create a single element tuple
>>> tuple= (1)
>>> type (tuple)
<type ' int ' >
List vs tuples
Their difference is that the list is variable type and the tuple is immutable, and the list is represented by [] for tuples ()
Indicates that different usage scenarios use more appropriate types, so lists and tuples are needed,
If you define sensitive data and do not want to be tampered with tuples, define Dynamic Data available lists
Dictionary
Dictionary is the only type of mapping in Python
>>> dict={' key1 ': ' value1 ', ' Key2 ': 2}
>>> Dict
{' Key2 ': 2, ' key1 ': ' value1 '}
Characteristics:
1. Variable type
Collection
Mathematically sets the set as a set of different elements, which is a set of unordered hash values
>>> s=set (' python ')
>>> s
Set ([' H ', ' o ', ' n ', ' P ', ' t ', ' Y '])
Use:
1. Repeating elements in the list
>>> a=[1,1,2,3,4]
>>> B=set (a)
>>> b
Set ([1, 2, 3, 4])
>>> c=[i for i in B]
>>> C
[1, 2, 3, 4]

About Factory methods:
The factory method pattern is an object-oriented design pattern that realizes the concept of "factory". Just like the other creation patterns,
It is also an issue in which objects are created without specifying object-specific types.
The essence of the factory method pattern is "to define an interface to create an object, but let the class implementing the interface decide which class to instantiate."
The factory method defers instantiation of a class to a subclass.

public abstract class Pizzastores () {public Pizza Orderpizza (String type) {Pizza Pizza;        The subclass of Pizzastore handles the instantiation Pizza=createpizza (type) of an object in the Createpizza () method;    ... return pizza; }//Now, the responsibility to instantiate the pizza is moved to a "method", the secondary method is like a "factory" protected abstract Pizza Createpizza (String type); 

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.