__init__(): Represents a constructor
# notes
Chinese annotation
# coding: code
# codign:utf-8
Single quotation marks
'''
'''
Double quotes
"""
"""
Be careful with small letters l And capital letters O
grammar
Variable name =value;
print(type( Variable name ))
The real number is 3.14 The plural is 12.5j
3.14+12.5j
Python in ture Express 1 false Express 0
The following values are 0, But in if, perhaps while Behave as true
val=input(" Prompt text ")
val =int(input(" Prompt text "))
print(" Print text ")
a=10
b=20
print(a,b)
》》》 10 20
Enter into folder
fp=open(r'D:\a.txt','a+')
print(" Input to txt Content ",file=fp)
fp.close()
+
-
*
/ 7/2=3.5 Python2.x yes 3
%
// 7//2=3 Take the whole part
** power return x Of y Power 2**4 16
The right is assigned to the left
**=``````x**=y
---->x=x**y >
<
==
!=
>=
<=
grammar
if expression :
Sentence block
grammar
if expression :
Sentence block
else:
Sentence block
grammar
if expression :
Sentence block
elif expression :
Sentence block
else:
Sentence block
if expression :
if expression :
Sentence block
else:
Sentence block
else:
Sentence block
while Conditional expression :
The loop body
for Iterative variable in object
The loop body
for i in range(101)
print(i)
>>>0 、1····100
range(start,end,step):
start Starting value , Do not write default is 0
end End value , End value , It doesn't contain
step: Specify the step size : The default is 1
Only one parameter is written as end
Two are start and end
string =' Don't say I can't '
for ch in string
print(ch)
》》》
No
want
say
I
No
can
Python You can start from the left or the right
The starting subscripts are 0 and -1
grammar :sname[start:end:step]
sname: The name of the series
start: Starting position , The default is 0
end: End position , The default is length
step: : step , The default is 1
valekk = ["1","2", "3","4"]
print(valekk[1:2:1])
》》》['2']
Use it directly + Must be of the same type
if __name__ == '__main__':
valekk = ["1","2", "3","4"]
print(valekk*5)
》》》
['1', '2', '3', '4', '1', '2', '3', '4', '1', '2', '3', '4', '1', '2', '3', '4', '1', '2', '3', '4']
if __name__ == '__main__':
values = ["1","2", "3","4"]
print('1' in values)
》》》
True
if __name__ == '__main__':
values = [1, 5, 4, 6, 7, 8, 9, 5, 3,2]
print(len(values)) # length
print(max(values)) # Maximum
print(min(values)) # minimum value
》》》
10
9
1
listname=[123," test ",888]
emptylist=[]
listname=list(rang(10,20,2))
print(listname)
>>>[10,12,14,16,18]
items=[Expression for var in range]
Expression : expression
var: Loop traversal
range : Generating function objects
items=[Expression for var in list]
according to list Generate new objects
price=[1,2,5,6]
eg:iteams=[int(x*0.5 for x in pritce)]
Delete list
del listname
Access list elements
Method 1 : direct print
Method 2 : By subscript
print(listname[2])
>>> 14
for item in items:
print(item)
for index ,item in enumerate(items)
print(items[index])
print(item)
print(index)
No line breaks print(,end=") ,end=" Output without line break
1. Additive elements
items.append( Elements )
items.insert( Location , Elements )
items.extend(seq) # take seq Spliced in items Back
2. Modifying elements
items[ Subscript ]= Element value
3. Remove elements
del items[ Subscript ]
items.remove(“ Elements ”)
4. Determine whether an element exists Statistics
items.count( Elements )>0 There is
5. Specifies the subscript of the first occurrence of the element
items.index( Elements )
6. Statistical elements and
sum(items[,start])
start: Start subscript
7. Sort
items.sort(key= Elements ,reverse=False)
reverse Default False: Ascending reverse=True: Descending
key=str.lower // Case insensitive Default case sensitive
8. Built in functions sorted() Sort
sorted(items,key=None,reverse=False)
The difference from the list Tuples cannot become , All elements use () Enclose and separate with commas
items=( Elements 1, Elements 2, Elements 3)
Actually () Not its logo Comma is
items= Elements 1, Elements 2, Elements 3
eg
items=tuple(range(1,5))
》》》
(1,2,3,4)
for index ,item inenumerate(items)
print(item)
items[ Subscript ]= Elements
items= Tuples 1+ Tuples 2
When there is only one tuple connected, be sure to add a comma
iteams1=(1,2,3)
iteams2=(4,)
print(iteams1+iteams2)
amount to java Of MAP
dictionary={‘key’:‘value’,‘key’:‘value’,‘key’:‘value’,‘key’:‘value’,}
Method 1 : Use the equals sign directly
dictionary={‘key’:‘value’,‘key’:‘value’,‘key’:‘value’,‘key’:‘value’,}
Method 2 : Mapping function creates dictionary table
dictionary=dist(zip(list1,list2))
zip() function : Put multiple or one list 、 The corresponding positions of Yuanzu are combined into a dictionary
Method 3 : Specify a key value pair
dictionary=dict(key1=value1,…keyn=valuen)
eg
dictionary=dict( Elements =' value ',...keyn=valuen)
Method four : Specify all values as key
names=[' Elements 1',' Elements 2',' Elements 3']
items=names.forkeys(names)
print(items)
》》》
{[' Elements 1':None,' Elements 2':None,' Elements 3':None}
Delete
del items
eliminate
del.clear()
value=items[key Elements ]
value=items.get(key Elements )
value=items.get(key Elements 1,key Elements 2)
dictionary.items()
for item in dictionary.items()
print(item)
dictionary[key]=value
del dictionary[“ Elements ”]
No repetition
Method 1 : Use it directly {} establish
setname={ Collection elements 1, Collection elements 2, Collection elements 3}
Method 2 : Use set() Function creation
setname=set( Content )
eg:
set1=set(" What destiny gives us is not the wine of despair , It's the cup of opportunity ")
print(set1)
set2=set([1,2,3])
print(set2)
set3=set((' Life is too short ',' Timely job hopping '))
print(set3)
》》》
{' life ', ' Alcohol ', ',', ' And ', ' Meeting ', ' and ', ' yes ', ' at ', ' People ', ' No ', ' I ', ' A cup of ', ' loss ', ' to ', ' shipment ', ' Give ', ' machine ', ' Of '}
{1, 2, 3}
{' Life is too short ', ' Timely job hopping '}
setname.add( Element content )
setname.pop()// Remove the last one
setname.clear()
setname.remove( Elements )
set1=set([1,2,3])
print(set1)
set2=set([3,4,5])
print(set2)
set3=set([5,6,7])
print(set3)
print(set1&set2)
print(set1|set2|set3)
print(set1-set2)
》》》
{1, 2, 3}
{3, 4, 5}
{5, 6, 7}
{3}
{1, 2, 3, 4, 5, 6, 7}
{1, 2}