python3中统一都是input,python2中有raw_input等同于python3的input,另外python2中也有inputhtml
1.res=input("python3: ")python
2.res=raw_input("python2: ")shell
3.res=raw_input("python2: ")函数
1,2不管接收何种输入,都被存为字符串赋值给res,而3的意思是,用户输入何种类型,就以何种类型赋值给resspa
#!/usr/bin/env python name=input('请输入用户名:') print(name)
执行3d
C:\Users\Administrator>python D:\python_test\hello.py 请输入用户名:egon egon
===============================================================================code
输入密码时,若是想要不可见,须要利用getpass 模块中的 getpass方法,即:htm
#!/usr/bin/env python import getpass password=getpass.getpass('请输入密码:') print(password)
执行(在pycharm中没法执行,须要到终端中执行)blog
C:\Users\Administrator>python D:\python_test\hello.py 请输入密码: 123456
>>> 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 。utf-8
例子 2 能够看到:raw_input() 将全部输入做为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具备本身的特性,它返回所输入的数字的类型( int, float );同时在例子 1 知道,input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4 。
Python3 中的input至关于python2中的raw_input。
一、算数运算:
二、比较运算:
三、赋值运算:
四、位运算:
注: ~ 举例: ~5 = -6 解释: 将二进制数+1以后乘以-1,即~x = -(x+1),-(101 + 1) = -110
按位反转仅能用在数字前面。因此写成 3+~5 能够获得结果-3,写成3~5就出错了
五、逻辑运算:
and注解:
or注解:
and-or结合使用:
(1 and 'x') or 'y'
六、成员运算:
7.身份运算
8.运算符优先级:自上而下,优先级从高到低
1 单分支
2 多分支
需求1、用户登录验证
#!/usr/bin/env python name=input('请输入用户名字:') password=input('请输入密码:') if name == 'egon' and password == '123': print('egon login success') else: print('用户名或密码错误')
需求2、根据用户输入内容输出其权限
#!/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('普通用户')
while 循环
一、基本循环
while 条件: # 循环体 # 若是条件为真,那么循环体则执行 # 若是条件为假,那么循环体不执行
二、break
break用于退出本层循环
while True: print "123" break print "456"
三、continue
continue用于退出本次循环,继续下一次循环
while True: print "123" continue print "456"
四、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)
五、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 | 其它的字符以普通格式输出 |