python學習第一篇
阿新 • • 發佈:2018-03-04
else div sta width aps 技術 orm 提示 pre
一:python安裝及開發工具(pycharm)安裝
1.在python官網(https://www.python.org/)安裝最新版的python文件。
2.下載pycharm最新版並安裝(www.jetbrains.com)
二:第一個python程序(Hello World)
print(‘hello World‘)
三:變量
1.變量聲明:
1 name = ‘jone‘ 2 name2="jone"#字符變量要用‘xx‘或者"xx" 3 num = 1View Code
2.輸出變量:
1 print(name,name2) 2 #或者print(name+name2)
四:用戶交互式輸入
1 name = input(‘name:‘) 2 print("Hello",name)
顯示name:提示用戶輸入數據,假設為‘jone’將jone賦值給name這個變量
五:格式化輸出
1.
1 name = ‘jone‘ 2 age = ‘18‘ 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------‘‘‘+name+‘‘‘------- 6 name:‘‘‘+name+‘‘‘ 7 age:‘‘‘+age+‘‘‘ 8 address:‘‘‘+address+‘‘‘ 9 ‘‘‘10 print(info)
不推薦使用該方法且只能食醋出字符串,不能輸出int型
2.
1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------%s------- 6 name:%s 7 age:%d 8 address:%s 9 ‘‘‘%(name,name,age,address) 10 print(info)
每一個小括號裏的值對應一個格式輸出的值
3.
1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------{name1}------- 6 name:{name1} 7 age:{age1} 8 address:{address1} 9 ‘‘‘.format(name1 = name, 10 age1 = age, 11 address1 = address) 12 print(info)
4.
name = ‘jone‘ age = 18 address = ‘shandong‘ info =‘‘‘ ------{0}------- name:{0} age:{1} address:{2} ‘‘‘.format(name, age, address) print(info)
六:if...語句
1 #猜年齡 2 age_of_me = 20 3 age =int(input(‘guess age:‘)) 4 if age == age_of_me: 5 print(‘get it‘) 6 elif age > age_of_me: 7 print(‘too old‘) 8 else:print(‘too young‘)
七:while循環
1 #猜年齡 2 age_of_me = 20 3 i = 0 4 while i<3: 5 age = int(input(‘guess age:‘)) 6 if age == age_of_me: 7 print(‘get it‘) 8 break 9 elif age > age_of_me: 10 print(‘too old‘) 11 i += 1 12 continue 13 else: 14 print(‘too young‘) 15 i += 1 16 else: 17 print(‘game over‘) #只有循環正常結束才會運行,被berak中斷的不會執行
八:for循環:
1.
for i in range(10): print(i)
補充:range函數range(1,2,3) * range函數是一個用來創建算數級數序列的通用函數,返回一個[start, start + step, start + 2 * step, ...]結構的整
數序列
1.開始值,缺省默認為0
2.結束值,不可缺省
3.每個x個數返回一個
2.猜年齡
1 #猜年齡 2 age_of_me = 20 3 i = 0 4 for i in range(3): 5 age = int(input(‘guess age:‘)) 6 if age == age_of_me: 7 print(‘get it‘) 8 break 9 elif age > age_of_me: 10 print(‘too old‘) 11 i += 1 12 continue 13 else: 14 print(‘too young‘) 15 i += 1 16 else: 17 print(‘game over‘) #只有循環正常結束才會運行,被berak中斷的不會執行
python學習第一篇