Python Python Learning Notes

Source: Internet
Author: User

List: Lists

1) List expression: Sometimes it is convenient to replace the function to implement some functions

1.1) Center string (9 characters long) >>> a = ["123", "456", "ABC", "abc", "AAA"]>>> [K.center (9) for K in a][' 123 ', ' 4 ", ' abc ', ' abc ', ' AAA ']1.2) get a letter-only string in a, make it uppercase >>> [K.upper () for k in a If K.isalpha ()] [ ' abc ', ' abc ', ' AAA '

2) Common operation methods

add element A.append (1) Insert data at the end of the list insert data 1a.insert (0,1) at index 0 1a.extend (b) Append list B in List a delete element A.pop () Delete the last element and return its value a.remove ("a")  Delete Element A, delete from left to right, no return value other methods A.reverse () will list a reverse order A.count (3) Element 3 occurrences A.index (3) Element 3 index in the list str.replace (sub, new_sub) Replace the sub in Str with a new string new_sub

STR: String

1) Common methods for judging string categories: true return

Str.isalnum () All characters are numbers 0-9 or a-Z or A-zstr.isalpha () all characters are letters A-Z or A-zstr.isdigit () All characters are numeric 0-9str.islower () All characters are lowercase letters str.isupper () All characters are uppercase letters Str.istitle () uppercase Str.isspace () All characters are white space characters

2) Finding substrings in a string

2.1) Finds.find (Substring,[start,[end]) if found, returns the index; return-12.2) traversal string for (Index,char) in Enumerate (S): Enumerate () In each loop, a fixed value table (tuple) with two elements is returned, and two elements are assigned index and Char, respectively

3) formatting

S.ljust (width) left aligned, if s length is less than width, then s.rjust (width) Right alignment s.center (width) centered s.strip () S.lstrip () S.rstrip () Remove the space S.split () to split the string (the default is a space), and return the list result "\ n". Join ([+]) to \ n ([+)]; string connections can also use +

Tuple: Constant list

Dict: Dictionary

1) Common operations

A.clear () clears the dictionary a.copy () copy dictionary:1> b=a.copy (), at which point B is independent, change B does not affect A;2>b=a, at which point B points to the memory space value of a, so modify B will affect Aa.get (key) if key exists, return A[key]; otherwise, the return Nonea.get (Key,default) is different from the above, if the key does not exist, return to Defaulta.has_key (key) to determine whether a key is present in the dictionary; equals key in A.keys () Traverse dictionary Dic.keys (), Dic.values (), Dic.items () Get all keys, all values, all elements (key-value pairs)

Function

1) Parameter type: The value of the name (namespace) pointing to the memory address

Location pass-through, parcel transfer (*ARGS/**ARGS); Rules for key-word-pass-through parameters: first position, then keyword, parcel position, parcel key

2) Non-changeable object parameters and variable object transfer

Objects: Divided into immutable objects (int/tuple/string, etc.) and mutable objects (list/dict, etc.)

A = 1 def change_integer (a): a = a + 1 return a print Change_integer (a) print a>>> output 2 1 Immutable object parameters: A variable of the base data type, When a variable is passed to a function, the function copies a new variable in memory, without affecting the original variable b = [1] def change_list (b): b[0] = b[0] + return b print change_list (b) PR int b >>> output [2,2,3] [2,2,3] Variable object argument; table, the table is passed to the function is a pointer, the pointer to the position of the sequence in memory, the function of the table operation will be in the original memory, thereby affecting the original variable

3) Common functions

Lambda function  func = lambda x,y: x + y print func (3,4)    Higher order function: function as parameter pass  def test (f, a, b):    print  ' test '      print f (A, b)  test ((lambda x,y: x**2 + y),  3, 5)   map function: The function object is applied to the elements in the table, the first parameter is the function  re = map ((lambda x: x+3), [1,3,5,6])  re  = map ((lambda x,y: x+y), [1,2,3],[6,7,9])   filter function: filter, if the function object returns True, Then the element is stored in the returned table  def func (a):         if a  > 100:                  return True         else:                  return False          &nbSp;print filter (func,[10,56,101,500])   reduce: The first function must have 2 parameters  print reduce (( Lambda x,y: x+y), [1,2,5,7,9])


This article from "Kong Love to Eat fish" blog, declined reprint!

Python Python Learning Notes

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.