Python学习笔记(一)

来源:互联网 发布:淘宝两颗心 编辑:程序博客网 时间:2024/06/01 07:28
因为学习需要,自学Python,以下为个人的学习心得及汇集相关知识点。
一、输出
print "hello,world"print "你好,世界"print
hello,world你好,世界
最后一句输出结果是空一行。如果在语句后面添加“,”,结果为将两个语句输出在同一行。
print "hello,world",print "你好,世界"
hello,world 你好,世界
二、标识符
1.可以是字母、数字、下划线,但是不可以数字开头,区分大小写
2._foo:表示不能直接访问类属性,需要通过类提供接口进行访问,不能用from…import *导入
3.__foo:表示类的私有成员
4.__foo__:特殊方法的标识,例如__init__表示类的构造函数
5.保留字符:andexecnotassertfinallyorbreakforpassclassfromprintcontinueglobalraisedefifreturndel(删除)importtryelif(否则如果)inwhileelseiswithexceptlambdayield6.格式
可以理解为Python对代码的缩进量有严格要求,需含有相同的缩进量
7.多个语句构成代码组
if、while、def、class复合语句,首行以关键字开始,以“:”结束,该行之后的一行或多行构成代码组,于是将这样的首航以及后面的代码称为子句(cause)
if expression:suite
8.字符型
counter = 100  # 赋值整形变量miles = 100.0  # 浮点型name = "Ashling"  # 字符串型age = 15print counterprint milesprint nameprint age
100100.0Ashling15
9.Python创建变量时会在内存中创建一个空间,每个变量在内存中创建,包括变量标识、名称和数据,每个变量在使用前必须赋值,赋值之后可以创建,赋值符号为“=”,可以做这样多个变量赋值的输出:
a = b = c = 45print a, b, cprinta, b, c = 78, 89, 'ashling'print a, b, c
10.标准数据类型
Numbers 数字String 字符串List 列表Tuple 元组Dictionary 字典
11.删除
var1 = 12var2 = 34print var1, var2del var1, var2
12 34
12.字符串列表
python的字串列表有2种取值顺序:
从左到右索引默认0开始的,最大范围是字符串长度少1
从右到左索引默认-1开始的,最大范围是字符串开头
str = 'hello,world!'print str            # 输出完整字符串print str[0]         # 输出字符串中第一个字符print str[2:5]       # 输出字符串中第三个到第五个之间的字符串print str[2:]        # 输出从第三个字符开始的字符串print str * 2        # 输出字符串两次print str + "TEST"   # 输出连接的字符串
hello,world!hllollo,world!hello,world!hello,world!hello,world!TEST
13.列表
列表中值的切割也可以用到变量 [头下标:尾下标] 
就可以截取相应的列表,从左到右索引默认 0 开始
从右到左索引默认 -1 开始
下标可以为空表示取到头或尾。
list = ['runoob', 786, 2.23, 'john', 70.2]tinylist = [123, 'john']print list              # 输出完整列表print list[0]           # 输出列表的第一个元素print list[1:3]         # 输出第二个至第三个的元素print list[2:]          # 输出从第三个开始至列表末尾的所有元素print tinylist * 2      # 输出列表两次print list + tinylist   # 打印组合的列表
['runoob', 786, 2.23, 'john', 70.2]runoob[786, 2.23][2.23, 'john', 70.2][123, 'john', 123, 'john']['runoob', 786, 2.23, 'john', 70.2, 123, 'john']
14.元组
元组不能二次赋值,相当于只读列表
tuple = ('runoob', 786, 2.23, 'john', 70.2)tinytuple = (123, 'john')print tuple             # 输出完整元组print tuple[0]          # 输出元组的第一个元素print tuple[1:3]        # 输出第二个至第三个的元素print tuple[2:]         # 输出从第三个开始至列表末尾的所有元素print tinytuple * 2     # 输出元组两次print tuple + tinytuple # 打印组合的元组
('runoob', 786, 2.23, 'john', 70.2)runoob(786, 2.23)(2.23, 'john', 70.2)(123, 'john', 123, 'john')('runoob', 786, 2.23, 'john', 70.2, 123, 'john')
元组是不允许更新的,而列表是允许更新的
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]tuple[2] = 1000        # 元组中是非法应用list[2] = 1000         # 列表中是合法应用print tupleprint list
显示元组中是非法应用['runoob', 786, 1000, 'john', 70.2]
15.print dic.iteritems()得到[(键,值)]的列表
用sorted方法,通过key这个参数,指定排序是按照value,也就是第一个元素d[1]的值来排序
reverse=True表示需要翻转的,默认是从小到大,翻转的话,就是从大到小
a_dic = {'a':{'val':3}, 'b':{'val':4}, 'c':{'val':1}}dict_a= sorted(a_dic.iteritems(), key=lambda d:d[1]['val'], reverse = True)b_dic = {'e':{'val':4}, 'd':{'val':3}, 'f':{'val':2}}dict_b= sorted(b_dic.iteritems(), key=lambda d:d[1]['val'])print a_dicprint b_dicprint dict_aprint dict_b
{'a': {'val': 3}, 'c': {'val': 1}, 'b': {'val': 4}}{'e': {'val': 4}, 'd': {'val': 3}, 'f': {'val': 2}}[('b', {'val': 4}), ('a', {'val': 3}), ('c', {'val': 1})][('f', {'val': 2}), ('d', {'val': 3}), ('e', {'val': 4})]
16.字典
字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
键必须独一无二,可以用数、字符串或元组充当。但值则不必。
不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住
dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john', 'code': 6734, 'dept': 'sales', 'age': 12}dict['sex'] = "男"print dict['one']           # 输出键为'one' 的值print dict[2]               # 输出键为 2 的值print tinydict              # 输出完整的字典print tinydict.keys()       # 输出所有键print tinydict.values()     # 输出所有值print dict['sex']           #添加字典元素del tinydict['age']         #删除键为'age'print tinydict
This is oneThis is two{'dept': 'sales', 'age': 12, 'code': 6734, 'name': 'john'}['dept', 'age', 'code', 'name']['sales', 12, 6734, 'john']男{'dept': 'sales', 'code': 6734, 'name': 'john'}

文章参考来源:http://www.jb51.net/article/50689.htm,https://www.runoob.com/python/python-operators.html







原创粉丝点击