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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python中的class是创建对象的蓝图或模板。它定义了对象的属性和方法,可以根据它来创建具有相同属性和方法的多个对象。在类中,属性和方法使用关键字def和self来定义。属性是类中的变量,而方法是类中的函数。类可以通过继承来扩展和定制。Python中使用class关键字来定义类。以下是一个简单的示例: ``` class Person: def __init__(self, name, age): self.name = name self.age = age def get_name(self): return self.name def get_age(self): return self.age p = Person("John", 30) print(p.get_name()) # 输出 "John" print(p.get_age()) # 输出 30 ``` ### 回答2: Python中的class(类)是一种定义自定义对象类型的方法。它是一种运用面向对象编程(OOP)的基本工具。 在Python中,class定义了一个对象的属性和方法。一个类可以看作是一个对象的蓝图,通过该蓝图可以创建实例对象。我们也可以将一个类看作是一种数据类型,可以使用该类型创建多个相似的对象实例。 定义一个类的语法格式如下: ``` class 类名: 属性1 属性2 ... 方法1 方法2 ... ``` 在类的定义中,可以定义多个属性,这些属性用于存储对象的状态信息。同时,还可以定义多个方法,这些方法用于实现对象的行为。 通过类,我们可以方便地创建对象实例并访问其属性和方法。例如,我们可以通过以下代码创建一个类和对象实例: ``` # 定义一个类 class Person: def __init__(self, name, age): self.name = name self.age = age def introduce(self): print("My name is", self.name, ", and I'm", self.age, "years old.") # 创建对象实例 person1 = Person("Alice", 20) person2 = Person("Bob", 25) # 调用对象方法 person1.introduce() person2.introduce() # 输出结果: # My name is Alice, and I'm 20 years old. # My name is Bob, and I'm 25 years old. ``` 在上述代码中,我们定义了一个Person类,该类有两个属性(name和age)和一个方法(introduce)。然后,我们通过该类分别创建了person1和person2两个对象实例,并分别调用了它们的introduce方法。 总的来说,Python中的class是一种定义自定义对象类型的机制,它可以方便地创建多个相似的对象实例,并且可以通过类的属性和方法来访问和操作这些对象的状态和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值