"Python core Programming" sequence: strings, lists, and tuples Python:attribute Error-' Nonetype ' object has no Attribute

Source: Internet
Author: User
Summary:In Python, there is a data type whose members are ordered and can be accessed by subscript offsets, called "sequences," including strings, lists, tuples. We learn these sequences mainly from the introduction, operators, built-in functions, features, related modules and so on.

Source: Python Core programming Sequence: strings, lists, and tuples
1. Sequence

sequence type operator:
Seq[index],seq[index1:index2],seq*expr,seq1+seq2,obj in seq,obj not in SEQ


sequence Type factory conversion function:
List (ITER), str (obj), Unicode (obj), basestring (), tuple (ITER)


available builtin functions for sequence types

Enumerate (ITER)
Len (seq)
Max (Iter,key=none) or Max (Arg0,arg1...,key=none)
Min .....
Reverse (SEQ)
Sorted (Iter,func=none,key=none,reverse=false)
SUM (seq,init=0)
Zip ([it0,it1...intn])


View Two examples:
1)
s= ' Foobar '
For I,t in enumerate (s):
Print I, t
0 F
1 o
2 O
...
2) Zip ()
S,t= ' Foa ', ' OBR '
Zip (s,t)

[(F,o), (O,b), (A,R)]



2. String

member Operator:In,not in


character conversion function:ASCII corresponds to the conversion between the character and the value
>>>CHR (65)
A
>>>ord (' a ')
97
String-built functions: see String class
Note thatthe string in Python is not used as the Terminator, nor do we need to consider the end of the string.

In addition, in Python, strings are a type that cannot be changed. You can only change the string corresponding to a variable by recreating the string object.


3. List


3.1) Basic Operation
Create: []
Visit: List[index]
Update: Append

Delete: Del\remove


List creation:

Sample_list = [' A ', 1, (' A ', ' B ')]

Num_inc_list = Range (30): Creating a list

Sample_list = [Initial_value for i in range (10)]: Create a list

Sample_list = [initial_value]*list_length: Create a list of lengths

List=list (tuple): Creating a list using collection objects


List access:

List_length = Len (sample_list): Get list length


List Update:


List.extend ([1,2,3]): Extended list

List=list1+list2: Connection to List

List.insert (0, "start"): Inserts an element into the list

List.reverse (): List inversion

List.sort (): List sort

List.rsort (): List sort

list[i:j]=[2,3,4]: List index assignment

List deletion:


Del Sample_list[0]: Delete a list element

List.pop (): list Deletes last element

3.2 operator standard operator:> < = =
Sequence type operators: slices [::], [:], [], Member relation operator (in,not in); link operator (+); repeat operator (*)
List type operators and List resolution: []

3.3) built-in function
Standard type functions: CMP ()
Sequence type functions: Len (), Max (), Min (), sorted (), reversed (), enumerate (), zip (), sum (), list (), tuple

List type Built-in functions: Range ()


List functions

Role

List.append (obj)

Add an object to the list obj

List.count (obj)

Returns the number of times an object obj appears in the list

List.extend (seq) a

Add the contents of the sequence SEQ to the list

List.index (obj, i=0, J=len (list))

Returns the K value of list[k] = = obj, and the range of K is i<=k<j; otherwise the ValueError exception is thrown

List.insert (index, obj)

Inserts an object obj at the position index

List.pop (index=−1) A

Deletes and returns the object at the specified location, which defaults to the last object

List.remove (obj)

Remove Object obj from the list

List.reverse ()

Flip List in situ

List.sort (Func=none, key=none,reverse= False) b

Sorts the members in the list in the specified manner, if the Func and key arguments specify, the elements are compared in the specified manner, and if the reverse flag is set to True, the list is sorted in reverse order

Attention: The method of changing objects that can change the value of an object has no return value; instead, the corresponding string method has a return value because the string is immutable. Do not use List=list.reverse (), because so you will get the list is none.


Possible errors:

If in Python programming, get the error: Python:attribute error-' Nonetype ' object has no Attribute reason: the list after the operation of the return value, re-assign to the LIS T
3.4) The specificity of the list

The list has container-variable features that you can use to build stacks and queues.


3.5) and the conversion of string

String into List:list (mystring)

List into string:string.jion (MyList, "") 4. Tuples

Overview: The creation, deletion, access, and update of tuples.


tuple operators and built-in functions

Create, repeat, connect

Member relationships, slices

Built-in functions

Operator

Like a list, Ganso does not have its own proprietary operators and built-in functions. In addition, sort, append, and so on, because tuples are immutable, these operations are not feasible for tuples.

In addition, a tuple is immutable, but it can perform a "repeat" operation, and the tuple is not, but the mutable objects contained in the tuple are mutable.


All of the multiple-object, comma-separated, undefined-defined, default types of these collections are tuples.

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.