List, listing
classmates=["Michael", "Bob", "Tracy"] #创建一个列表list
Classmates.sort () #把list的元素进行从小到大的排序, such as 1,2,1,2,4,3 's list, will become 1,1,2,2,3,4
Len (classmates) #获取list元素个数
Classmates[0] #访问list位置的元素, starting from 0, be careful to cross the border, and the last element's index is Len (classmates)-1
CLASSMATES[-1] #正序从0开始, reverse starting from-1
Classmates.append ("Adam") #在classmates的末尾增加元素
Classmates.insert (2. " Feng ") #插入指定位置
Classmates.pop () #删除末尾元素
Classmates.pop (2) #删除指定位置元素
classmates[2]= "Hello" #替换指定位置元素
Classmate=[1, "good", True] #一个list可以有不同类型的元素
l=[1,2,3,["Python", "Java"],5,6,7] #list可以在另外一个list中作为元素, equivalent two-dimensional array
L=[] Len (L) #list可以为空, no element, becomes an empty list, length is 0
Tuple, tuple
Once a tuple is initialized, it cannot be modified because the tuple is immutable, so the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
When a tuple is defined, the element must be determined.
T= (ON)
Define an empty tuple
T= ()
Defines a tuple with only one element, which must be prefixed with a comma
t= (1,)
If < condition determination 1>:
< Executive 1>
Elif < condition determination 2>:
< Executive 2>
Elif < condition determination 3>:
< Executive 3>
Else
< Executive 4>
The data type returned by the input () function is a character type
The Python loop contains two types.
A loop is a for......in loop that iterates through the elements of a list or tuple of tuples.
For...x...in each element into the variable x and then executes the indented block statement.
names = [' Michael ', ' Bob ', ' Tracy ']
For name in Names:
Print (name)
sum = 0
For x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
Range () function, you can generate a sequence of integers, starting with 0 and left-closing right. The list () function allows you to convert the result of the range () function to a list of lists and assign a value to the variable for invocation.
Range (5)
>>>l=list (range (10))
>>>l
[01,1,2,3,4,5,6,7,8,9]
A loop is a while loop. As long as the conditions are met, the cycle is repeated, and the condition is not satisfied when exiting the loop.
Sum=0
Num=0
While num<=10:
Sum=sum+num
Num=num+1
Print (sum)
The break statement can exit the loop prematurely.
n = 1
While n <= 100:
If n > 10: # when n = 11 o'clock, the condition is met, the break statement is executed
Break # break statement ends the current loop
Print (n)
n = n + 1
Print (' END ')
Continue statement, skip the current loop and start the next loop directly.
n = 0
While n < 10:
n = n + 1
If n% 2 = = 0: # If n is an even number, execute the Continue statement
The Continue # Continue statement will proceed directly to the next loop, and subsequent print () statements will not execute
Print (n)
Dict, is a python built-in dictionary. Full name dictionary, stored as "key (key)-value", has a very fast query efficiency.
>>>d = {' Michael ': Up, ' Bob ': +, ' Tracy ': 76}
>>>d[' Michael ']
In addition to initializing the specified dict, it can also be placed by key.
>>>d[' Adam ']=80
>>>d[' Adam ']
A key corresponds to a value, if the key is repeatedly updated, the following will overwrite the previous.
There are two ways to determine if a key exists in Dict: one through in, and the other through the Get () method of Dict.
>>> ' Themas ' in D
False
>>>d.get (' Themas ')
None #注意, this none is not displayed
>>>d.get (' Themas ',-1)
-1
Dict has the following features:
- The speed of finding and inserting is very fast and will not slow with the increase of key;
- It takes a lot of memory, and it wastes a lot of memory.
So, Dict is a way of exchanging space for time. The Dict key must be an immutable object .
Because Dict calculates the storage location of value based on key, if each calculation of the same key results in a different result, the dict interior is completely chaotic. The object as key cannot be changed. In Python, strings, integers, and so on are immutable.
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.
The arrangement of elements in set is random. is the only one. Repeating elements are automatically filtered.
>>>s=set ([i])
>>>s
{A-i}
>>>s=set ([1,1,2,2,3,4,5,6,6,7])
>>>s
{1,2,3,4,5,6,7}
The Add (key) method can add elements to the set and can be added repeatedly, but only for the first time.
>>>s.add (10)
>>>s
{1,2,3,4,5,6,7,10}
>>>s.add (#重复执行)
>>>s
{1,2,3,4,5,6,7,10} #不变化
The Remove (key) method can delete an element.
>>>s.remove (2)
>>>s
{1,3,4,5,6,7,10}
Python----syntax