1. 程式人生 > >學習Python第二天

學習Python第二天

raw true else 無效 單行註釋 time 3.0 == 輸入

1/創建xxx.py文件
ps:不要有中文路徑(不然會出現不可預知的錯誤)
2/寫代碼
a.頭部兩行
#!/usr/bin/env python 在linux上有用
#-*- coding:utf-8 -*-
b.寫功能代碼
3、執行代碼
a.打開終端(cmd)
b.python 代碼文件的路徑

4、變量名:只能有數字、字母、下劃線組成、不能以數字開頭、不能是Python內部的關鍵字
5、基本數據類型
數字:
age=123
字符串:
a1="sd"
a1=‘sds‘
a1="""fdsfds"""
布爾值:
True/False
a4=True/False
6、if 條件: (同級別縮進必須一樣)
條件成立執行此條語句(要加上縮進)
else:
條件不成立執行此條語句(要加上縮進)
True False
1 > 2 n1 > n2 n1 == n2 n1 != n2
and or

例:
name=input("請輸入用戶名")
pwd=input("請輸入密碼")
if name==‘aa‘ and pwd==‘123‘:
print(‘y‘)
else:
print("n")
ps:Python2.7裏面不能用input去接受字母,只能接受數字,要接受字母必須用raw_input
Python3.0以後的版本input可以接受數字和字母,但是不能寫raw_input;(在環境變量裏把2.7的路徑改成3.0以後的版本)

例2:
inp = input("...")
if inp == "1" or inp == "2":
print("1")
elif inp == "3" or inp == "4": (或者)
print("4")
else:
print(inp)


7、註釋:
#無效的內容,只做註釋用(單行註釋)
"""
多行註釋
"""

8、載入外部文件import

9、while 條件:
代碼塊
例:
a = 0
while a <= 10:
print(a)
a+=1
print(a)

#break跳出所有循環
b = 0
while True:
print(b)
if b == 10:
break
b+=1
print(b)

#continue終止當前循環 進行下次循環
例:輸出1/2/3/4/5/6/8/9
import time
a = 0
while True:
time.sleep(0.3)
if a == 6:
a = 7
elif a == 9:
break
a+=1
print(a)
#print(a)


b = 1
while True:
if b == 7:
b += 1
continue
print(b)
if b ==10:
break
b+=1

學習Python第二天