第一周工作任务总结

第一周 我学习了python的基本语法,以下我对基本语法进行了一点总结·:

首先是Python标识符
在 Python 里,标识符有字母、数字、下划线组成。
在 Python 里,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。
Python 中的标识符是区分大小写的。
以下划线开头的标识符是有特殊意义的。以单下划线开头 foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入;
以双下划线开头的 foo 代表类的私有成员;以双下划线开头和结尾的foo 代表 Python 里特殊方法专用的标识,如init() 代表类的构造函数。

然后是Python有五个标准的数据类型
• Numbers(数字)
• String(字符串)
• List(列表)
• Tuple(元组)
• Dictionary(字典)
接下来Python支持四种不同的数字类型:
• int(有符号整型)
• long(长整型[也可以代表八进制和十六进制])
• float(浮点型)
• complex(复数)
python的字串列表有2种取值顺序:
• 从左到右索引默认0开始的,最大范围是字符串长度少1
• 从右到左索引默认-1开始的,最大范围是字符串开头
List(列表) 是 Python 中使用最频繁的数据类型。
• 列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。
• 列表用 [ ] 标识,是 python 最通用的复合数据类型。
• 列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。
• 加号 + 是列表连接运算符,星号 * 是重复操作。
元组是另一个数据类型,类似于List(列表)。
• 元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。
• 列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
• 字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
另一个需要掌握的就是Python数据类型转换
• 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可。
• 以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。

• 函数
• 函数通过“def”关键字进行声明。可选参数以集合的方式出现在函数声明中并紧跟着必选参数,可选参数可以在函数声明中被赋予一个默认值。已命名的参数需要赋值。函数可以返回一个元组(使用元组拆包可以有效返回多个值)。Lambda函数是由一个单独的语句组成的特殊函数,参数通过引用进行传递,但对于不可变类型(例如元组,整数,字符串等)则不能够被改变。这是因为只传递了该变量的内存地址,并且只有丢弃了旧的对象后,变量才能绑定一个对象,所以不可变类型是被替换而不是改变(译者注:虽然Python传递的参数形式本质上是引用传递,但是会产生值传递的效果)。
匿名函数lambda
python 使用 lambda 来创建匿名函数。
• lambda只是一个表达式,函数体比def简单很多。
• lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。
• lambda函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。
• 虽然lambda函数看起来只能写一行,却不等同于C或C++的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。
如:
sum = lambda arg1, arg2: arg1 + arg2;
print "相加后的值为 : ", sum( 10, 20 ) //输出30
• 类
• Python支持有限的多继承形式。私有变量和方法可以通过添加至少两个前导下划线和最多尾随一个下划线的形式进行声明(如“__spam”,这只是惯例,而不;是Python的强制要求)。当然,我们也可以给类的实例取任意名称。例如:
• class MyClass(object):
• common = 10
• def init(self):
• self.myvariable = 3
• def myfunction(self, arg1, arg2):
• return self.myvariable

• # This is the class instantiation
• >>> classinstance = MyClass()
• >>> classinstance.myfunction(1, 2)
• 3
• # This variable is shared by all classes.
• >>> classinstance2 = MyClass()
• >>> classinstance.common
• 10
• >>> classinstance2.common
• 10
• # Note how we use the class name
• # instead of the instance.
• >>> MyClass.common = 30
• >>> classinstance.common
• 30
• >>> classinstance2.common
• 30
• # This will not update the variable on the class,
• # instead it will bind a new object to the old
• # variable name.
• >>> classinstance.common = 10
• >>> classinstance.common
• 10
• >>> classinstance2.common
• 30
• >>> MyClass.common = 50
• # This has not changed, because "common" is
• # now an instance variable.
• >>> classinstance.common
• 10
• >>> classinstance2.common
• 50

• # This class inherits from MyClass. The example
• # class above inherits from "object", which makes
• # it what's called a "new-style class".
• # Multiple inheritance is declared as:
• # class OtherClass(MyClass1, MyClass2, MyClassN)
• class OtherClass(MyClass):
• # The "self" argument is passed automatically
• # and refers to the class instance, so you can set
• # instance variables as above, but from inside the class.
• def init(self, arg1):
• self.myvariable = 3
• print arg1

• >>> classinstance = OtherClass("hello")
• hello
• >>> classinstance.myfunction(1, 2)
• 3
• # This class doesn't have a .test member, but
• # we can add one to the instance anyway. Note
• # that this will only be a member of classinstance.
• >>> classinstance.test = 10
• >>> classinstance.test

其它杂项
• 数值判断可以链接使用,例如 1<a<3 能够判断变量 a 是否在1和3之间。
• 可以使用 del 删除变量或删除数组中的元素。
• 列表推导式(List Comprehension)提供了一个创建和操作列表的有力工具。列表推导式由一个表达式以及紧跟着这个表达式的for语句构成,for语句还可以跟0个或多个if或for语句。

猜你喜欢

转载自www.cnblogs.com/xuzihan/p/9033861.html