(1):易学:python 关键字少、结构简单、语法清晰
易读:没有其他语言通常用来访问变量、定义代码块和进行模式匹配的命令式符号
(2):内存管理器:内存管理是由 python 解释器负责的
>>> py_str = 'python'
>>> len(py_str)----- 输出变量字符的长度
6
>>> 't' in py_str---------"t" 是否在 py_str 变量中
True
>>> 'th' in py_str
True
>>> 'to' not in py_str
True
>>> py_str[0]---- 从左向右取出第一个字符
'p'
>>> py_str[5]
'n'
>>> py_str[-1]
'n'
>>> py_str[3:]
'hon'
>>> py_str[3:5]
'ho'
>>> py_str[1:6]
'ython'
>>> py_str[0:6]
'python'
>>> py_str[0:]
'python'
>>> py_str[:2]
'py'
>>> py_str[::2]--------- 步长值
'pto'
>>> py_str[::3]
'ph'
>>> py_str[1::3]
'yo'
>>> py_str[::-1]
'nohtyp'
>>> py_str + 'cool'------ 相加表示拼接
'python cool'
>>> py_str.upper()------ 转换大写
'PYTHON'
>>> py_str.lower()------ 转换小写
'python'
>>> py_str.isdigit()---- 判断是否是数字
False
>>> py_str.center(50,'#')------------ 居中对齐
'######################python######################'
>>> py_str.ljust(50)------ 左对齐
'python
列表
>>> alist = [10, 20,'dc','alice', [1,3]]
字典表示
>>> adict = {'name': 'hanjie', 'age':22}
>>> len(adict)--------- 两个字符长对,看中间的逗号分隔
2
>>> adict-------- 箭直对类型
{'age': 22, 'name': 'hanjie'}
>>> 'name' in adict----- 判断'name'是否在 adict
True
>>> adict['age'] = 21-------- 改变'age'的值
>>> adict
{'age': 21, 'name': 'hanjie'}
>>> adict['email'] = 'hj@qq.com'
>>> adict
{'age': 21, 'name': 'hanjie', 'email': 'hj@qq.com'}------ 增加变量
if 语句:
#coding: utf8
import getpass------------------ 表示 python 中的一个模块
username = raw_input("请输入用户名:")
passwd = getpass.getpass("请输入用户密码:")--------- 第一个 getpass 表示模块,第二个表示模块中的一个方法
if username == "bob" and passwd == "123456":---------python 中的并且和或者用 "and"、"or" 表示
print "Login successful"
else:
print "Login incorrect"
PYTHON 解释器中的判断
>>> if 5 > 8:(python 判断是必须要冒号分隔)、if、else 是评级;两个 print 是一个评级;输入 else 时必须要用冒号分隔
... print 'yes'
... else:
... print 'no'
...
no
在 python 中随机猜一个数:
#coding: utf8
import random
num = random.randint(1, 10)
answer = int(raw_input(" 请猜一个数: "))
if answer > num:
print "猜大了"
elif answer < num:
print "猜小了"
else:
print "猜对了"
print num
Python 中三引号的区别:列 b = "hello,\n
world"
c="""hello'
world!""" #三引号就相当于双引号里面的 \ n,字符串越多 \ n 就多,使用起来不方便,也不方便查看, 所以就使用三引号来实现
If 语句的写法:
#coding: utf8
import random #导入 random 模块,是产生随机数的模块
all_choices = ['石头', '剪刀', '布'] #定义一个列表,将选择添加在列表中
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['石头', '布']] #定一个用户赢的列表,列表中的元素仍然是一个列表
prompt = """(0) 石头 #定义一个变量,将提示语写入这个变量中
(1) 剪刀
(2) 布
请选择 (0/1/2):"""
computer = random.choice(all_choices) #随机选一个值
ind = int(raw_input(prompt)) #因为输入的是字符串类型,所以将字符串转变为整型
player = all_choices[ind] #列表可以取下标,下标对应的列表中的值
print "Your choice: %s, Computer's choice: %s"%(player, computer) #提示信息,友好界面:相当于人出石头、计算机出布
if player == computer: #如果两个变量值相等
print '\033[32;1m 平局 \ 033[0m' #输出平局
elif [player,computer] in win_list: #如果列表在 win_list 中
print '\033[31;1m 你赢了 \ 033[0m' #输入你赢了
else:
print '\033[31;1m 你输了 \ 033[0m' #输出你输了
pwin = 0 #人赢的次数
cwin = 0 #电脑赢得次数
import random
all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['石头', '布']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择 (0/1/2):"""
while pwin < 2 and cwin < 2: #人和电脑赢的次数都不够两次时就的循环下面语句
computer = random.choice(all_choices)
ind = int(raw_input(prompt))
player = all_choices[ind]
print "Your choice: %s, Computer's choice: %s"%(player, computer)
if player == computer:
print '\033[32;1m 平局 \ 033[0m'
elif [player,computer] in win_list:
pwin += 1
print '\033[31;1m 你赢了 \ 033[0m'
else:
cwin += 1
print '\033[31;1m 你输了 \ 033[0m'
Break 语句的写法:
import random
num = random.randint(1, 10)
while True:
answer = int(raw_input(" 请猜一个数: "))
if answer > num:
print "猜大了"
elif answer < num:
print "猜小了"
else:
print "猜对了"
break
print num
For 语句的写法:
import random
num = random.randint(1, 10)
for i in range(3):
answer = int(raw_input(" 请猜一个数: "))
if answer > num:
print "猜大了"
elif answer < num:
print "猜小了"
else:
print "猜对了"
break
else:
print num
纯粹 if 语句结构
#coding: utf8
import random
computer = random.choice(['石头', '剪刀', '布'])
player = raw_input('请出拳头 (石头 / 剪刀 / 布): ')
print "your choice: %s, Computer's choice: %s"%(player, computer)
if player == '石头':
if computer == '石头':
print '平局'
elif computer == '剪刀':
print '你赢了'
else:
print '你输了'
elif player == '剪刀':
if computer == '石头':
print '你输了'
elif computer == '剪刀':
print '平局'
else:
print '你赢了'
else:
if computer == '石头':
print '你赢了'
elif computer == '剪刀':
print '你输了'
else:
print '平局'
#coding: utf8
num = int(raw_input(" 请输入您的成绩: "))
if num >= 90:
print "优秀"
elif num >= 80:
print "好"
elif num >= 70:
print "良"
elif num >= 60:
print "及格"
else:
print "你要努力了"
#coding: utf8
num = int(raw_input(" 分数: "))
if 60 <= num < 70:
print '及格'
elif 70 <= num < 80:
print '良'
elif 80 <= num < 90:
print '好'
elif num >= 90:
print '优秀'
else:
print '你要努力了'
sum = 0
i = 1
while i <= 100:
sum += i
i += 1
print sum
while i <= 100:
sum = sum + i
i = i + 2
print sum
result = 0
counter = 0
while counter < 100:
counter += 1
if counter % 2 == 1:
continue
result += counter
print result
sum = 0
i = 2
while i <= 100:
sum += i
i += 2
print sum
生成八随机密码: for 循环
import random
all_chs = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
result = ''
for i in range(8):
ch = random.choice(all_chs)
result += ch
print result
调用函数生成 8 位随机密码
import random
import string
all_chs = string.letters + string.digits #表示大小写字母和数字的调用
def gen_pass(n=8):
result = ''
for i in range(n):
ch = random.choice(all_chs)
result += ch
return result
if __name__ == '__main__':
print gen_pass()
print gen_pass(10)
将 / bin/ls 拷贝到 / root / 目录下, 简单写法
f1 = open('/bin/ls')
f2 = open('/root/ls', 'w')
data = f1.read()
f2.write(data)
f1.close()
f2.close()
while 语句的写法:
src_fname = '/bin/ls' #定义源文件 src_fname
dst_fname = '/root/ls' #定义目标文件 dst_fname
src_fobj = open(src_fname)
dst_fobj = open(dst_fname,'w')
while True:
data = src_fobj.read(4096)
if data == '':
break
dst_fobj.write(data)
src_fobj.close()
dst_fobj.close()
定义函数的调用的写法:
import sys
def copy(src_fname, dst_fname):
src_fobj = open(src_fname)
dst_fobj = open(dst_fname,'w')
while True:
data = src_fobj.read(4096)
if data == '':
break
dst_fobj.write(data)
src_fobj.close()
dst_fobj.close()
copy(sys.argv[1], sys.argv[2])
原文:http://blog.51cto.com/13513556/2054077
来源: http://www.bubuko.com/infodetail-2439238.html