Chapter 2

1、符号>>用来定向输出。

>>> import sys
>>> print >> sys.stderr,'Fatal error!'
Fatal error!

2、raw_input()内建函数读取标注输入,用户的输入是字符串类型,需要转为整型。

1 >>> num=raw_input("Enter a num: ")
2 Enter a num: 2341
3 >>> print 'the num is: %d' % int(num)
4 the num is: 2341

3、Python不支持C语言的自增1和自减1操作

加号(+)用户字符串的连接运算,乘号(*)用于字符串的重复

4、类

当一个实例被创建的时候,__init__()就会自动被调用。

self.__class__.__name__返回的是该类的类名.

dir()是Python提供的一个API函数,dir()函数会自动寻找一个对象的所有属性,包括搜索__dict__中列出的属性。

>>>dir(FooClass)
>>>['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'showname', 'version']
>>>FooClass.__dict__()
>>>{'__module__': '__main__', 'version': 0.1, 'showname': <function showname at 0x10994e578>, '__dict__': <attribute '__dict__' of 'FooClass' objects>, '__weakref__': <attribute '__weakref__' of 'FooClass' objects>, '__doc__': 'optional documentation string', '__init__': <function __init__ at 0x109945ed8>}

5、 模块

>>> import sys
>>> sys.platform
'darwin'
>>> sys.version
'2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]'
>>> sys.stdout.write("Hello dear!
")   #write()不会在字符串的后面添加换行符
Hello dear!
>>> sys.stdout.write("Hello dear!")
Hello dear!>>>
原文地址:https://www.cnblogs.com/relaxgirl/p/4540180.html