一、break_continue_exit
break:跳出整个循环 不会再执行循环后面的内容
continue:跳出本次循环,continue后面的代码不会执行
exit():结束程序的运行
与shell内基本一致。
例:
break:
for i in range(10):
if i == 7:
break break跳出整个循环,不执行循环后的内容
print(i) 到7的时候print(i)的命令被跳过,
print('hello')
continue:
for i in range(10):
if i == 7:
#break
continue 跳出本次循环
print(i) i = 7的时候不做print(i),继续做 后续 i =7 以后的
print('hello')
exit:
for i in range(10):
if i == 7:
#break
#continue
exit() i=7的时候exit退出当前脚本
print(i)
print('hello')
二、命令行提示符
运行脚本之后获得一个类似的命令行,并且可以运行ls、pwd等命令,只有输入exit才能退出
import os
for i in range(1000): 1000次循环
cmd = input('[test@foundation0 bin]$')
if cmd:
if cmd == 'exit':
print('logout')
break
else:
print('run %s' %(cmd)) 运行shell命令
os.system(cmd)
else:
continue
三、循环
while 循环
while 条件满足:
语句1...
else:
循环完成后 要执行的语句
例:用户登陆程序
1.输入用户名和密码
2.判断用户名和密码是否正确(‘name==root’,'passwd=‘westos’)
3.为了防止暴力破解,登陆次数仅有三次,如果超过三次机会,报错。
i = 0
while i < 3:
name = input('用户名:')
passwd = input('密码:')
if name == 'root' and passwd == 'westos':
print('登陆成功') 跳出整个循环,不会再执行后面的内容
break
else:
print('登陆失败')
print('您还剩余%d次机会' %(2-i))
i += 1 每循环一次i+1
else:
print('登陆次数超过三次,请等待100s后再次尝试登陆')
死循环
即永真。
例:while 1>0等永远成立的条件下。
i = 0
while i >=0: 将while条件改为永真,无限循环,就不会存在登陆次数限制
name = input('用户名:')
passwd = input('密码:')
if name == 'root' and passwd == 'westos':
print('登陆成功') 跳出整个循环,不会再执行后面的内容
break
else:
print('登陆失败')
print('您还剩余%d次机会' %(2-i))
i += 1
else:
print('登陆次数超过三次,请等待100s后再次尝试登陆')
四、字符串
a = 'westos' 定义字符串需要 “ 或者 ‘ 引起来
b = "what's" 定义带有 ‘ 等符号的字符串需要用 “
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
...
""" 定义大量的字符串上下用 “”“ 标示
字符串的特性
s = 'hello' # 索引:0 1 2 3 4 索引从0开始
print(s[0]) 拿出第一个字符
print(s[1]) 拿出第二个字符
拿出最后 一个字符
print(s[4])
print(s[-1])
s[start:stop:step] 从satrt开始到end -1结束
步长为step
print(s[0:3]) 从第一个字符到第三个字符
print(s[0:4:2]) 从第一个字符到第五个字符,步长为2
print(s[:]) 显示所有字符
print(s[:3]) 显示前三字符
字符串的反转
print(s[::-1])
除了第一个字符之外的其他全部字符
print(s[1:])
重复
print(s * n) 重复显示n遍
连接
print('hello ' + 'python')
成员操作符
print('he' in s) 判断s是否含有 he
print('qaq' in s) 判断s 是否含有 qaq
print('he' not in s) 判断s 是否不含有 he
for循环遍历
for i in s:
print(i)
字符串的常用方法
判断字符串的组成
'hello'.istitle() 判断字符串是否是标题
'hello'.isupper()
'Hello'.isupper() 判断字符串是否全部是大写
'HELLO'.isupper()
'hello'.islower()
'Hello'.islower() 判断字符串是否是全部为小写
'Hello'.islower()
字符串大小写的转化
'Hello'.lower() 把字符串转换成小写
a = 'Hello'.lower()
a = 'Hello'.upper()
a = 'Hello'.title()
'qaq321'.isdigit() 判断字符串是否纯数字组合
'321qaq'.isalpha() 判断字符串是否纯字母组合
'q1a2q3'.isalnum() 判断字符串是否纯数字字母组合
字符串的常用方法(2)
if语句中,以 什么字符开头或结尾
filename = 'hello.log'
if filename.endswith('.log'): 以.log结尾的
print(filename)
else:
print('error.file')
filename = 'hello.loggg'
if filename.startswith('hello'): 以hello开头的
print(filename)
else:
print('error.file')
去除不规范字符的左右空格
s = ' hello'
s.lstrip() 去除左空格
例:
s = ' hello '
s.lstrip() 去除左空格
s.rstrip() 去除右空格
s.strip() 去除左右空格
s = 'helloh'
s.strip('h') 去除字符串左右的 h
s.lstrip('h') 去除字符串左边的 h
s.rstrip('h') 去除子付串右边的 h
字符串的对齐
print('你好'.center(30)) 总共三十格,要显示的内容居中
print('你好'.center(30,'@')) 总共三十格,显示的内容居中,左右为‘@‘
print('你好'.center(30,'&')) 总共三十格,要显示的内容居中,左右为‘&’
print('你好'.ljust(30,'#')) 总共三十格,要显示的内容居左,右边以‘#’补齐
print('你好'.rjust(30,'#')) 总共三十格,要显示的内容居右,左边以‘#’补齐
字符串的索引和替换
s = 'hello world hello'
print(s.find('hello')) 索引出来的是最近的一个hello 前面有0个字符
print(s.find('world')) 索引出来的world前面有6个字符(hello和一个空格)
print(s.rfind('hello')) 索引右边的hello前面有12个字符
print(s.replace('hello','redhat')) 替换字符串中的hello为redhat
字符串的统计
print('hello'.count('l')) 统计字符串‘hello’ 里面有几个‘l’
print('hello'.count('ll')) 统计字符串‘hello’ 里面有几个‘ll’
print(len('qweewq')) 统计字符串总共多少个字符
字符串的分离和连接
s = '172.25.254.3'
s1 = s.split('.') 定义s1是 以‘.’为分隔符并不显示‘.‘的字符串
print(s1)
print(s1[::-1]) 反向显示字符串 s1
date = '2019-9-1'
date1 = date.split('-') 以‘-’为分隔符分离字符串,并且不显示‘-‘
print(date1)
连接
print(''.join(date1)) 以空来连接字符串date1
print('/'.join(date1)) 以/ 来连接字符串date1
print('~'.join('hello')) 以~来连接字符hello
五、列表
数组:存储同一种数据类型的集合 scores = [34,56,90,100]
列表(打了激素的数组):可以存储任意数据类型 list = [1,3,5,7,‘hello’,‘world’]
print(scores)
print(list,type(list))
列表里面也可以嵌套列表
list2 = [1,1.2,'westos','redhat',list]
print(list2,type(list2)
列表的特性
service = ['http','ftp','ssh']
print(service)
索引
print(service[0]) 正向索引,即显示第一个
print(service[-1]) 反向索引,显示最后一个
切片
print(service[::-1]) 反向显示service列表
print(service[1:]) 除了第一个显示所有
print(service[:-1]) 除了最后一个显示所有
重复
print(service * n) 重复显示n次
连接
service1 = ['mysql','firewalld']
print(service + service1) 把列表service和列表service1连接到一起
成员操作符
print('firewalld' in service) 判断‘firewalld’ 是否存在于列表service
print('ftp' not in service1) 判断‘ftp’是否不存在于列表service1
列表元素
列表元素的增加
service = ['http','ftp','ssh']
print(service)
service.append('firewalld') 追加一个元素‘firewalld’到列表service内
print(service)
列表元素的拉伸(添加多个元素到列表内)
service.extend(['mysql','nfs'])
print(service)
指定插入元素的所在位置
service.insert(1,'https') 将‘https’插入到service列表的第一个元素后面
print(service)
列表元素的删除
在python3中效果更直观
service = ['http','ftp','ssh']
service.pop() 默认删掉最后一个元素
service.pop(0) 默认删掉第一个元素
service.remove(‘ftp‘) 删掉元素ftp
del service[1] 从内存中删除一个元素