一 輸入輸出
python3中統一都是input,python2中有raw_input等同於python3的input,另外python2中也有input
1.res=input("python3: ")
2.res=raw_input("python2: ")
3.res=raw_input("python2: ")
1,2無論接收何種輸入,都被存為字符串賦值給res,而3的意思是,用戶輸入何種類型,就以何種類型賦值給res
#!/usr/bin/env python
name=input('請輸入用戶名:')
print(name)
執行
C:\Users\Administrator>python D:\python_test\hello.py 請輸入用戶名:egon egon
===============================================================================
輸入密碼時,如果想要不可見,需要利用getpass 模塊中的 getpass方法,即:
#!/usr/bin/env python
import getpass
password=getpass.getpass('請輸入密碼:')
print(password)
執行(在pycharm中無法執行,需要到終端中執行)
C:\Users\Administrator>python D:\python_test\hello.py 請輸入密碼: 123456
![]()
Python2 的input,輸入什么類型,就是什么類型(輸入字符串應該加引號‘’,不加引號會識別成變量名,找不到就報錯)
>>> raw_input_A = raw_input("raw_input: ") raw_input: abc >>> input_A = input("Input: ") Input: abc Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> input_A = input("Input: ") File "<string>", line 1, in <module> NameError: name 'abc' is not defined >>> input_A = input("Input: ") Input: "abc" >>>
>>> raw_input_B = raw_input("raw_input: ") raw_input: 123 >>> type(raw_input_B) <type 'str'> >>> input_B = input("input: ") input: 123 >>> type(input_B) <type 'int'> >>>
例子 1 可以看到:這兩個函數均能接收 字符串 ,但 raw_input() 直接讀取控制台的輸入(任何類型的輸入它都可以接收)。而對於 input() ,它希望能夠讀取一個合法的 python 表達式,即你輸入字符串的時候必須使用引號將它括起來,否則它會引發一個 SyntaxError 。
例子 2 可以看到:raw_input() 將所有輸入作為字符串看待,返回字符串類型。而 input() 在對待純數字輸入時具有自己的特性,它返回所輸入的數字的類型( int, float );同時在例子 1 知道,input() 可接受合法的 python 表達式,舉例:input( 1 + 3 ) 會返回 int 型的 4 。
Python3 中的input相當於python2中的raw_input。
二 簡單的運算符
1、算數運算:

2、比較運算:

3、賦值運算:
4、位運算:

注: ~ 舉例: ~5 = -6 解釋: 將二進制數+1之后乘以-1,即~x = -(x+1),-(101 + 1) = -110
按位反轉僅能用在數字前面。所以寫成 3+~5 可以得到結果-3,寫成3~5就出錯了
5、邏輯運算:

and注解:
- 在Python 中,and 和 or 執行布爾邏輯演算,如你所期待的一樣,但是它們並不返回布爾值;而是,返回它們實際進行比較的值之一。
- 在布爾上下文中從左到右演算表達式的值,如果布爾上下文中的所有值都為真,那么 and 返回最后一個值。
- 如果布爾上下文中的某個值為假,則 and 返回第一個假值
or注解:
- 使用 or 時,在布爾上下文中從左到右演算值,就像 and 一樣。如果有一個值為真,or 立刻返回該值
- 如果所有的值都為假,or 返回最后一個假值
- 注意 or 在布爾上下文中會一直進行表達式演算直到找到第一個真值,然后就會忽略剩余的比較值
and-or結合使用:
- 結合了前面的兩種語法,推理即可。
- 為加強程序可讀性,最好與括號連用,例如:
(1 and 'x') or 'y'
6、成員運算:
7.身份運算

8.運算符優先級:自上而下,優先級從高到低


三 流程控制
3.1 條件語句
1 單分支
2 多分支
需求一、用戶登陸驗證
#!/usr/bin/env python
name=input('請輸入用戶名字:')
password=input('請輸入密碼:')
if name == 'egon' and password == '123':
print('egon login success')
else:
print('用戶名或密碼錯誤')
需求二、根據用戶輸入內容輸出其權限
#!/usr/bin/env python
#根據用戶輸入內容打印其權限
'''
egon --> 超級管理員
tom --> 普通管理員
jack,rain --> 業務主管
其他 --> 普通用戶
'''
name=input('請輸入用戶名字:')
if name == 'egon':
print('超級管理員')
elif name == 'tom':
print('普通管理員')
elif name == 'jack' or name == 'rain':
print('業務主管')
else:
print('普通用戶')
3.2 循環語句
while 循環
1、基本循環
while 條件:
# 循環體
# 如果條件為真,那么循環體則執行
# 如果條件為假,那么循環體不執行


2、break
break用於退出本層循環
while True:
print "123"
break
print "456"
3、continue
continue用於退出本次循環,繼續下一次循環
while True:
print "123"
continue
print "456"
4、tag
#!/usr/bin/env python #_*_coding:utf-8_*_ # while True: # username=input('username: ') # password=input('password: ') # if username == 'egon' and password == '123': # while True: # cmd=input('>>: ') # if cmd == 'q': # break # print('------>%s' %cmd) # break tag=True while tag: username=input('username: ') password=input('password: ') if username == 'egon' and password == '123': while tag: cmd=input('>>: ') if cmd == 'q': tag=False continue print('------>%s' %cmd)
5、for循環
for i in range(1,10): for j in range(1,i+1): print('%s*%s=%s' %(i,j,i*j),end=' ') print()
# -*-coding:UTF-8-*- #99乘法表 for i in range(1,10): for n in range(1,i+1): print("%s*%s=%s" %(n,i,i*n),end=' ') #print默認后面跟一個\n換行,end的意思是將最后默認的\n換成 end 指定的內容。 print()
------------------------------------------------------------
D:\Python36\python.exe D:/py/train.py 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 Process finished with exit code 0
| 轉義字符 | 描述 |
|---|---|
| \(在行尾時) | 續行符 |
| \\ | 反斜杠符號 |
| \' | 單引號 |
| \" | 雙引號 |
| \a | 響鈴 |
| \b | 退格(Backspace) |
| \e | 轉義 |
| \000 | 空 |
| \n | 換行 |
| \v | 縱向制表符 |
| \t | 橫向制表符 |
| \r | 回車 |
| \f | 換頁 |
| \oyy | 八進制數,yy代表的字符,例如:\o12代表換行 |
| \xyy | 十六進制數,yy代表的字符,例如:\x0a代表換行 |
| \other | 其它的字符以普通格式輸出 |


