一 输入输出
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 == '':
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 | 其它的字符以普通格式输出 |