Python formatted output
name = "laowang" age = 18 print(age) print(type(name)) print(name + str(age)) print("老王今年%d岁"%age) print("%s今年%d岁"%(name,name))
Print floating-point numbers (Specify the number of decimal places reserved) * *
Print ("His height is%.2f m"% (1.83))
Effect for his height is 1.83 m
Print ("Hello")
Print ("World")
If she doesn't change the line display, you can change
Print ("Hello", end= "")
Print ("World")
Print default end = "/n"
More Python formatted output click here
Input is a string.
num = input ("Please enter Number:")
Print ("Number entered is%s"%num)
Operator
a,b = 1,2 a,b = b,a #a,b可以直接交换
Enter two numbers and output two number of
NUM1 = input ("Please enter first number:")
num2 = input ("Please enter a second number:")
result = Int (NUM1) +int (num2)
Print ("%s +%s =%d"% (Num1,num2,result))
String
Given a word, print the given letter in turn
world = input("请输入一个单词:")for temp in world: print(temp)
string slices
name = ' ABCdef '
Name[0:2]--------> ' ab ' starting from 0 to 2 in front of a character
name[:]-----------> ' abcdef ' Take all
NAME[-1]----------> ' F ' last character
NAME[0:-1]--------> ' ANCDE '
name[start Position: End Position: Step length]
name[::]----------> ' abcdef ' Take all
Name[::2]---------> ' Ace ' step is 2
NAME[::-1]--------> ' FEDCBA ' inverted output
List additions
Append-------------> Add elements to the end of the list
Extend-------------> Add a list to the end of the list
>>> a = [+]
>>> B = [3,4]
>>> A.append (b)
>>> A
[1,2,[3,4]]
>>>a.extend (b)
>>>a
[1,2,[3,4],3,4]
Insert-------------> Inserts an element before the specified position
>>>a = [0,1,2]
>>>a.insert (1,3)
>>>a
[0,3,1,2]
Find
names =["xiaohong","xiaoming","laowang"] shuru = input("请输入你的名字:") if shuru in names: print("在花名册里") else: print("不在花名册里")
Index-------> return subscript, if not present, exception
Count-------> returns the number of occurrences, if not present, returns 0
Delete
del-----------> Delete by subscript del names[]
Pop-----------> Delete last element Names.pop ()
Remove--------> Delete names.remove ("") based on the value of the element
Sort
Sort ()----------> from small to large
Sort (reverse=true)-----------------> from big to Small
Revserse ()---------------> Bring all the elements from the list.
Meta-group
The value of a tuple is not allowed to be modified, when only one element in the tuple is added ",", otherwise the type type is an element
Dictionary
Key-value pairs: Key is not allowed to be the same and cannot be modified.
Modify element: variable name [' Key ']= ' modified value '
add element: variable name [' Key ']= data
Delete element: Del variable name [key]
del Delete entire dictionary
Clear clears the entire dictionary
Traverse
1. Traversing the dictionary key
>>> for key in Dict.keys ():
.. print (Key)
2. Traversing the value of a dictionary
>>> for value in Dict.values ():
... print (value)
3. Traversing a dictionary item
>>>FOR item in Dict.items ():
.... print (item)
4. Traversing dictionary Key-value (key-value pairs)
>>>for Key,value in Dict.items ():
... print ("key=%s,value=%s"% (Key,value))
Function
Calling function: Function name ()
Document Description of the function
>>>def test(a,b)... "用来完成对2个数求和" ... print("%d"%(a+b)) >>> help (test)Help on function test in module _main_: test(a,b) 用来完成对2个数求和 (END)
The function definition cannot be preceded by the TAB key
The program does not have a syntax error because the first return is executed and the function ends, and all subsequent return will not be executed.
If the defined two function names are the same, execute the function only if the following function is executed
If you modify a global variable directly in a function, an exception is generated
If you really need to make changes, you can declare +global in the function, and the other functions modify the
Indefinite length parameter
>>>def fun(a,b,*args,**kwargs): print(a) print(b) print(args) print(kwargs) test(11,22,33,44,55,66,77,88,99,mm=11,nn=44)>>>11 22 (33,44,55,66,77,88,99) {"mm":11,"nn":44}>>>A=[11,22,33] B={"aa":100,"bb":200}
Reference
>>>a = 100>>>b = a>>>id(a) 10914880>>>id(b) 10914880>>>a = 200>>>b 100>>>id(b) 10914880>>>id(a) 10918080 >>>A = [11,22]>>>B = A>>>A.append(33)>>>A [11,22,33]>>>B [11,22,33]
Variable data types: list, dictionary
Immutable data types: numbers, strings, tuples
The key in the dictionary is an immutable type
A = A+a and A+=a pass parameter effects are different
A = A + A: Take a out first, reorganize it, and put it in a new address.
A+=a: Directly to the original a modification, the address is unchanged
anonymous function —————————— lambda parameter: expression
B.sort (key=lambda x:x[' Xuhao ') sort by xuhao from small to large
File
Open the file: open (file name, access mode)
Close file: Close ()
An object-oriented definition of a class is as follows:
class 类名: #类属性(常用“类”访问类属性) # #实例方法 def 方法名(self): #实例属性 执行的程序 #如果类属性的名字和实例属性的名字相同,那么通过对象去获取的时候,会获取实例属性的值
Class name: Rule Big Hump
To add an attribute to an object:
The name of the object. New Property name = value
Get the properties of this object, 2 methods:
1. Object. Properties
2. Define a method, in which the self is used. property
Init() Method 1. Is the method that Python calls automatically, the call time is: After the object is created, automatically call 2 immediately. No developer calls are required, that is, the object name. __INTI__ () 3. This method typically accomplishes some default things, such as adding some property 4.
class XXXX: def __init__(self,new_a,new_b): self.a = new_a self.b = new_b
Note: new_a, New_b is a local variable, not an object's property, and if you add a property to the __init__ method, you need to use something like self. The format of the property name = value, at which time SELF.A = New_a represents an attribute added to the object, the property is named A, The value of this property is the value inside the local variable new_a
As long as there are 2 underscores in front of the property name, it represents the private property, the so-called private property, and cannot be used externally with the object name. Property name, the property is not added __, the default is public.
Python Learning Notes