給數據起一個名字,在後續的代碼中使用變量名訪問數據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)