• Python基础教程(二)


    Python 中字符串被定义为引号之间的字符集合。Python 支持使用成对的单引号或双引号,
    三引号(三个连续的单引号或者双引号)可以用来包含特殊字符。使用索引运算符( [ ] )和切
    片运算符( [ : ] )可以得到子字符串。字符串有其特有的索引规则:第一个字符的索引是 0,
    最后一个字符的索引是 -1
    加号( + )用于字符串连接运算,星号( * )则用于字符串重复。下面是几个例子:

    >>> pystr = 'Python'
    >>> iscool = 'is cool!'
    >>> pystr[0]
    'P'
    Edit By Vheavens
    Edit By Vheavens
    >>> pystr[2:5]
    'tho'
    >>> iscool[:2]
    'is'
    >>> iscool[3:]
    'cool!'
    >>> iscool[-1]
    '!'
    >>> pystr + iscool
    'Pythonis cool!'
    >>> pystr + ' ' + iscool
    'Python is cool!'
    >>> pystr * 2
    'PythonPython'
    >>> '-' * 20
    '--------------------'
    >>> pystr = '''python
    ... is cool'''
    >>> pystr
    'python
    is cool'
    >>> print pystr
    python
    is cool

    列表:

    >>> aList = [1, 2, 3, 4]
    >>> aList
    [1, 2, 3, 4]
    >>> aList[0]
    1
    >>> aList[2:]
    [3, 4]
    >>> aList[:3]
    [1, 2, 3]
    >>> aList[1] = 5
    >>> aList
    [1, 5, 3, 4]


    元组:

    >>> aTuple = ('robots', 77, 93, 'try')
    >>> aTuple
    ('robots', 77, 93, 'try')
    >>> aTuple[:3]
    ('robots', 77, 93)
    >>> aTuple[1] = 5
    Traceback (innermost last):
    File "<stdin>", line 1, in ?
    TypeError: object doesn't support item assignment


    if语句:

    if expression1:
    if_suite
    elif expression2:
    elif_suite
    else:
    else_suite


    while循环:

    >>> counter = 0
    >>> while counter < 3:
    ... print 'loop #%d' % (counter)
    ... counter += 1
    


    for循环和range()内建函数:

    >>> print 'I like to use the Internet for:'
    I like to use the Internet for:
    >>> for item in ['e-mail', 'net-surfing', 'homework',
    'chat']:
    ... print item
    ...

    数字循环:

    >>> for eachNum in range(3):
    ... print eachNum
    ...

    字符串迭代:

    >>> foo = 'abc'
    >>> for c in foo:
    ... print c
    ...
    >>> foo = 'abc'
    >>> for i in range(len(foo)):
    ... print foo[i], '(%d)' % i
    ...
    >>> for i, ch in enumerate(foo):
    ... print ch, '(%d)' % i
    ...

    列表解析

    >>> sqdEvens = [x ** 2 for x in range(8) if not x % 2]
    >>>
    >>> for i in sqdEvens:
    ... print i


    文件和内建函数open() 、file()

    如何打开文件
    handle = open(file_name, access_mode = 'r')
    file_name 变量包含我们希望打开的文件的字符串名字, access_mode 中 'r' 表示读取,
    'w' 表示写入, 'a' 表示添加。其它可能用到的标声还有 '+' 表示读写, 'b'表示二进制访
    问. 如果未提供 access_mode , 默认值为 'r'。如果 open() 成功, 一个文件对象句柄会被
    返回。所有后续的文件操作都必须通过此文件句柄进行。当一个文件对象返回之后, 我们就可
    以访问它的一些方法, 比如 readlines() 和close()

    下面有一些代码, 提示用户输入文件名, 然后打开一个文件, 并显示它的内容到屏幕上:
    filename = raw_input('Enter file name: ')
    fobj = open(filename, 'r')
    for eachLine in fobj:
    print eachLine,
    fobj.close()


    错误和异常:

    try:
    filename = raw_input('Enter file name: ')
    fobj = open(filename, 'r')
    for eachLine in fobj:
    print eachLine, fobj.close()
    except IOError, e:
    print 'file open error:', e

    定义函数:

    def addMe2Me(x):
    'apply + operation to argument'
    return (x + x)
    这个函数, 干的是“在我的值上加我”的活。它接受一个对象, 将它的值加到自身, 然
    后返回和


  • 相关阅读:
    promise的终止调用方法:
    (五)浅谈测试用例
    (四)一个bug的生命周期
    (三)趣谈软件需求分析
    (二)软件测试分类
    (一) 软件测试实质
    【转载—“光荣之路”公众号】Bug预防体系(上千bug分析后总结的最佳实践)
    <MFC>FILE的操作
    <CAN>测试的原理
    <C++>消息循环
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3241538.html
Copyright © 2020-2023  润新知