Python(輸入、輸出;簡單運算符;流程控制)
一 輸入輸出
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 。
二 簡單的運算符
算數運算;邏輯運算;比較運算;關系運算;
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()
Python(輸入、輸出;簡單運算符;流程控制)