程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python basic knowledge review | quick start | common key points

編輯:Python

Basic grammar


Python It's usually a sentence written one line at a time , But if the sentence is long , We can use backslash (\) To implement multiline statements , for example :

total = item_one + \
item_two + \
item_three

Show multiple statements on the same line

Python You can use multiple statements on the same line , Use semicolons... Between statements (;) Division , Here is a simple example :

import sys; x = 'runoob'; sys.stdout.write(x + '\n')

Wrap without wrapping output

The default output is line feed
Add... At the end without newline end=""

Multiple variable assignments

a = b = c = 1
a, b, c = 1, 2, "runoob"

Numerical operation

>>> 5 + 4 # Add
9
>>> 4.3 - 2 # Subtraction
2.3
>>> 3 * 7 # Multiplication
21
>>> 2 / 4 # division , Get a floating point number
0.5
>>> 2 // 4 # division , Get an integer
0
>>> 17 % 3 # Remainder
2
>>> 2 ** 5 # chengfang
32

python list list Intercept

Normal interception [:] Left closed right away

letter = ['r','o','b','o','t']
letter[0:5:2] # Not included 5, yes 5 Previous , Step length is 2
>>['r', 'b', 't']

Flip strings

The first parameter -1 Represents the last element
The second parameter is empty , Means to move to the end of the list
The third parameter is the step ,-1 It means reverse

Delete list elements del

input = 'I like runoob'
inputWords = input.split(" ")
inputWords=inputWords[-1::-1]
output = ' '.join(inputWords)
output
>> runoob like I
# Delete list elements
list = ['Google', 'Runoob', 1997, 2000]
del list[2]
>> list = ['Google', 'Runoob', 2000]

Flip list elements
list.reverse()

a = [i for i in range(10)]
list.reverse(a)
>>[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

list.index(obj)
The index position corresponding to the value

a = [i for i in range(10)]
a[7] = 'a'
print(a.index('a'))
>> 7

list.count(obj)
Count the number of times an element appears in a list
list.remove(obj)
Removes the first match of a value in the list

a = [i for i in range(10)]
a.remove(7)
a
>>[0, 1, 2, 3, 4, 5, 6, 8, 9]

Sort the list

List.sort() # The default is ascending
List.sort(reverse = True) # Add parameter is in descending order

python aggregate set

You can use braces { } perhaps set() Function to create a collection
Be careful :
To create an empty collection, you must use the set() instead of { }, because { } Is used to create an empty dictionary

Main functions: membership testing and deleting duplicate elements , Automatically delete duplicate elements

List de duplication can be used list(set(xxx))

# set You can do set operations
a = set('abracadabra')
b = set('alacazam')
print(a - b) # a and b The difference between the set
print(a | b) # a and b Union
print(a & b) # a and b Intersection
print(a ^ b) # a and b Elements that do not exist at the same time in

python Dictionaries

The elements in the dictionary are accessed by keys , Instead of accessing by offset
In the same dictionary , key (key) Must be unique

print (dict['one']) # The output key is 'one' Value
print (dict[2]) # The output key is 2 Value
print (tinydict) # Output complete dictionary
print (tinydict.keys()) # Output all keys
print (tinydict.values()) # Output all values

Delete operation

del dict['Name'] # Delete key 'Name'
dict.clear() # Empty dictionary
del dict # Delete Dictionary

dict.keys()
Returns an iterator , have access to list() To convert to list
dict.values()
Returns an iterator , have access to list() To convert to list
dict.items()
Return traversable as a list ( key , value ) Tuple array

for key,value in dict.items():

key in dict
If the key is in the dictionary dict Back in true, Otherwise return to false

Mathematical functions

ceil(x) Returns an upinteger of a number , Such as math.ceil(4.1) return 5
floor(x) Returns the rounded value of a number , Such as math.floor(4.9) return 4
round(x [,n]) Return floating point number x Round the value of , Such as given n value , Represents the number rounded to the decimal point .

round(4.3252222,2)
>> 4.33

Random function random

Sort all the elements of the sequence at random random.shuffle

a = [i for i in range(10)]
random.shuffle(a)
print(a)

Random selection of sequence elements random.choice

print(random.choice(a))

String function

upper() Lowercase letters in the string are uppercase
capitalize() Convert the first character of the string to uppercase


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved