Python语法(一)

1.两种解释器

    CPython由C写成;

    Jython由Java写成 ,特点包括

         只要有 Java 虚拟机, 就能运行Jython
         拥有访问 Java 包与类库的能力
         为 Java 开发环境提供了脚本引擎
         能够很容易的测试 Java 类库
         提供访问 Java 原生异常处理的能力
         继承了 JavaBeans 特性和内省能力
         鼓励 Python 到Java 的开发(反之亦然)
         GUI 开发人员可以访问 Java 的 AWT/Swing 库
         利用了 Java 原生垃圾收集器(CPython 未实现此功能)

2.交互式解释器

    >>> primary prompt:expecting the next Python statement

    ...secondary prompt:indicates that the interpreter is waiting for additional input to complete the current statement

主提示符下,'''表示要输入多行

次提示符下,输入if statement 回车后,缩进 然后输入执行语句 最后空行回车表示输入完成

3.print用法

    >>>print variable  #输出 variable content

    >>>variables  #输出 'variable content'

    >>>_  #last evaluated expression

    logfile = open('/tmp/mylog.txt', 'a')
    print >> logfile, 'Fatal error: invalid input!' #redirect output to file named logfile
    logfile.close()

    >>> user = raw_input('Enter login name: ')#built-in function to obtain user input from command line
    Enter login name: root
    >>> print 'Your login is:', user
    Your login is: root    # comma seperate two variables

    >>>print"%s words %d"   %("python",10)

    循环中,print item会为每一个item添加换行符,而print item, 则会用空格代替换行

4.Lists and Tuples

lists and tuples can store different types of objects.

Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed. Tuples are
enclosed in parentheses ( ( ) ) and cannot be updated (although their contents may be). Tuples can be thought of for now as “read-only” lists.

>>> 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]
Slice access to a tuple is similar, except it cannot be modified:
>>> 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值