《南京大学张莉用python玩转数据》由会员分享,可在线阅读,更多相关《南京大学张莉用python玩转数据(47页珍藏版)》请在金锄头文库上搜索。
1、Python面面 Multi-dimensional View of Python 观 Department of Computer Science and Technology Department of University Basic Computer Teaching Nanjing University 条件 用Python玩转数据 2 Nanjing University if 语句 # Filename: ifpro.py sd1 = 3 sd2 = 3 if sd1 = sd2: print “the squares area is:%d“ % (sd1*sd2) File i
2、f expression : expr_true_suite 语 法 条件表达式: 比较运算符 成员运算符 逻辑运算符 expression expression 条 件 为 True时执行的代码块 代码块必须缩进(通 常为4个空格) expr_true_suite 3 Nanjing University else 语句 # Filename: elsepro.py sd1 = int(raw_input(the first side:) sd2 = int(raw_input(the second side:) if sd1 = sd2: print “the squares area i
3、s:%d“ %(sd1*sd2) else: print “the rectangles area is:%d“ %(sd1*sd2) File 4 if expression : expr_true_suite else: expr_false_suite 语 法 expression 条 件 为 False时执行的代码 块 代码块必须缩进 else语句不缩进 expr_false_suite the first side:4 the second side:4 the squares area is:16 Input and Output Nanjing University elif 语
4、句 5 if expression : expr_true_suite elif expression2: expr2_true_suite : : elif expressionN : exprN_true_suite else: none_of_the_above_suite 语 法 expression2为True时 执行的代码块 expr2_true_suite expressionN 为 True 时执行的代码块 exprN_true_suite none_of_the_above_s uite是以上所有条件都 不满足时执行的代码块 else Nanjing University e
5、lif 语句 # Filename: elifpro.py k = raw_input(input the index of shape:) if k = 1: print circle elif k = 2: print oval elif k = 3: print rectangle elif k = 4: print triangle else: print you input the invalid number File input the index of shape:3 rectangle Input and Output input the index of shape:8 y
6、ou input the invalid number Input and Output 6 Nanjing University 条件嵌套 同等缩进为同一条件结构 # Filename: ifnestpro.py k = raw_input(input the index of shape:) if k = 1: print circle elif k = 2: print oval elif k = 3: sd1 = int(raw_input(the first side:) sd2 = int(raw_input(:the second side) if sd1 = sd2: prin
7、t “the squares area is:%d“ %(sd1*sd2) else: print “the rectangles area is:%d“ %(sd1*sd2) print rectangle elif k = 4: print triangle else: print you input the invalid number File input the index of shape:3 the first side:3 the second side:4 the rectangles area is:12 Input and Output input the index o
8、f shape:2 oval Input and Output 7 Nanjing University 猜数字游戏 程序随机产生一个0300间的整数, 玩家竞猜,系统给出“猜中”、“太 大了”或“太小了”的提示。 # Filename: guessnum1.py from random import randint x = randint(0, 300) print Please input a number between 0300: digit = input() if digit = x : print Bingo! elif digit x: print Too large,plea
9、se try again. else: print Too small,please try again. File 8 Nanjing University RANGERANGE和 XRANGEXRANGE 用Python玩转数据 9 Nanjing University range() 10 range (start, end, step=1) range (start, end) range (end) 语 法 起始值(包含) start 终值(不包含) end 步长(不能为0) step 生成一个真实的列表 不包含end的值 range (start, end, step=1) 缺省s
10、tep值为1 range (start, end) 缺省了start值为0,step为1 range (end) range(3,11,2) 3, 5, 7, 9 range(3,11) 3, 4, 5, 6, 7, 8, 9, 10 range(11) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Source Nanjing University xrange() 异同 range() xrange() 语法 一样 返回 列表 生成器 生成 真实列表 用多少生成多少 xrange(3,11,2) xrange(3, 11, 2) print xrange(3,11,2)
11、 xrange(3, 11, 2) for i in xrange(3,11,2): print i Source 11 Output: 3 5 7 9 在Python 3.x中不支持xrange() 函数,都使用range()函数, 其返回值为range对象,并且 需要显式调用,常用 list(range(10)形式 Nanjing University 循环 用Python玩转数据 12 Nanjing University while 循环 sumA = 0 j = 1 while j sumA 45 j 10 Source 13 while expression: suite_to_r
12、epeat 语法 条件表达式 当expression值为True 时执行suite_to_repeat 代码块 expression Nanjing University for 循环(一) 14 for iter_var in iterable_object: suite_to_repeat 语 法 遍历一个数据集内的成员 在列表解析中使用 生成器表达式中使用 可以明确循环的次数 String List Tuple Dictionary File iterable_object Nanjing University for 循环(二) 字符串就是一个iterable_object range
13、()返回的也是 iterable_object s = python for c in s: print c p y t h o n for i in range(3,11,2): print i, 3 5 7 9 Source 15 Python 3.x中print函数用 法与Python 2.x中的语句 用法也有所改变,例如 此处变成print( i, end = ) Nanjing University 猜数字游戏 16 程序随机产生一个0300间的整数, 玩家竞猜,允许猜多次,系统给出 “猜中”、“太大了”或“太小了” 的提示。 # Filename: guessnum2.py fro
14、m random import randint x = randint(0, 300) for count in range(0,5): print Please input a number between 0300: digit = input() if digit = x : print Bingo! elif digit x: print Too large,please try again. else: print Too small,please try again. File Nanjing University 循环中的 BREAK,CONTINUEBREAK,CONTINUE
15、和ELSEELSE 用Python玩转数据 17 Nanjing University break 语句 break语句终止当前循环,转而执行循环之后的语句 # Filename: breakpro.py sumA = 0 i = 1 while True: sumA += i i += 1 if sumA 10: break print i=%d,sum=%d % (i,sumA) File 18 Output: i=6,sumA=15 Nanjing University while 循环和break 输出2-100之间的 素数 # Filename: prime.py from math
16、 import sqrt j=2 while j k=5 for i in range(1,10): if k=3: break else: print i 9 Source 24 Nanjing University 自定义函数 用Python玩转数据 25 Nanjing University 函数 函数调用之前必须先定义 26 内置 函数 自定 义 函数 Nanjing University 语 法 自定义函数的创建 def addMe2Me(x): apply operation + to argument return (x+x) Source 27 def function_name(arguments): “op