Google Python Class 学习笔记(1) Introduce String list del

来源:互联网 发布:矩阵奇异值分解例题 编辑:程序博客网 时间:2024/06/05 00:53
1、python 带参数的main函数:  

if len(sys.argv) >= 2:
    name = sys.argv[1]
  else:
    name = 'World'

如  运行  pyhon hello.py Zhang,
参数数组argv中,第一个参数是hello.py 之后才是真正带的参数,与C++相同,但是与java不同,java直接是第一个。

sys.argv[0] being the program itself, sys.argv[1] the first argument, and so on. 


2、运行时代码检查
def main():
    if name == 'Guido':
        print repeeeet(name) + '!!!'
    else:
        print repeat(name)
只有当运行时,name为Guido时,程序才报错,不像JAVA编译时检查错误。


3、变量命名不要股改内置的变量名
  However, be careful not to use built-ins as variable names. For example, while 'str' and 'list' may seem like good names, you'd be overriding those system variables. Built-ins are not keywords and thus, are susceptible to inadvertent use by new Python developers.

4、String literals can be enclosed by either double or single quotes

5、Unlike Java, the '+' does not automatically convert numbers or other types to string form. The str() function converts values to a string form so they can be combined with other strings.
 pi = 3.14 ##text = 'The value of pi is ' + pi      ## NO, does not work text = 'The value of pi is '  + str(pi)   ## yes

6、There is no ++ operator


7、Here are some of the most common string methods:


s.lower(), s.upper() 
-- returns the lowercase or uppercase version of the string
s.strip() 
-- returns a string with whitespace removed from the start and end
s.isalpha()/s.isdigit()/s.isspace()... 
-- tests if all the string chars are in the various character classes
s.startswith('other'), s.endswith('other') 
-- tests if the string starts or ends with the given other string
s.find('other') 
-- searches for the given other string (not a regular expression) within s, 
and returns the first index where it begins or -1 if not found
s.replace('old', 'new') 
-- returns a string where all occurrences of 'old' have been replaced by 'new'
s.split('delim') 
-- returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text.'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. 
As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
s.join(list) 
-- opposite of split(), joins the elements in the given list together using the string as the delimiter. 
e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---ccc
8、for example "hello"

h   e  l  l  o
正序: 0   1  2  3  4
反序: -5 -4 -3 -2 -1


s[1:4] is 'ell' -- chars starting at index 1 and extending up to but not including index 4
s[1:] is 'ello' -- omitting either index defaults to the start or end of the string
s[:] is 'Hello' -- omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)
s[1:100] is 'ello' -- an index that is too big is truncated down to the string length


s[-1] is 'o' -- last char (1st from the end)
s[-4] is 'e' -- 4th from the end
s[:-3] is 'He' -- going up to but not including the last 3 chars.
s[-3:] is 'llo' -- starting with the 3rd char from the end and extending to the end of the string.


9、List 
List Methods

Here are some other common list methods.

list.append(elem) 
-- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
list.insert(index, elem) 
-- inserts the element at the given index, shifting elements to the right.
list.extend(list2) 
-- adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
list.index(elem) 
-- searches for the given element from the start of the list and returns its index. 
-- Throws a ValueError if the element does not appear (use "in" to check without a ValueError).
list.remove(elem) 
-- searches for the first instance of the given element and removes it (throws ValueError if not present)
list.sort() 
-- sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
list.reverse() 
-- reverses the list in place (does not return it)
list.pop(index) 
-- removes and returns the element at the given index. 
-- Returns the rightmost element if index is omitted (roughly the opposite of append()).
list = [1, 2, 3]print list.append(4)   ## NO, does not work, append() returns None## Correct pattern:list.append(4)print list  ## [1, 2, 3, 4]



10、del
  
  var = 6  del var  # var no more!    list = ['a', 'b', 'c', 'd']  del list[0]     ## Delete first element  del list[-2:]   ## Delete last two elements  print list      ## ['b']  dict = {'a':1, 'b':2, 'c':3}  del dict['b']   ## Delete 'b' entry  print dict      ## {'a':1, 'c':3}




0 0
原创粉丝点击