1.输入及占位符练习
从命令行输入两个数字,打印两个数字的和及平均数。
#!/usr/bin/env python # coding: utf8 num1 = raw_input("Please input num1: ") num2 = raw_input("Please input num2: ") sum = int(num1) + int(num2) print '%s + %s = %s' % (num1, num2, sum) print '(%s + %s) / 2 = %s' % (num1, num2, sum * 1.0 / 2) >>> Please input num1: 1 Please input num2: 2 1 + 2 = 3 (1 + 2) / 2 = 1.5
1. 逻辑运算
布尔运算:True / False
None 0 "" [] () {} ==> False
>>> 1 < 2 True >>> 1 != 2 True >>> 1 == 2 False >>> a = 2 >>> b = 1 >>> a != b True >>> a <= b False
逻辑或/且,或(or),且(and)
或: a or b a = True, b = True a = True, b = False a = False, b = True a = False, b = False a or b:如果a或者b之间有一个为True,则结果为True 真值表: (a or b) a(True) a(False) b(True) True True b(False) True False a or b or c or .... or z (((a or b) or c) or ....) or z >>> True or True True >>> True or False True >>> False or True True >>> False or False False >>> True or False or True True >>> False or True or False True ======================================= 且: a and b a和b同时都为True,结果为True 真值表: (a and b) a(True) a(False) b(True) True False b(False) False False >>> True and True True >>> True and False False >>> False and True False >>> False and False False
用法:逻辑判断
if True/False: 为真(True)则执行语句 语句1 语句2 ... 2. if 支持嵌套 if 1 == 1: if 2 == 2: print("满足2=2条件,输出结果") else: print('不满足2=2条件,输出结果') else: print("不满足1==1条件,输出结果") 3. if elif inp = input('请输入级别:') if inp == "白银": print('玩个锤子') elif inp == "黄金": print('可以一战') elif inp == "钻石": print('还不错哦!') elif inp == "王者": print('大神别走,带带我!') else: print('滚') print('老司机快上车...') 补充:pass if 1==1: pass #不管拉不拉屎,先占住坑 else: print('一边凉快去')
2.if else练习
输入一个分数score 如果成绩在[0, 60) 打印不及格 [60, 70) 一般 [70, 80) 良好 [80, 90) 优良 [90, 100] 优秀 score <0 或者 > 100, print 你输入错误 >>> score = raw_input("Please input your score: ") score = int(score) if score < 0: print '你输入错误' elif score < 60: print '不及格' elif score < 70: print '一般' elif score < 80: print '良好' elif score < 90: print '优良' elif score <= 100: print '优秀' else: print '你输入错误'
逻辑非:not A
A True, not A => False A False, not A => True
2. while循环
while 判断条件: #如果判断条件是真,循环体的语句就会一起执行 语句1 语句2 修改判断条件中的变量,使得循环是可以结束的 #注意缩进 >>> i = 0 >>> while i < 10: ... print i ... i = i + 1 ... 0 1 2 3 4 5 6 7 8 9 >>> i = 0 while i < 3: i += 1 # i = i + 1 score = raw_input("Please input your score: ") score = int(score) if score < 0: print '你输入错误' elif score < 60: print '不及格' elif score < 70: print '一般' elif score < 80: print '良好' elif score < 90: print '优良' elif score <= 100: print '优秀' else: print '你输入错误'
3.while循环练习
往银行存10000元,利率是3.3%,存几年,银行账户能翻一翻?
year = 0 rate = 0.033 money = 10000 while money <= 20000: money = money * (1 + rate) year += 1 print year
输入pc跳出循环,求和及平均值
total = 0 value = 0 cnt = 0 while value != 'pc': total += int(value) cnt += 1 value = raw_input("Please input your number:") if cnt == 0: print 'total: %s, avg:0' % total else: print 'total: %s, avg: %s' % (total, total * 1.0 / cnt)
3. 列表(List)简介
list:有序的,可遍历, 可变(可变长、内容可变)集合
python列表可以包含混合类型的数据
chars = ['a', 'b', 'c'] 0 , 1 , 2 .... >>> chars = ['a', 'b', 'c'] >>> chars[0] 'a' >>> chars[1] 'b' >>> chars[2] 'c' >>> chars[6] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range cast = ["Cleese",'Palin','Jones','Idle'] print(cast) print(len(cast)) print(cast[1]) ['Cleese', 'Palin', 'Jones', 'Idle'] 4 Palin # append(): 从列表末尾增加一个数据项 cast.append("Gilliam") print(cast) ['Cleese', 'Palin', 'Jones', 'Idle', 'Gilliam'] # pop(): 从列表末尾删除数据 cast.pop() print(cast) ['Cleese', 'Palin', 'Jones', 'Idle'] # extend(): 扩展一个列表,各数据项之间腹膜逗号隔开,整个列表用中括号挺直 cast.extend(["anzhihe","eto"]) print(cast) ['Cleese', 'Palin', 'Jones', 'Idle', 'anzhihe', 'eto'] # remove(): 从列表中找到并删除一个特定的数据项 cast.remove("eto") print(cast) ['Cleese', 'Palin', 'Jones', 'Idle', 'anzhihe'] # insert(): 在某个特定的位置前面增加一个数据项 cast.insert(0,"Chapman") print(cast) ['Chapman', 'Cleese', 'Palin', 'Jones', 'Idle', 'anzhihe'] movies = ["The Holy Grail","The Life of Brian","The Meaning of Life"] movies.insert(1,1975) movies.insert(3,1979) #movies.insert(5,1983) movies.append(1983) print(movies) ['The Holy Grail', 1975, 'The Life of Brian', 1979, 'The Meaning of Life', 1983]
索引:(有序)_可以通过索引去访问元素
>>> print chars ['a', 'b', 'c'] >>> chars[1] = 'e' >>> print chars ['a', 'e', 'c']
列表的遍历:
>>> for x in chars: ... print x ... a e c
range
range(start, end)
[start, start+1, start+2, ....,end-1]
>>> range(0,10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in range(0, 10): ... print i ... 0 1 2 3 4 5 6 7 8 9
4.遍历列表统计次数
languages = ['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node'] cnt = 0 for l in languages: if l == 'js': cnt += 1 print 'js的次数:%s' % cnt
5.选出列表内的最大值
num_list = [-10, -2, -1, 0, 2, 11, 32, 148, 1, 20] num_max = None for num in num_list: if num_max is None: num_max = num elif num_max < num: num_max = num print 'num_max in num_list is :%s' % num_max
4. break & continue
break终止跳出整个循环,continue只终止本次循环
>>> for i in range(0, 10): ... if i == 5: ... break ... print i ... 0 1 2 3 4 >>> for i in range(0, 10): ... if i == 5: ... continue ... print i ... 0 1 2 3 4 6 7 8 9
6.判断是不是闰年
是100的倍数且是400的倍数
不是100的倍数,是4的倍数
输入年份判断是否是闰年,输入quit表示退出
while True: year = raw_input('Please input year:') if year == 'quit': break year = int(year) if year % 100 == 0 and year % 400 == 0: print '闰年' elif year % 100 != 0 and year % 4 == 0: print '闰年' else: print '非闰年'
5. dict简介
dict 就是 key value的键值对,通过key值访问,索引有意义
>>> user_data = {'name':'anzhihe'} #定义dict >>> print user_data['name'] #获取dict值 anzhihe >>> user_data['age'] = 0 #增加新值 >>> print user_data['age'] 0 >>> user_data['age'] = 20 #修改值 >>> print user_data['age'] 20
7.统计列表中每个字符串出现的次数
langs = ['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node'] langs_dict = {} for lang in langs: if lang not in langs_dict: langs_dict[lang] = 1 else: langs_dict[lang] += 1 print langs_dict