1.range() function Return to list , tuple , The number of data in the dictionary
2.enumerate() Can be understood as enhancement for loop example :

foo='abc'
for i,ch in enumerate(foo):
print(ch,'(%d)'%i)

>>>>>a(0)
b(1)
c(2)

The first return is a sequence , The second return is the object in the loop .
3. List resolution

squared=[x**2 for x in rang(4)]
for i in squared:
print(i)
>>>>>0
1
4
9

4.open(filename,access_mode='r')
Open the file first is the file name , Second parameter 'r' Yes read ,'w' Yes write ,'a' It's an increase . The default is 'r'

filename='d:\1.txt'
fobj=open(filename,'r')
for eachLine in fobj:
print(eachLine)
fobj.close()

5. adopt def Defining functions , Default parameter value = Represents the default value for example :

def foo(debug=True):
if debug:
print('A')
else:
print('B')
>>>>>>foo()
>>>>>>A
>>>>>>foo(False)
>>>>>>B

6. Build class :

class MyClass:
"""A Simple example Class"""
i=1234
def f(self):
return 'Hello World'
>>>>x=MyClass()
>>>>x.i
1234
>>>>x.f()
'Hello World'

Constructor __init__( Pay attention to the def Follow __init__ You need to add a space in the middle of )

class Complex:
def __init__(self,a,b):
self.r=a
self.y=b

>>>>>x=Complex(2.3,4.5)
>>>>>x.r,x.y
(2.3,4.5)

self Is a reference to the class instance itself , amount to java In this
self.__class__.__name__ The name of this class is returned
7. Import module
import amount to java In import Reference package

Exercises :
1. User enters a list or tuple containing five fixed values , Output their sum

def total()
x=list(input("Please enter the list "))
total=0
for index in x:
total=total+int(index)
print(total)

2. User enters a list or tuple containing five fixed values , Enter their average

>>> def total():
x=list(input("Please enter the list"))
total=0
for index in x:
total=total+int(index)
print(total/len(x))

>>> total()
Please enter the list1234
2.5

3. User input with loop and condition judgment
use input() Function to prompt the user for a 1 and 100 Number between , If the user input meets this condition , Prompt success and exit . Otherwise, an error message will be displayed and the user will be prompted to enter the value again , Until the conditions are met

>>> def enterNumber():
x=int(input("Please enter the number from 1 to 100"))
if 1<x<100:
print('Success')
else:
print('error')
enterNumber()

>>> enterNumber()

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Some East 14 Pay change 16 salary , Sincerity or routine ? Browser kernel ( understand )java Four functional interfaces ( a key , simple )HashMap Explain in detail html Writing about cherry trees , Writing about cherry trees os Simple use of module