python-入门基础语法
2016-08-14 21:06
971 查看
输入输出
数据类型和变量
list
dic
set
列表操作
元组
集合
字典
if语句
循环语句
浮点数
字符串
布尔值:只有True和False两个值。布尔值可以用and、or和not运算
空值:None
变量:变量名必须是大小写英文、数字和_的组合,且不能用数字开头。变量本身类型不固定的语言称之为动态语言。
添加一个元素到列表的末尾。
list.extend(L)
将指定列表中的所有元素附加到另一个列表的末尾。
list.insert(i, x)
在给定位置插入一个元素。第一个参数是插入的元素的索引,所以 a.insert(0, x) 在列表的最前面插入
list.remove(x)
删除列表中第一个值为 x 的元素。如果没有这样的元素将会报错。
list.pop([i])
删除列表中指定位置的元素并返回它。如果未指定索引,a.pop()将删除并返回列表中的最后一个元素。(i 两边的方括号表示这个参数是可选的,而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示法)。
list.index(x)
返回列表中第一个值为 x 的元素的索引。如果没有这样的元素将会报错。
list.count(x)
返回列表中 x 出现的次数。
list.sort(cmp=None, key=None, reverse=False)
原地排序列表中的元素
list.reverse()
原地反转列表中的元素。
list.pop(index)
删除在下标为index的元素。
[‘a’, ‘b’, ‘c’] [‘a’, ‘b’, ‘c’, ‘d’] [‘a’, ‘b’, ‘c’, ‘d’, ‘a’] 2 [‘b’,
‘c’, ‘d’, ‘a’] [‘b’, ‘c’, ‘d’, ‘a’, [‘e’, ‘f’, ‘g’]] [‘e’, ‘f’, ‘g’]
[‘b’, ‘c’, ‘d’, ‘a’] 3 None [‘a’, ‘d’, ‘c’, ‘b’] None [‘a’, ‘b’, ‘c’,
‘d’]
del语句
从列表中根据索引而不是值来删除一个元素:del语句。这不同于有返回值的pop()方法。 del语句还可以用于从列表中删除切片或清除整个列表。
花括号或set()函数可以用于创建集合。注意:若要创建一个空的集合你必须使用set(),不能用{};后者将创建一个空的字典
遍历的方法
enumerate()函数
遍历字典时,使用iteritems()方法可以同时得到键和对应的值。
按排序顺序循环一个序列,请使用sorted()函数,返回一个新的排序的列表,同时保留源不变。
数据类型和变量
list
dic
set
列表操作
元组
集合
字典
if语句
循环语句
输入输出:
从raw_input()读取的内容永远以字符串的形式返回!age = int(raw_input()) ,int()把字符串转换为我们想要的整型。>>> myinput = raw_input("please input:") please input:hello word ! >>> print myinput hello word ! >>>
word = "Marry" for char in word: print char, #逗号表示,输出显示在同一行
数据类型和变量:
整数浮点数
字符串
布尔值:只有True和False两个值。布尔值可以用and、or和not运算
>>> True and False False
空值:None
>>> a = None >>> print a None >>> a >>>
变量:变量名必须是大小写英文、数字和_的组合,且不能用数字开头。变量本身类型不固定的语言称之为动态语言。
>>> a = 'first' >>> b = a >>> a = 'second' >>> print "a=",a,"b=",b a= second b= first >>> 注意除法: >>> 10.0/3 3.3333333333333335 >>> 10/3.0 3.3333333333333335 >>> 10/3 3 >>> 10%3 #求余数 1 >>>
list:
>>> name = ['John','Lily','Bob'] #列表 >>> print name ['John', 'Lily', 'Bob'] >>> name[1] #元素 'Lily' >>> name[-1] #元素 'Bob' >>> len(name) 3 >>>
dic
>>> dic = {'John':90,'Lily':98,'Bob':80} #字典dictionary,键值对 >>> dic['Bob'] #根据key取值 80 >>> dic {'Bob': 80, 'John': 90, 'Lily': 98} >>> dic {'Bob': 80, 'John': 90, 'Lily': 98} >>> dic[1] Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> dic[1] KeyError: 1 >>> dic['Amy'] = 66 #增加元素到字典 >>> dic {'Amy': 66, 'Bob': 80, 'John': 90, 'Lily': 98} >>> dic.get('Bob') #获取元素 80 >>> dic.get('Bob',-100) #获取元素,不存在这个key=‘Bob’则赋值-100 80 >>> dic {'Amy': 66, 'Bob': 80, 'John': 90, 'Lily': 98} >>> 'red' in dic #判断key是否在字典中 False >>> 'Bob' in dic #判断key是否在字典中 True
set
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。>>> myset = set([1,2,3]) #定义集合 >>> myset set([1, 2, 3]) >>> myset.add(4) #元组中增加key >>> myset set([1, 2, 3, 4]) >>> myset.remove(4) #元组中删除key >>> myset set([1, 2, 3]) >>> myset1 = set([1,5]) >>> myset|myset1 #集合并集 set([1, 2, 3, 5]) >>> myset & myset1 #集合交集 set([1]) >>>
列表操作
list.append(x)添加一个元素到列表的末尾。
list.extend(L)
将指定列表中的所有元素附加到另一个列表的末尾。
list.insert(i, x)
在给定位置插入一个元素。第一个参数是插入的元素的索引,所以 a.insert(0, x) 在列表的最前面插入
list.remove(x)
删除列表中第一个值为 x 的元素。如果没有这样的元素将会报错。
list.pop([i])
删除列表中指定位置的元素并返回它。如果未指定索引,a.pop()将删除并返回列表中的最后一个元素。(i 两边的方括号表示这个参数是可选的,而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示法)。
list.index(x)
返回列表中第一个值为 x 的元素的索引。如果没有这样的元素将会报错。
list.count(x)
返回列表中 x 出现的次数。
list.sort(cmp=None, key=None, reverse=False)
原地排序列表中的元素
list.reverse()
原地反转列表中的元素。
list.pop(index)
删除在下标为index的元素。
l = ['a','b','c'] print l l.append('d') print l l.insert(4,'a') count_a = l.count('a') print l print count_a l.remove('a') print l l1 = ['e','f','g'] l.append(l1) print l l2 = l.pop(4) print l2 print l index_a = l.index('a') print index_a print l.reverse(),l print l.sort(),l
[‘a’, ‘b’, ‘c’] [‘a’, ‘b’, ‘c’, ‘d’] [‘a’, ‘b’, ‘c’, ‘d’, ‘a’] 2 [‘b’,
‘c’, ‘d’, ‘a’] [‘b’, ‘c’, ‘d’, ‘a’, [‘e’, ‘f’, ‘g’]] [‘e’, ‘f’, ‘g’]
[‘b’, ‘c’, ‘d’, ‘a’] 3 None [‘a’, ‘d’, ‘c’, ‘b’] None [‘a’, ‘b’, ‘c’,
‘d’]
del语句
从列表中根据索引而不是值来删除一个元素:del语句。这不同于有返回值的pop()方法。 del语句还可以用于从列表中删除切片或清除整个列表。
mylist = [1,2,3,4,5,6,7,8] del mylist[0] print mylist #[2, 3, 4, 5, 6, 7, 8] del mylist[2:4] print mylist #[2, 3, 6, 7, 8] del mylist #之后再引用名称 a 将会出错(直到有另一个值被赋给它(a才能被引用))
元组
元组输出总有括号,输入可有可无。不能单独给元组中的一个元素赋值。>>> t = 123,'abc',8.0 >>> t[1] 'abc' >>> t (123, 'abc', 8.0) >>> tt = t,('gg') >>> tt ((123, 'abc', 8.0), 'gg') >>> t[1] = 'edf' Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> t[1] = 'edf' TypeError: 'tuple' object does not support item assignment >>> v = ([1,3,6],[2,4,6]) >>> v ([1, 3, 6], [2, 4, 6]) >>> x,y,z = t >>> x,y,z #序列分拆,序列分拆要求左侧变量的数目和序列中元素的数目相同。 (123, 'abc', 8.0) >>> x 123 >>> y 'abc' >>> z 8.0
集合
集合中的元素没有顺序且不会重复。集合的基本用途有成员测试和消除重复的条目。集合对象还支持并集、交集、差和对称差等数学运算。花括号或set()函数可以用于创建集合。注意:若要创建一个空的集合你必须使用set(),不能用{};后者将创建一个空的字典
>>> people = ['student','teacher','manageer'] >>> allpeople = set(people) >>> allpeople set(['manageer', 'teacher', 'student']) >>> 'teacher' in allpeople True >>> 'stu' in allpeople False >>> a = set('abcdabcd') >>> b = set('acmnz') >>> a set(['a', 'c', 'b', 'd']) >>> b set(['a', 'c', 'z', 'm', 'n']) >>> a-b set(['b', 'd']) >>> a | b set(['a', 'c', 'b', 'd', 'm', 'n', 'z']) >>> a & b set(['a', 'c']) >>> a ^ b #在a或b中,但不同时在两者中 set(['b', 'd', 'm', 'n', 'z'])
字典
序列由数字做索引,字典由 key 做索引,key可以是任意不可变类型;如果元组只包含字符串、 数字或元组,此元组可以用作key;如果元组直接或间接地包含任何可变对象,那么它不能用作key。不能用list作为key,因为列表可以用索引、切片或者append()和extend()方法原地修改。>>> ran = {'black':1010,'red':2332} >>> ran['blue'] = 4567 >>> ran {'blue': 4567, 'black': 1010, 'red': 2332} >>> ran['red'] 2332 >>> ran.keys() ['blue', 'black', 'red'] >>> dict([('a',1),('b',2)]) #dict()构造函数直接从键-值对序列创建字典 {'a': 1, 'b': 2} >>> dict(a = 1,b = 2) #如果键都是简单的字符串,通过关键字参数指定 键-值 对更为方便 {'a': 1, 'b': 2}
遍历的方法
enumerate()函数
>>> for k,v in enumerate(['av','bc','de']): print k,v 0 av 1 bc 2 de >>>
遍历字典时,使用iteritems()方法可以同时得到键和对应的值。
>>> dic = {'apple':'red','water':'white'} >>> for k,v in dic.iteritems(): print k,v water white apple red >>>
按排序顺序循环一个序列,请使用sorted()函数,返回一个新的排序的列表,同时保留源不变。
>>> mlist = ['v','a','e','f'] >>> for i in sorted(set(mlist)): print i a e f v >>>
if语句
if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> else: <执行4>
循环语句
for循环和while循环。names = ['John','Bob','Amy'] for name in names: print name sum = 0 for x in range(101): sum = sum+x print sum #计算1+2+...+100的和 total = 0 n = 0 while n<=100: total = total+n #计算1+2+...+100的和 n = n+1 print 'total=',total,'n=',n John Bob Amy sum= 5050 total= 5050 n= 101
相关文章推荐
- python3 入门 (一) 基础语法
- Python简单入门语法python基础入门知识
- Python入门(基础语法)
- Python 3.x 入门教程、基础语法、基础运算符
- Python入门基础语法之序列、字典、函数、递归
- python3 入门 (一) 基础语法
- python入门基础语法
- python机器学习基础语法入门
- python入门(一):基础语法
- Python教程入门到精通:千锋Python语法基础学习视频教程
- Python从入门到PY交易,基础语法,散记(一)
- Python3 (入门0) Python基础语法
- Python3 基础语法,从入门到精通一
- Python入门基础语法之字符串、列表、元组
- Python学习笔记(一):基础语法、变量类型、运算符(快速入门篇)
- Python从入门到PY交易,基础语法,散记(二)
- Python3基础语法 从入门到精通三
- Python入门(二)中文编码与基础语法
- 1-Python3从入门到实战—基础之语法
- Python入门(2)-基础语法(强调几点)