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直接是第一个。
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.
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()).
10、del
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.
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
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
- Google Python Class 学习笔记(1) Introduce String list del
- #python学习笔记# del
- Google Python Class 学习笔记(2) 正则表达式
- Python list del remove
- python学习笔记——pass del
- python学习笔记1---class
- python list 的 del() 函数
- 【Python】python学习笔记——pass del
- 「学习笔记——Python」Google's Python Class 学习笔记
- Python 学习笔记:class
- Python开发学习笔记(9) - list,tuple,string切片
- python tutorial 学习笔记(六)class
- python学习笔记-10(类class)
- Python学习笔记:类(class)
- python学习笔记--list
- python学习笔记--List
- python list.remove(),del()和filter & lambda
- python学习笔记 (string)
- 三分求极值
- OpenCV_Python学习笔记--OpenCV中的绘图函数
- ViewPager和Tabhost结合,可滑动的tabhost
- POI实战-java开发excel详解之常用操作-注释
- Ubuntu配置nginx+php
- Google Python Class 学习笔记(1) Introduce String list del
- 使用正则表达式进行匹配
- 【Android】实现联系人效果
- curl 返回CURLE_COULDNT_RESOLVE_HOST、CURLE_COULDNT_CONNECT
- js 验证手机号码和座机号码
- hdu1044+BFS+DFS+Collect More Jewels
- 黑马程序员——集合框架(单列集合:Collection接口)
- POI实战-java开发excel详解之常用操作-单元格合并与数据读取
- Java教程