Python学习总结之一

 

Using Python as a Calculator
1、  两个相除可能会出现 floating point results
2、The equal sign ('=') is used to assign a value to a variable
3、 value can be assigned to several variables simultaneously:
4、There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:
5、Complex numbers(复数) are also supported:imaginary numbers are written with a suffix of j or J. Complex numbers with a nonzero real component are written as (real+imagj), or can be created with the complex(real, imag) function.
>>> 1j * complex(0, 1)                                       --》1j *(0 +1j)=1j*1j = -1 = -1 + 0j
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> z=1j*complex(0, 1)
>>> z.real
-1.0
>>> z.imag
0.0
>>>
6、不能使用float()和int()来获得复数的整数部分。Use abs(z) to get its magnitude(模) (as a float) or z.real to get its real part。
        >>> abs(a)  # sqrt(a.real**2 + a.imag**2)
        5.0
7、In interactive mode, the last printed expression is assigned to the variable _ 。(刚求出来的表达式用“_”表示)
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _        这里的_等于12.5625
113.0625
>>> round(_, 2)
113.06
二、Strings
        String:They can be enclosed in single quotes or double quotes。backslash(反斜线)
        Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line
        使用反斜线\可以表示这行未结束。继续下一行。
 \n 换行
        hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."
       
1、 可以使用triple-quotes
        >>> print("""\
Usage: thingy [OPTIONS]
        -h                 Display this usage message
        -H hostname     Hostname to connect to
""")
Usage: thingy [OPTIONS]
        -h                 Display this usage message
        -H hostname     Hostname to connect to

       2、 使用r"String" ,原生态字符串
        >>> hello=r"This is \n\
text!"
>>> print(hello)
This is \n\
text!
      3、Strings can be concatenated (glued together) with the + operator, and repeated with *: 使用+连接,使用*重复
     word*num,num为重复次数,  如:word=“hello”     word*3  ==》  hellohellohello
        4、Strings can be subscripted (indexed);   字符串可以下标的,如index[0]
                word[1] -->e   word[0]-->h
                word[0:2] -->he        word[:2] -->he             word[2:] -->llo   等
        5、Unlike a C string, Python strings cannot be changed  不像C语言的字符串,不能更改下标的值:word[0] = 'x'  错误的
        6、Here’s a useful invariant of slice operations: s[:i] + s[i:] equals s.     如:>>> word[:2] + word[2:]
        7、Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.   索引太大被字符串大小替换。如果上界限小于小界限,接返回空字符串。
word="helpA"
>>> word[1:100]
'elpA'
>>> word[10:]
''
>>> word[2:1]
''
        8、Indices may be negative numbers, to start counting from the right。如果是负数,从右边开始算起
>>> word[-1]     # The last character
'A'
>>> word[-2]     # The last-but-one character
'p'
>>> word[-2:]    # The last two characters
'pA'
>>> word[:-2]    # Everything except the last two characters
'Hel'
        9、But note that -0 is really the same as 0, so it does not count from the right! -0和0相等,都是从左边算起
>>> word[-0]     # (since -0 equals 0)
'H'
        10、Out-of-range negative slice indices are truncated。溢出的部分会被截断
>>> word[-100:]        --》相当于word[0:]
'HelpA'
记忆大法:
 +---+---+---+---+---+
 |   H |   e |    l |   p |   A |
 +---+---+---+---+---+
 0     1     2     3    4     5
-5   -4    -3    -2   -1

        11、The built-in function len() returns the length of a string  内置函数len()
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

Unicode字符
        1、Starting with Python 3.0 all strings support Unicode
>>> 'Hello\u0020World !'
'Hello World !'

Lists
        1、List items need not all have the same type.  不需要相同的类型数据
>>> a = ['spam', 'eggs', 100, 1234]
>>> a
['spam', 'eggs', 100, 1234]
        Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
        2、All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list a:   所有slice操作都返回一个新的list,下面的操作时浅复制一个a
>>> a[:]
['spam', 'eggs', 100, 1234]
        3、Unlike strings, which are immutable,it is possible to change individual elements of a list  可以修改list的元素
        4、Assignment to slices is also possible, and this can even change the size of the list or clear it entirely。修改list
>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:                --》在index【1】插入元素
... a[1:1] = ['bletch', 'xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
        5、The built-in function len() also applies to lists:  len()函数
        6、It is possible to nest lists (create lists containing other lists), for example:可以内嵌List
>>> q=[2,3]
>>> p=[1,q,4]
>>> p
[1, [2, 3], 4]
>>> len(p)         ---》len()仍然是3
3
>>> p[1]
[2, 3]
>>> p[1][0]    --》像二维数组操作
2
>>>
        7、You can add something to the end of the list:  使用append()函数添加元素,在List的最后面添加元素
>>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']
>>> p[0].append('yes')
>>> p.append('yes')
>>> p
[1, [2, 3, 'xtra'], 4, 'yes']
        8、we can write an initial sub-sequence of the Fibonacci series as follows
>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b
...
1
1
2
3
5
8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值