1 、 在inux和 UNIX系統安裝中(包括Mac OS X),Python的解釋器就已經存在了。輸入python命令使用 liuyangdeMacBook-Pro:~ liuyang$ python Python 2.7.10 (default, Jul 30 2016, 18:31:42) [ ...
1 、 在inux和 UNIX系統安裝中(包括Mac OS X),Python的解釋器就已經存在了。輸入python命令使用
liuyangdeMacBook-Pro:~ liuyang$ python
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
輸入一下命令,看其是否可以正常運行。
>>> print "hello,word"
hello,word
解釋器路徑
#!/usr/bin/env python
編碼格式
# -*- coding:utf8 -*-
ps.python3.5版本可以不指定編碼格式
python兩種執行方式
python解釋器 py文件路徑
python 進入解釋器:
實時輸入並獲取到執行結果
2 、數字和表達式:
>>> 1-2
-1
>>> 1+2
>>> 32784*13
>>> 1/2
試試“%” 取餘
>>> 6 % 3
>>> 6 / 3
>>> 6 % 3
>>> 7 / 3
>>> 7 % 3
>>> 13 % 9
>>> 0.75 % 0.5
0.25
再試試“ ** ” 冪運算
>>> 2*2*2
>>> 2**3
>>> 2**6
>>> -3**2
-9
>>> (-3)**2
2的3次方可以用乘方符(**)表示
3 執行一個用戶輸入操作
提醒用戶輸入:用戶和密碼 x=input("name:") y=input("pwd:")
獲取用戶名和密碼,檢測:用戶名=root 密碼=root if x=="root" and y =="root":
正確:登錄成功 print("success")
錯誤:登陸失敗 else :
pritn("error")
4變數名
變數名智能字母,數字,下劃線 並且不能以數字開頭,也不能使用python關鍵字取名.
5 條件語句
縮進用4個空格
a.
n1 = input('>>>')
if "alex" == "alex":
n2 = input('>>>')
if n2 == "確認":
print('alex SB')
else:
print('alex DB')
else:
print('error')
註意:
n1 = "alex" 賦值
n1 == 'alex' 比較,
b.
if 條件1:
pass
elif 條件2:
pass
elif 條件3:
pass
else:
pass
print('end')
c. 條件1
and or
if n1 == "root" or n2 == "root":
print('OK')
else:
print('OK')
PS:
pass 代指空代碼,無意義,僅僅用於表示代碼塊
6 死迴圈
while 1==1:
print('ok')
條件永遠成立,輸出的無限的ok
7 練習題
1、使用while迴圈輸入 1 2 3 4 5 6 8 9 10
n=1
while n<11:
if n==7:
pass:
else:
print(n)
n +=1
print("end")
2 求1-100的所有數的和
n=0
for i in range(101):
n=n+i
print(n)
3、輸出 1-100 內的所有奇數
for i in range(100)
if i%2==1:
print(i)
else:
pass
4、
for i in range(100)
if i%2==0:
print(i)
else:
pass
5、求1-2+3-4+5 ... 99的所有數的和
n=1
m=0
while n<100:
temp=n%2
if temp==0:
m=m-n
else:
m=m+n
n +=1
print(m)
7 用戶登陸(三次機會重試)
i=0
while i<3:
n1=input("輸入用戶名")
n2=input("輸入密碼")
print(i)
if n1=="root"and n2=="123"
print("登陸成功")
else:
print("密碼錯誤請重試")
i +=1