1.print和import1.1 print 略1.2 import(1)impore somemodule (2)from somemodule import somefunction(3)from somemodule import somefunction,anotherfunction(4)from somemodule import*(5)import somemodule as somename #为整个模块提供别名(6)from somemodule import somefunction as somename #为某个模块的某个函数提供别名 2.赋值2.1 序列解包:将多个值的序列解开,然后放到变量的序列中。2.2 链式赋值:将同一个值赋给多个变量。2.3 增量赋值:eg:x+=1 3.语句块:条件为真时执行或执行多次的一组语句,由代码前放置空格缩进语句创建。 4.条件语句4.1 布尔变量 python中的假:False None 0 “” () [] {}4.2 if语句4.3 else语句4.4 elif语句4.5 嵌套代码块4.6 条件(1)比较运算符
(2)相等运算符 ==
(3)同一性运算符 is
(4)成员资格运算符 in
(5)字符串和序列比较
(6)布尔运算符
4.7 断言: assert 放入检查点,确保某个条件一定为真才能让程序正常工作
5. 循环
5.1 while循环
5.2 for循环
5.3 循环遍历字典元素
5.4 迭代工具
(1) 并行迭代:同时迭代两个序列
(2)按索引迭代
(3)翻转和排序迭代
5.5 跳出循环
(1) break
(2) continue
(3) while True/break
5.6 循环中的else子句
6.列表推导式-轻量级循环
7.pass,del,exec
本章相关代码:# -*- coding: utf-8 -*- #1.print 与import#1.1 print 使用逗号输出print ‘Age:‘,42 #Age: 42name=‘xiaming‘age=‘42‘print name,age #xiaming 42 #2.赋值语句#2.1.序列解包:将多个值的序列解开,然后放到变量的序列中x,y,z=1,2,3print x,y,z #1 2 3x,y=y,x #2 1 3print x,y,zv=1,2,3x,y,z=vprint x #1 #2.2.链式赋值 将同一个值赋给多个变量的捷径x=y=4print x,y #2.3.增量赋值x=2x+=1x*=2a=‘bo‘a+=‘x‘a*=2print x,a #6,boxbox #3:语句块:缩排 #4.条件与条件语句#4.1.布尔变量的作用 #4.2 if elif else语句name=raw_input(‘what is your name?‘)if name.endswith(‘ming‘): print ‘hello,ming‘elif name.endswith(‘hong‘): print ‘hello,hong‘else: print ‘hello‘ #4.3 嵌套语句num=raw_input(‘input a number‘)if num>10: if num<20: print ‘10<num<20‘ else: print ‘num>=20‘else: print ‘num<=10‘ #5.循环#5.1.while循环x=1while x<=10: print x x+=1 name=‘‘while not name: name=raw_input(‘enter your name:‘)print ‘Hello,%s!‘%name #Hello,ming! #5.2.for 循环names=[‘ming‘,‘hong‘,‘qiang‘,‘qing‘]for n in names: print n print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] rang(0,10)for num in range(1,10): print num #5.3.循环遍历字典元素d={‘x‘:1,‘y‘:2,‘z‘:3}for key in d: print key,d[key] #y 2 x 1 z 3 #5.4.一些迭代工具#5.4.1 并行迭代names=[‘ming‘,‘hong‘,‘qiang‘,‘qing‘]ages=[10,11,12,13]for i in range(len(names)): print names[i],‘is‘,ages[i],‘years old‘ #ming is 10 years old... print zip(names,ages) #[(‘ming‘, 10), (‘hong‘, 11), (‘qiang‘, 12), (‘qing‘, 13)] 将两个序列压缩,返回一个元组的列表for n,a in zip(names,ages): print n, ‘is‘,a, ‘years old‘ #ming is 10 years old... #5.4.2按索引迭代#5.4.3翻转和排序迭代 #5.5.跳出循环#5.5.1breakfrom math import sqrtfor n in range(99,0,-1): print n root=sqrt(n) if root==int(root): print "the biggst is %s"%n break #5.5.2 continue #5.5.3 while True/breakwhile True: word=raw_input(‘Please enter a word:‘) if not word:break print ‘The word was ‘+word #5.6 循环体中的else子句from math import sqrtfor n in range(99,80,-1): print n root=sqrt(n) if root==int(root): print "the biggst is %s"%n break else: print "didn‘t find it" #6 列表推导式-轻量级循环:利用其它列表创建新列表print [x*x for x in range(10)] #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]print [x*x for x in range(10) if x%3==0] #[0, 9, 36, 81]print [(x,y,z) for x in range(2) for y in range(2) for z in range(2)]#[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] #7 三个语句:pass,del,exec#7.1 pass :#7.2 del:删除不再使用的对象#7.3 exec:执行一系列Py语句 exel:计算Py表达式,并且返回结果值
时间: 2024-10-21 06:35:33