第一讲 Python语言

download 第一讲 Python语言

of 29

description

第一讲 Python语言

Transcript of 第一讲 Python语言

  • Dassault Systmes, 2008

    Python

    1

    Introduction to Python and Scripting in Abaqus

    L1.2

    Dassault Systmes, 2008

    Python

  • Dassault Systmes, 2008

    Python

    Introduction to Python and Scripting in Abaqus

    L1.4

    Dassault Systmes, 2008

    Python

    Python :

    : python :

    ::

    : ,,,, , ,

  • Introduction to Python and Scripting in Abaqus

    L1.5

    Dassault Systmes, 2008

    Python

    Python Python : www.python.org

    www.python.org/doc/current/tut/tut.html Python

    comp.lang.pythoncomp.lang.python.announce

    www.rmi.net/~lutz/pybooks.htmlhttp://books.google.com/books?q=python

    Introduction to Python and Scripting in Abaqus

    L1.6

    Dassault Systmes, 2008

    Python

    Python

    $ abaqus python>>> print 5**4625>>> UNIX Ctrl-D Windows

    Ctrl-Z import sys; sys.exit()

    abaqus

    $ abaqus python -c "print 5**4"625

    $ abaqus python mytest.py625

    a = 5; b = 4c = a ** bprint c

  • Introduction to Python and Scripting in Abaqus

    L1.7

    Dassault Systmes, 2008

    Python

    Python UNIX

    $ mytest.py625

    Abaqus/CAE

    #!/prog/pythona = 5; b = 4c = a ** bprint c

    Introduction to Python and Scripting in Abaqus

    L1.8

    Dassault Systmes, 2008

    Python

    Python () Windows

    D:\ mytest625

    a = 5; b = 4c = a ** bprint c

  • Introduction to Python and Scripting in Abaqus

    L1.9

    Dassault Systmes, 2008

    Python

    C++ Perl { } # \

    Introduction to Python and Scripting in Abaqus

    L1.10

    Dassault Systmes, 2008

    Python

    :

    # simple examplemax = 5; i = 0x, y, z = 1, 2, 3

    while i

  • Introduction to Python and Scripting in Abaqus

    L1.11

    Dassault Systmes, 2008

    Python

    : >>> x = 4 # variable name x refers to the integer 4

    X 4.

    >>> a = b = 'test'>>> print a, b'test' 'test'

    Introduction to Python and Scripting in Abaqus

    L1.12

    Dassault Systmes, 2008

    Python

    Objects Python ,(member),(method) mytest2.py

    >>> f = open('tmp.txt')>>> f.close()>>> f.closedTrue

    >>> mdb.__members__['acis', 'adaptivityProcesses', 'annotations', ]>>> mdb.__methods__['AdaptivityProcess', 'Annotation', 'Arrow', ]

    method

    member

    The attributes called __members__ and __methods__ are discontinued in Python v2.2 but will be maintained in Abaqus Python.

  • Introduction to Python and Scripting in Abaqus

    L1.13

    Dassault Systmes, 2008

    Python

    mytest3.py

    >>> def test(arg1, arg2):... print 'arg1=', arg1... print 'arg2=', arg2... >>> test('pink', 'bicycle')arg1= pinkarg2= bicycle

    mytest4.py>>> def distance(point1, point2):... x1, y1 = point1[0], point1[1]... x2, y2 = point2[0], point2[1]... sq = (x2 - x1)**2 + (y2 - y1)**2... return sq**(0.5)... >>> distance((0,0),(4,6))7.2111025509279782

    Introduction to Python and Scripting in Abaqus

    L1.14

    Dassault Systmes, 2008

    Python

    >>> test(arg2='bicycle', arg1='blue')arg1= bluearg2= bicycle

    mytest5.py:>>> def distance(point1, point2=(0,0)):... x1, y1 = point1[0], point1[1]... x2, y2 = point2[0], point2[1]... sq = (x2 - x1)**2 + (y2 - y1)**2... return sq**(0.5)... >>> distance((4,6))7.2111025509279782

  • Introduction to Python and Scripting in Abaqus

    L1.15

    Dassault Systmes, 2008

    Python

    Python 1000 :

    :0! = 1n! = n*(n-1)!

    factorial.pydef factorial(n):

    if n == 0:return 1

    else:return n*factorial(n-1)

    >>> factorial(69)17112245242814131137246833888127283909227054489352036939364800923257279754140647424000000000000000L

    Introduction to Python and Scripting in Abaqus

    L1.16

    Dassault Systmes, 2008

    Python

    : +, -, *, **, /, %

    >>> (5 + 3)/24: and, or, not

    >>> 1 or 01: =, != (!= )

    >>> 5 != 6True: |, ^, &,

    >>> 1

  • Introduction to Python and Scripting in Abaqus

    L1.17

    Dassault Systmes, 2008

    Python

    Python http://www.python.org/dev/peps/pep-0008 http://www.python.org/dev/peps/pep-0257

    Python Guido van Rossum http://www.python.org/doc/essays/styleguide.html

    Introduction to Python and Scripting in Abaqus

    L1.18

    Dassault Systmes, 2008

    Python

    del ,:

  • Introduction to Python and Scripting in Abaqus

    L1.19

    Dassault Systmes, 2008

    Python

    NoneTypemytest6.py:

    >>> list1 = [1,2,3]>>> list1[1, 2, 3]>>> list2 = list1.append(4)>>> list2>>> list1[1, 2, 3, 4]>>> type(list2)

    Dassault Systmes, 2008

  • Introduction to Python and Scripting in Abaqus

    L1.21

    Dassault Systmes, 2008

    : ,,, , : ,,, : : , ,,

    C ( FORTRAN integer*4 or *8 ) C (FORTRAN real*8.) Abaqus typeobject

    Introduction to Python and Scripting in Abaqus

    L1.22

    Dassault Systmes, 2008

    ()*

    : 1234, 0644, 0x100fea8 **

    :>>> 17643L**11111463783748290623436091218...

    : 3.1415, .0314e2, .0314e+2, .0314e-2

    : z = 2 + 3j, 12.34jz.real, z.imag

    * 0, 0x

    ** int

  • Introduction to Python and Scripting in Abaqus

    L1.23

    Dassault Systmes, 2008

    >>> 5*2.010.0

    >>> 3/21>>> 2/30

    Introduction to Python and Scripting in Abaqus

    L1.24

    Dassault Systmes, 2008

    Python

    : : ( sort(), reverse()). :

    Type list [9.0,'b']tuple ('a',45,)

    string 'wxyz'array array((2.3,5.6))

  • Introduction to Python and Scripting in Abaqus

    L1.25

    Dassault Systmes, 2008

    0

    >>> a = ['a','b','c',('A','B','C')]>>> a[0]a>>> a[3]('A', 'B', 'C')>>> a[3][1]'B'.

    >>> a[-1]('A', 'B', 'C')>>> a[-1][-2]'B'

    Introduction to Python and Scripting in Abaqus

    L1.26

    Dassault Systmes, 2008

    s[m:n]

    m n [0:len(s)] m n-1

    >>> s = [0, 1, 2, 3, 4]>>> s[1:4][1, 2, 3]

    >>> myTuple = (175, 999, 'A', 'B')>>> myTuple[1:3](999, 'A')>>> welcome = 'Hello World'>>> welcome[0:3] + welcome[5:-1]'Hel World'

  • Introduction to Python and Scripting in Abaqus

    L1.27

    Dassault Systmes, 2008

    >>> a = [0,1] * 3>>> a[0, 1, 0, 1, 0, 1]

    >>> len(a)6

    >>> [1,2] + [6,7][1, 2, 6, 7]>>> (1,2) + (6,7)(1, 2, 6, 7)>>> 'Dead' + 'Parrot''DeadParrot'

    Introduction to Python and Scripting in Abaqus

    L1.28

    Dassault Systmes, 2008

    :

    >>> myList = [175, 999, 'A', 3.14159]>>> myList.append(2003.567)>>> myList[175, 999, 'A', 3.14159, 2003.567]>>> del myList[3]>>> myList[175, 999, 'A', 2003.567]>>> myList.sort()>>> myList[175, 999, 2003.567, 'A']

    :

    >>> dir(myList)['append', 'count', 'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort']

  • Introduction to Python and Scripting in Abaqus

    L1.29

    Dassault Systmes, 2008

    :

    >>> myTuple = (175, 999, 'A', 'B')

    :>>> myTuple[3] = 'W'Traceback (most recent call last):File "", line 1, in ?

    TypeError: object doesn't support item assignment :

    >>> myTuple = (175, 999, [1,2,3,4])

    myTuple[2]myTuple[2],

    Introduction to Python and Scripting in Abaqus

    L1.30

    Dassault Systmes, 2008

    :

    a a a a a>>> name = 'Norwegian'>>> name[3]'w'

    >>> name = name + ' Blue'>>> name'Norwegian Blue'

    >>> doc = """This program is designed... to be all things to all men"""

  • Introduction to Python and Scripting in Abaqus

    L1.31

    Dassault Systmes, 2008

    : ()

    mytest7.py:>>> myDict = {} # create an empty dictionary>>> myDict['eric'] = 'Eric Idle'>>> myDict['PI'] = 3.14159265359>>> myDict[2001] = 'Beginning of the century'>>> type(myDict)

    >>> myDict{'eric': 'Eric Idle', 2001: 'Beginning of the century', 'PI': 3.14159265359}>>> myDict['PI']3.14159265359

    Introduction to Python and Scripting in Abaqus

    L1.32

    Dassault Systmes, 2008

    :

    has_key() keys() values() items()clear() copy() get() update() setdefault()

    mytest7.py :>>> myDict.keys()['eric', 2001, 'PI']

    >>> myDict.has_key('Turnip') False>>> myDict.values()['Eric Idle', 'Beginning of the century', 3.14159265359]

    >>> myDict.items()[('eric', 'Eric Idle'), (2001, 'Beginning of the century'), ('PI', 3.14159265359)]

  • Introduction to Python and Scripting in Abaqus

    L1.33

    Dassault Systmes, 2008

    %

    C sprintf %s (Python )

    >>> '%s weighs %5.2f pounds' % ('Eric', 67)'Eric weighs 67.00 pounds'>>> '%s weighs %s pounds' % ('Eric', 67)'Eric weighs 67 pounds'

    % .

    >>> d = {'name': 'Eric', 'number': 67}>>> '%(name)s weighs %(number)5.2f pounds' % d'Eric weighs 67.00 pounds'

    Introduction to Python and Scripting in Abaqus

    L1.34

    Dassault Systmes, 2008

    None Python :

    0, 0.0, (,), [], {}, , None Python :

    1, 2.3, (1,2), 'xyz' Python , True False

    >>> (1 == 0)False

  • Dassault Systmes, 2008

    Introduction to Python and Scripting in Abaqus

    L1.36

    Dassault Systmes, 2008

    if elif elseif grade > 90:

    print 'you got an A'elif grade > 80:

    print 'you got a B'elif grade > 70:

    print 'you got a C'elif grade > 60:

    print 'you got a D'elif grade > 50:

    print 'you got an E'else:

    print 'you failed'

  • Introduction to Python and Scripting in Abaqus

    L1.37

    Dassault Systmes, 2008

    While C

    while expression:do something

    For >>> for i in range(2, 6):... print i, ... 2 3 4 5>>> myList = ['a', 'b', 'c']>>> for letter in myList:... print letter...abc

    Range

    Introduction to Python and Scripting in Abaqus

    L1.38

    Dassault Systmes, 2008

    break break.py.i, max = 0, 50while 1:

    if i > max:break

    else:print i

    i = i + 1continue else break else

    for i in range(10):if i < 0:

    breakelse:

    print 'There are no negative values in range(10)'

  • Introduction to Python and Scripting in Abaqus

    L1.39

    Dassault Systmes, 2008

    : for x in range(-21, +19):

    # skip the case of x = 0if x == 0:

    continue# evaluate an expressiony = 30 + 30/x# print (x,y) followed by y spaces and a markerprint '(%03d,%03d):' % (x,y), ' '*y, 'o'

    scr_terminalgraph.py

    Dassault Systmes, 2008

  • Introduction to Python and Scripting in Abaqus

    L1.41

    Dassault Systmes, 2008

    qualified names

    (.).>>> myList = [1, 2, 3]>>> myList.reverse()

    myList reverse .

    Introduction to Python and Scripting in Abaqus

    L1.42

    Dassault Systmes, 2008

    Python unqualified names__builtin__

    1. 2. (Python )

    >>> len = 5>>> def test():>>> len = 6>>> print len

    56 len __main__

  • Dassault Systmes, 2008

    Introduction to Python and Scripting in Abaqus

    L1.44

    Dassault Systmes, 2008

    Python .py

    >>> import math>>> x = math.sin(7)

    >>> from math import sin>>> x = sin(7)

    functionsmethods>>> from math import *

  • Introduction to Python and Scripting in Abaqus

    L1.45

    Dassault Systmes, 2008

    import Python Abaqus

    (httplib) Internet

    :

    # module hworldprint 'importing hworld'hw = 'Hello World'c = 76def pr(x):

    print x

    $ abaqus python>>> import hworldimporting hworld>>> print hworld.hwHello World>>> print hworld.c76>>> hworld.pr(58)58

    Introduction to Python and Scripting in Abaqus

    L1.46

    Dassault Systmes, 2008

    Python Python sys.path ImportError Abaqus sys.path

    PYTHONPATH . sys.path

    >>> import sys>>> sys.path

  • Introduction to Python and Scripting in Abaqus

    L1.47

    Dassault Systmes, 2008

    Python sys.path

    : import syssys.path += ['d:\system\pythonStuff']

    PYTHONPATH Windows Python

    sys.path += ['d:\\system\\pythonStuff']sys.path += [r'd:\system\pythonStuff']

    Introduction to Python and Scripting in Abaqus

    L1.48

    Dassault Systmes, 2008

    test __main__ ,

    __main__

    < exported objects >def test():

    < test my exports >if __name__ == '__main__': test()

    __main__

  • Introduction to Python and Scripting in Abaqus

    L1.49

    Dassault Systmes, 2008

    Python modulename.__file__

    globals()

    locals()

    dir(object)

    (, dir(__builtins__))sys.modules

    Dassault Systmes, 2008

  • Introduction to Python and Scripting in Abaqus

    L1.51

    Dassault Systmes, 2008

    ? Python

    ?

    Introduction to Python and Scripting in Abaqus

    L1.52

    Dassault Systmes, 2008

    >>> 1/0 Traceback (most recent call last):

    File "", line 1, in ? ZeroDivisionError: integer division or modulo by zerodef inverse(x):

    try:print 1/x

    except ZeroDivisionError:print x, 'has no inverse'

  • Dassault Systmes, 2008

    Introduction to Python and Scripting in Abaqus

    L1.54

    Dassault Systmes, 2008

    1-1: import sys # to access command line argumentsdef isAnagram(string1, string2):

    """Return TRUE if string1 is an anagram of string2.Returns FALSE if not an anagram"""if len(string1) != len(string2):

    return 0for c in string1:

    if string1.count(c) != string2.count(c):return 0

    else:return 1

    if isAnagram('helloworld','world'):print 'helloworld', 'is an anagram of', 'world'

    else:print 'helloworld', 'is not an anagram of','world'

  • Introduction to Python and Scripting in Abaqus

    L1.55

    Dassault Systmes, 2008

    1-2:

    # open a file for reading, and print it outf = open('fileManipulation.txt')text = f.readlines()for line in text:

    print line[:-1] # chop off the newline char

    # reverse the order of the lines and save ittext.reverse()f = open('temp.txt', 'w')f.write('Reversed file\n')for line in text:

    f.write(line)f.close()

    # now print the new fileprint open('temp.txt').read()

    fileManipulation.py

    Dassault Systmes, 2008

  • Introduction to Python and Scripting in Abaqus

    L1.57

    Dassault Systmes, 2008

    1-1:

    1. 10000

    kn = kn-1 + kn-2

    : 0, 1, 1, 2, 3, 5, 8, 13,. . .

    2. :

    Introduction to Python and Scripting in Abaqus

    L1.58

    Dassault Systmes, 2008

    1-2: Circle

    1. circle math .

    2. circle :(5, 6, 7, 8, 9).

    :radius circumference area 5.00 31.416 78.53986.00 37.699 113.09737.00 43.982 153.93808.00 50.265 201.06199.00 56.549 254.4690