Python3学习笔记

1. Download python3 from https://www.python.org/downloads/release/python-365/

test:  python3 --version



2.Basic Grammar 

List []

list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]
print (list[1:3]) #print 2nd 3rd elements


Tuple ()

elements can not be changed

tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2 )


Set

注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空Dictionary。

student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}

if('Rose' in student) :

    print('Rose in the set')

else :

print('Rose not in the set')


Dictionary

dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

print ("dict['Name']: ", dict['Name'])

print ("dict['Age']: ", dict['Age'])


end 用于将结果输出到同一行,或者在输出的末尾添加不同的字符

print(b, end=',')


Cycle

while... else...


for <variable> in <list>:

<statements>

else:

<statements>


for i in range(0, 10, 3) : # i = 0; i<=10; i++

print(i)


Iterator

list=[1,2,3,4]

it = iter(list)

print (next(it))

for x in it:


Function

def 函数名(参数列表):
    函数体

Import module from other files

http://www.runoob.com/python3/python3-module.html


Keyboard input and write

http://www.runoob.com/python3/python3-inputoutput.html

>>> with open('/tmp/foo.txt', 'r') as f:
...     read_data = f.read()
>>> f.closed

Syntax 

while True:

    try:

x = int(input("Please enter a number: "))

break

    except ValueError:

print("Oops! That was no valid number. Try again ")

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except IOError:
        print('cannot open', arg)
    else:
        print(arg, 'has', len(f.readlines()), 'lines')
        f.close()



Class

http://www.runoob.com/python3/python3-class.html

你可能感兴趣的:(Python)