# Single-line comments
''' Multiline comment '''
The same code block is in an indentation
\
Implement multiline statements
[],{},()
No need to use backslash
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
=
Left variable name , The value of the variable on the right
Can wait a=b=c=1
[]
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 ()
Create , Like a list , But the elements cannot be changed ,tuple The element types in can be different {}
Create ,set It can be used as a set operation ,-,&,|
It can be used as a set operation {}
, Key value storage , The key must be immutable , Can't repeat Python3 Of the six standard data types :
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))
if Put it on the back elif
range
range( start , End , step )
iterator :
list = [1,2,3,4]
it = iter(list)
print(next(it))
def Function name ( parameter list ):
The body of the function
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))
# 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()
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_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.
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: 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