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

Python3 review notes -runoob

編輯:Python

List of articles

  • Basics
    • Reserved words
    • notes
    • Code wrap
    • Multiple lines of code on one line
    • import
  • data type
    • Variable
    • Number
    • string
    • List,Tuple
    • Set
    • Dictionary
    • Logical operators
  • Data type operations
    • Number
    • String
    • List,Tuple,Dictionary
    • Set
  • Process control
    • if
    • while
    • for
    • iter
    • enumerate
    • zip
    • Dictionary traversal
  • function
    • Custom function
    • Anonymous functions
    • return

Basics

Reserved words

>>> import keyword
>>> keyword.kwlist

notes

# Single-line comments 
''' many That's ok notes Interpretation of the '''
""" This is also Multiline comment """

Code wrap

\

total = one + \
two

Multiple lines of code on one line

;

import keyword;keyword.kwlist

import

import os # Import the entire module 
from os import path # Import functions in the module 
from os import path,system # Import multiple functions 
from os import * # Import all functions 

data type

Number,String,List,Tuple,Set,Dictionary

Variable

a = b = c = 1 # Multivariable assignment 
a,b,c = 1,2,'run' # Multivariable assignment 
del a # Delete reference 
del a,b,c # To delete multiple 
type(a) # View data type 
isinstance(a,int) # Judge data type 

Number

int,bool,float,complex
+,-,*,/,//( integer ),%( Remainder ),**

string

''' Multiple lines Text '''
""" It's also Multiple lines Text """
str = ' This is a \' Escape character '
str = ' Connecting characters ' + ' Use plus sign '
str = ' Asterisk for repeated characters ' * 3
str[1:2:2] # Character slices [ rise : stop : step ]
str = r' It can also be used. r Character escape \n'

List,Tuple

[],()

a = [1,2,3,'a','b']
b = [4,'c']
a[0:2] # section 
a * 2 # repeat 
a + b # Connection list 

Set

a = {
'a','b',1,2} # How it was created 1
b = set('abc') # How it was created 2
a - b # Difference set 
a | b # Combine 
a & b # intersection 
a ^ b # a and b Elements that don't exist at the same time ( Remove intersection )

Dictionary

a = {
} # How it was created 1
b['a'] = 1 # How it was created 2
c[1] = b # How it was created 3
d = dict(a=1,b=2) # How it was created 4
e = dict([('a',1),('b',2)]) # How it was created 5
print(b.keys())
print(b.values())

Logical operators

==,!=,>,<,>=,<=
+=,-=,*=,/=,%=,**=,//=
and,or,not
in,not in
is,is not


Data type operations

Number

abscelexpfabsfloorloglog10maxminmodfpowroundsqrt

String

name = 'dan'
'Hellow %s' % name
f'Hello {
name}'
a = 1
f'{
a+1}'

List,Tuple,Dictionary

a = [1,2,'a']
a.append('b') # Additional tuple Elements in are not allowed to be modified 
del a[2] # Delete 
len(a) # length 
'a' in a # Whether there is 

Set

a = {
'a','b','c'}
a.add('d')
a.update(x)
a.remove('d') # Element does not exist will report an error 
a.discard('d') # Same as remove But there is no error 

Process control

if

age = 18
if age > 18:
print('adult')
elif age > 14:
print('young man')
else:
print('kid')

while

counter = 1
sum = 0
while True:
sum += counter
if sum > 50:
break
count = 0
while count < 5:
count += 1
else:
print(' Greater than or equal to 5')
while True: print(' One line and infinite loop ')

for

for i in range(4):
print(i)
for i in range(4):
print(i)
else:
print(' The loop ends ')

iter

a = list(range(4))
it = iter(a)
for x in it:
next(it)

enumerate

a = ['a','b','c']
for x,y in enumerate(a):
print(' Subscript :%s' % x)
print(' value :%s' % y)

zip

a = [1,2,3]
b = ['a','b','c']
for x,y in zip(a,b):
print(a,b)

Dictionary traversal

a = {
'a':1,'b':2}
for k in a:
print('key:%s' % k)
print('item:%s' % a[k])
for k,v in a.items():
print('key:%s' % k)
print('item:%s' % v)

function

Custom function

def hanshu():
pass
  • Keyword parameters
def hanshu(age=18):
print(age)
  • Optional parameters
    • There was no introduction age Other parameters , The second print is empty
    • Into age Parameters other than , Will tuple The form is packaged and printed
def hanshu(age,*info):
print(age)
print(info)
  • Keyword optional parameters , ditto
def hanshu(age,**info):
print(age)
print(info)

Anonymous functions

lambda Parameters : result

>>> sum = lambda x,y:x+y
>>> print(sum(1,2))
3

return

Exit function

def hanshu():
return
# return None

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