A Python syntax

First, integer

Python integers of any size can be treated, including negative integer, hexadecimal integer with a prefix 0x hexadecimal and 0-9, af represent, for example: 0xff00

Second, the float

That is decimal floating-point number can be 1.23,3.14, -9.9 ,, may also be expressed mathematically written in scientific notation, with the 10 e alternative, 1.23x10 ^ 9 12.3e8,0.000012 written or written 1.23e9 1.2e-5

Third, the string

String is '' or '' enclose any text, such as 'abc', "xyz"

Fourth, the Boolean value

Only Boolean value True, False, can be directly expressed, can be calculated by Boolean operators

Boolean values ​​can be used and, or and not operator

and operation: only if all are True, and operation is True

or operation: one of them is True, or operations on to True

not operational: True becomes False, False becomes True

Fifth, null

Python in a special value, None

Six variables

In Python, variable names must be a combination of English case, numbers and underscores, and can not begin with a number

Python is a dynamic language, you can assign the same variable repeatedly, it can be different types of variables

Seven list: list

list is an ordered set, which you can add and remove elements at any time, list of elements contained in data need not be of the same type

student=['Michale','Bob','Tracy']

L=['A',123,True]

empty_list=[]

Access list elements

   Access List by index, the index starting from 0, for example, print student [0]

   Reverse access, index starting at -1, e.g. print student [-1]

   Taken characters [index1: index2], does not include an element index index2, e.g. list = [1,2,3,4,5] print student [1: 3] Results [2,3]

list add and delete new elements:

   student.append ( 'Sam'), append () to add a new element to the tail of --student list = [ 'Michale', 'Bob', 'Tracy', 'Sam']

   student.insert (0, 'Paul'), insert (index, temp) to add new elements to the corresponding position, the rear element is automatically moved back --student = [ 'Paul', 'Michale', 'Bob', 'Tracy' , 'Sam']

   student.pop (), pop () to delete the last element --student = [ 'Paul', 'Michale', 'Bob', 'Tracy']

   del student [1], del delete elements

list method:

   len (list): a list of the number of elements

   max (list): a list of the largest element

   min (list): a list of the minimum element

Eight-tuple: tuple

Tuples use parentheses, and a list of similar, but can not modify the elements of the tuple

t=('physics',2000,'a')

Empty tuple t = ()

Only one tuple element t = (1,)

tuple access elements:

   Accessed by index, the index starting from 0, for example, t [0]

   Theme elements using brackets [index1: index2], does not include an element index index2, e.g. t2 = (1,2,3,4,5,6) print t2 [1: 5] results (2,3,4, 5)

tuple method:

   Comparison of the two elements of tuples: cmp (tuple1, tuple2)

   Tuple number of elements: len (tuple)

   max (tuple): the maximum element tuple

   min (min): the minimum element tuple

   tuple (sep): to convert into a list of tuples

IX dictionary: Dictionary

Dictionary is a variable container model, may store any type of objects, each key-value dictionary separated by a colon, separated by a comma between each key value contained in the entire dictionary braces

dict={'name':'Sam','age':8,'class':'First'}

Access elements:

   With access key, e.g. dict [ 'name'], dict [ 'age']

Updated Dictionary:

   Add new key pair, e.g. dict [ 'score'] = 100

   Update key-value pair, such dict [ 'age'] = 9

   Delete the key pair, e.g. del dict [ 'score']

   Empty dictionary, for example dict.clear ()

   Delete dictionary, del dict

Dictionary features:

1, with a duplicate key is assigned, it will only remember the latest assignment

2, the key immutable

Dictionary built-in methods:

   cmp (dict1, dict2): comparison of two dictionary size

   len (dict): counting the number of dictionaries

Dictionary of built-in functions:

   Returns true key to determine whether or not in the dictionary, there is: dict.has_key (key)

   dict.keys (): returns a list of all the keys

   dict.values ​​(): to return all the values

X. collection: set

set is a series of non-repeating element, unordered collection. Create a set way is to call set () and passing in a list, list of elements as a set of elements

s=set(['A','B','C'])

When the set can not contain duplicate elements, comprising a list of incoming duplicate elements, the elements will automatically remove duplicate

Access elements:

   unordered set of elements, can not all need to first determine whether there is an element, it can be used in operator judgment

Traversal set:

   For loop

Update set:

   Add set with additive elements () method, e.g. s.add ( 'C')

   Delete elements with remove set () method, e.g. s.remove ( 'A'), before removing the elements necessary to determine whether there is no error present will delete

 

Guess you like

Origin www.cnblogs.com/testerlina/p/11060469.html
Recommended