运行python程序的方式可分为:
1.交互式(能快速得到结果,但是无法保存文件)
2.python程序路径
Python解释器启动:
1. 先启动python解释器
2. 将盘里的文件读入内存
3.解释
输入输出:
1.输出:
#!/usr/bin/env python
#coding:utf-8
print('hello world')
#输出字符串‘hello world’
print('hello','world')
#输出的也是‘hello world’,这里能连接成一串,遇到逗号会输出一个空格
2.输入:input()接收用户输入,把用户输入的内容转成字符串
View Code
变量
变量是一种可以反应状态变化的机制。变量的三个特征:id、type、value。变量名是对value的引用。
自动的垃圾回收机制是python的特性,而垃圾的定义是value身上的引用计数为0。自动回收机制并不是随时回收,而是定期回收。
变量的命名规范:
- 不要用关键字['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
- 只能是字母、数字、下划线的任意组合
- 第一个字符不能是数字
- 不要用中文和拼音
is身份运算比较的是id
==比较value
变量的赋值:
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> x=1 >>> y=x >>> print(x,y) 1 1 >>> x=2 >>> print(x,y) 2 1
注释:
#!/usr/bin/env python #coding:utf-8 #单行注释 """ 多行注释 """
基本数据类型
1.数字(小数字池:-5 ~ 257)
int整型:
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long长整型:
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float浮点型
complex复数
bool布尔值:
True
False:空、0、None
2.str字符串(支持加法和乘法,但是不建议用加法,因为字符串的加法运算是开辟新的内存空间存放结果,效率低)
3.列表(值可以是任意类型)
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> x=['aa','bb',123] >>> y=list(['ac','bc',234]) >>> print(x,y) ['aa', 'bb', 123] ['ac', 'bc', 234]
4.字典(字典的键必须是不可变类型,值可以是任意类型)
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x={'name':'张三','agge':18}
>>> print(x)
{'name': '张三', 'agge': 18}
>>> y={1:23}
>>> print(y)
{1: 23}
>>> z={None:12}
>>> print(z)
{None: 12}
>>> h={0:0}
>>> print(h)
{0: 0}
>>> a={False:0}
>>> print(a)
{False: 0}
>>> c={[1,2,'a']:['b','c']}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
可变类型和不可变类型的区别是当id不变的情况下,值可不可以变。可变类型有字典和列表,不可变类型有数字、字符串。
If条件语句
name=input('name>>>')
pwd=input('password>>>')
if name == 'aa' and pwd == '123':
print('welcome')
else:
print('sorry')
while循环
1.用户验证:
name = 'seven'
pwd = 123
username = input('please input username:')
password = input('please input password:')
password = int(password)
if username==name and password==pwd:
print('you are successful')
else:
print('input error')
2.用户有三次验证机会
name = 'seven'
pwd = 123
count=0
while count<3:
count+=1
username = input('please input username:')
password = input('please input password:')
password = int(password)
if username==name and password==pwd:
print('you are successful')
break
else:
print('failed')
3.多用户验证,每个用户有三次验证机会
dic={
'kara1':{'pwd':'123','count':0},
'kara2':{'pwd':'123','count':0},
'kara3':{'pwd':'123','count':0}
}
while True:
username=input('username>>>')
if not username in dic:
print('用户不存在')
continue
if dic[username]['count']>2:
print('验证次数过多')
break
dic[username]['count'] += 1
password=input('password>>>')
if password==dic[username]['pwd']:
print('login in')
break
else:
print('error')
升级版:
#在同目录下创建一个db.txt文件
dic={
'kara1':{'pwd':'123','count':0},
'kara2':{'pwd':'123','count':0},
'kara3':{'pwd':'123','count':0}
}
count=0
while True:
username=input('username>>>')
if username not in dic:
print('用户不存在')
continue
with open('db.txt','r') as f:
lock_users=f.read().split('|')
if username in lock_users:
print('用户%s已被锁定'%username)
break
if dic[username]['count']>2:
print('验证次数过多,已被锁定')
with open('db.txt','a') as f:
f.write('%s|'%username)
break
dic[username]['count'] += 1
password=input('password>>>')
if password==dic[username]['pwd']:
print('login in')
break
else:
print('error')