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

Every day in winter vacation, make a little progress 16 Python review

編輯:Python

python review

Basic grammar

identifier

  • The first character must be a letter or underscore in the alphabet _ .
  • The rest of the identifier consists of the letters 、 Numbers and underscores .
  • Identifiers are case sensitive .

notes

# Single-line comments 
''' Multiline comment '''

Lines and indents

The same code block is in an indentation

\ Implement multiline statements

[],{},() No need to use backslash

python Numeric type

  • int ( Integers ), Such as 1, There is only one integer type int, Expressed as a long integer , No, python2 Medium Long.
  • bool ( Boolean ), Such as True.
  • float ( Floating point numbers ), Such as 1.23、3E-2
  • complex ( The plural ), Such as 1 + 2j、 1.1 + 2.2j

character string

python in Single quotation marks and Double quotes There is no difference in use

Three quotation marks specify a multiline string

Escape character \

+ String connection

* Duplicate string

r Precede the string to indicate that there is no escape

String interception : Variable [ Header subscript : Tail subscript : step ]

print Output default line wrap

python Basic data type

= Left variable name , The value of the variable on the right

Can wait a=b=c=1

Standard data type

  • Number( Numbers ) : int float complex
  • String( character string ):python String in cannot be changed
  • List( list ):[] Create , From left to right 0, From right to left -1, Similar to string , There are connections and duplicates , Elements can be changed , use del Delete
  • Tuple( Tuples ):() Create , Like a list , But the elements cannot be changed ,tuple The element types in can be different
  • Set( aggregate ):{} Create ,set It can be used as a set operation ,-,&,| It can be used as a set operation
  • Dictionary( Dictionaries ): An empty dictionary :{}, Key value storage , The key must be immutable , Can't repeat

Python3 Of the six standard data types :

  • ** Immutable data (3 individual ):**Number( Numbers )、String( character string )、Tuple( Tuples );
  • ** Variable data (3 individual ):**List( list )、Dictionary( Dictionaries )、Set( aggregate ).

Data type conversion

  • Implicit type conversion
  • Explicit type conversion

Operator

chengfang :**

to be divisible by ( Rounding down ==//==)

Logical operators :and or not

member operator :in

String formatting

print (" My name is %s This year, %d year !" % (' Xiao Ming ', 10))

Under controlled conditions

if Put it on the back elif

range

range( start , End , step )

iterator :

list = [1,2,3,4]
it = iter(list)
print(next(it))

function

def Function name ( parameter list ):
The body of the function

data structure

Lists can be used as stacks

append()
pop()

List as queue

append()
popleft()

List derivation

vec=[2,4,6]
[3*x for x in vec]
# if filter 
[2*x for x in vec if x > 3]
# Traverse two or more sequences 
zip()
for q, a in zip(questions, answers):
# Reverse traversal 
reversed(range(1,10,2))

object-oriented

# Class definition 
class people:
# Define basic attributes 
name = ''
age = 0
# Define private properties , Private properties cannot be accessed directly outside the class 
__weight = 0
# Define construction method 
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s say : I %d year ." %(self.name,self.age))
# Instantiate the class 
p = people('runoob',10,30)
p.speak()

Class inheritance

And c++ similar

# Class definition 
class people:
# Define basic attributes 
name = ''
age = 0
# Define private properties , Private properties cannot be accessed directly outside the class 
__weight = 0
# Define construction method 
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s say : I %d year ." %(self.name,self.age))
# Single inheritance example 
class student(people):
grade = ''
def __init__(self,n,a,w,g):
# Call the constructor of the parent class 
people.__init__(self,n,a,w)
self.grade = g
# Override method of parent class 
def speak(self):
print("%s say : I %d Year old , I'm reading %d grade "%(self.name,self.age,self.grade))
s = student('ken',10,60,3)
s.speak()

Support for multiple inheritance 、 Method rewriting

Private properties of class

__private_attrs: Start with two underscores , Declare the property private , Can't be used outside of the class or accessed directly . When used in methods within a class self.__private_attrs.

Class method

Inside the class , Use def Keyword to define a method , Different from general function definition , Class method must contain parameters self, And it's the first parameter ,self Represents an instance of a class .

self The name of is not meant to be dead , You can also use this, But it's best to use as agreed self.

Private method of class

__private_method: Start with two underscores , Declare the method private , Can only be called inside a class , Cannot call... Outside of a class .self.__private_methods.

class Site:
def __init__(self, name, url):
self.name = name # public
self.__url = url # private
def who(self):
print('name : ', self.name)
print('url : ', self.__url)
def __foo(self): # Private method 
print(' This is the private method ')
def foo(self): # Public methods 
print(' It's the public way ')
self.__foo()
x = Site(' Novice tutorial ', 'www.runoob.com')
x.who() # Normal output 
x.foo() # Normal output 
x.__foo() # Report errors 

Supports overloading of operators


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