给数据起一个名字,在后续的代码中使用变量名访问数据python
In [1]: pi = 3.1415926 In [2]: radius = 10 In [3]: print(2 * pi * radius) 62.831852 In [4]: print(pi * radius * radius) 314.15926 In [5]: radius = 20 In [6]: print(2 * pi * radius) 125.663704 In [7]: print(pi * radius * radius) 1256.63704
让计算机作一件事程序员
name = input('请输入你的名字:') print('你的名字是:', name)
#encoding: utf-8 print('Hello World')
像年龄、身高、体重、分数、圆周率这样的数字vim
加(+)、减(-)、乘(*)、除(/)、整除(//)、余(%)、幂(**)eclipse
注:Python2.7中,整数除(/)整数得整数(能够经过导入from__future__ import division 修改其行为与Python3.5一致)python2.7
像名字、一句话描述这样的文本编辑器
In [11]: print(type(1)) <class 'int'> In [12]: print(type(1.2)) <class 'float'> In [13]: print(type('')) <class 'str'> In [14]:
In [16]: print(type(float(1))) <class 'float'> In [17]: print(type(float('1.2'))) <class 'float'>
In [14]: print(type(int(1.2))) <class 'int'> In [15]: print(type(int('1'))) <class 'int'>
In [18]: print(type(str(1))) <class 'str'> In [19]: print(type(str(1.2))) <class 'str'>
#encoding: utf-8 name = "Li Lei", age = 20 print("my name is", name, ",", "and I'm", age, "years old")
#encoding: utf-8 name = input("your name is:"); age = input("your age is:") print("my name is", name, ",", "and I'm", age, "years old")
#encoding: utf-8 n1 = int(input("a number:")); n2 = int(input("a number:")) print(n1 + n2, n1 - n2, n1 * n2, n1 / n2)
表示真假,只有True/False函数
In [22]: is_boy = True In [23]: is_girl = False In [24]: print(is_boy, is_girl) True False
In [39]: bool(0) Out[39]: False In [40]: bool(0.0) Out[40]: False In [41]: bool(0.1) Out[41]: True ...
笑话:测试
程序员的妻子叫程序员去买一斤包子,若是看到卖西瓜的,买一个。一下子,程序员拿着一个包子回来了,妻子问他为何只买一个包子,答曰:看到卖西瓜的了。编码
解释:atom
买一斤包子,若是看到西瓜,就买一个西瓜
若是看到西瓜就买一个包子,不然就买一斤包子
妻子:
#encoding: utf-8 print(">>买一斤包子") show = input("看到西瓜了吗?:") if show == "看到了": print(">>买一个西瓜")
丈夫:
#encoding: utf-8 show = input("看到西瓜了吗?:") if show == "看到了": print(">>买一个包子") else: print(">>买一斤包子")
帮助:http://www.pythontutor.com
判断分数若是>=90分则打印优秀
判断分数若是>=80分则打印良好
判断分数若是>=60分则打印及格
不然打印不及格
#encoding: utf-8 score = input("请输入你的分数:") score = float(score) if score >= 90: print("优秀") elif score >= 80: print("良好") elif score >= 60: print("及格") else: print("不及格")
四年一闰,百年不闰;四百年再闰
示例:
1900
2000
2004
#encoding: utf-8 year = input("请输入年份:") year = int(year) if year % 4 == 0 and year % 100 !=0: print("闰年") elif year % 400 == 0: print("闰年") else: print("不是闰年")
while condition: code
#encoding: utf-8 idx = 1 idx_sum = 0 while idx <= 100: idx_sum = idx_sum + idx idx = idx + 1 print("sum is:", idx_sum)
#encoding: utf-8 input_test = input("请输入一个数字或exit(结束):") input_sum = 0 input_count = 0 while input_test != "exit": input_sum += float(input_test) input_count += 1 input_test = input("请输入一个数字或exit(结束):") if input_count == 0: print("sum: 0, avg: 0") else: print("sum:", input_sum, ", avg:", input_sum / input_count)
#encoding: utf-8 idx = 0 max_idx = 5 print("break") while idx <= max_idx: idx += 1 if idx == 3: break print(idx)
#encoding: utf-8 idx = 0 max_idx = 5 print("continue") while idx <= max_idx: idx += 1 if idx == 3: continue print(idx)
nums = [1, 5, 6, 3, 2, 5]
获取序列中第n个元素
nums[n - 1]
遍历集合中全部元素
#enconding: utf-8 nums = [1, 5, 6, 3, 2, 5] for num in nums: print(num)