Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140484
  • 博文数量: 58
  • 博客积分: 3191
  • 博客等级: 中校
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-13 11:16
文章分类

全部博文(58)

文章存档

2013年(4)

2012年(20)

2011年(10)

2010年(1)

2008年(22)

2007年(1)

我的朋友

分类: Python/Ruby

2008-03-10 16:18:56

Chapter 6 Control Flow

What we do here is supply it two numbers and range returns a sequence of numbers starting from the first number and up to the second number. For example, range(1,5) gives the sequence [1, 2, 3, 4]. By default, range takes a step count of 1. If we supply a third number to range, then that becomes the step count. For example, range(1,5,2) gives [1,3]. Remember that the range extends up to the second number i.e. it does not include the second number.

Chapter 7 Functions

Only those parameters which are at the end of the parameter list can be given default argument values i.e. you cannot have a parameter with a default argument value before a parameter without a default argument value in the order of parameters declared in the function parameter list.
 
Chapter 8 Modules
 
The sys.path contains the list of directory names where modules are imported from. Observe that the first string in sys.path is empty - this empty string indicates that the current directory is also part of the sys.path which is same as the PYTHONPATH environment variable. This means that you can directly import modules located in the current directory. Otherwise, you will have to place your module in one of the directories listed in sys.path .
 
Every module has a name and statements in a module can find out the name of its module. This is especially handy in one particular situation - As mentioned previously, when a module is imported for the first time, the main block in that module is run. What if we want to run the block only if the program was used by itself and not when it was imported from another module? This can be achieved using the __name__ attribute of the module.
 
Chapter 9 Data Structures
 
Then, we sort the list by using the sort method of the list. Understand that this method affects the list itself and does not return a modified list - this is different from the way strings work. This is what we mean by saying that lists are mutable and that strings are immutable.
 
Tuples are just like lists except that they are immutable like strings i.e. you cannot modify tuples. Tuples are defined by specifying items separated by commas within a pair of parentheses. Tuples are usually used in cases where a statement or a user-defined function can safely assume that the collection of values i.e. the tuple of values used will not change.
 
Most of the explanation is available in the comments itself. What you need to remember is that if you want to make a copy of a list or such kinds of sequences or complex objects (not simple objects such as integers), then you have to use the slicing operation to make a copy. If you just assign the variable name to another name, both of them will refer to the same object and this could lead to all sorts of trouble if you are not careful.
 
Chapter 11 Object-Oriented Programming
 
To use inheritance, we specify the base class names in a tuple following the class name in the class definition. Next, we observe that the __init__ method of the base class is explicitly called using the self variable so that we can initialize the base class part of the object. This is very important to remember - Python does not automatically call the constructor of the base class, you have to explicitly call it yourself.
 
We also observe that we can call methods of the base class by prefixing the class name to the method call and then pass in the self variable along with any arguments.
阅读(880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~