python2 for 、while语句的简单运用,字符串了解

##############for语句的循环跳出方法
exit():它代表的是整个程序结束。
break:它代表的for这个循环结束,而程序中的其它命令继续运行
continue:它代表的是for里面循环的仅满足条件的这一次循环跳过。但是不结束for的整个循环。

for i in range(0, 9):
     if i == 4:breakcontinue         exit()
     print(i)                                 ###continue后这条语句
 print('hello')                             ####break后这条在for之外的程序继续运行

##############怎么使用python中运行命令

import os                                 ##所用内部命令print(os.system('ls /home/kiosk/'))print(os.system('pwd'))for i in range(3):                             ##循环三次
    cmd = input('[kiosk@foundation71 ~]$ ')    ##输如
    if cmd:                                    ##cmd的判断
        if cmd == 'exit':                      ##如果输入是exit
            print('logout')
            break                                ##跳出for循环
        else:                                 ##如果是其他输入
            print('%s' %cmd)                       ##将输入的命令输出一次
            os.system(cmd)                           ##用内置程序运行

##########python中循环的应用,求两数的最大公约数和公倍数

num1 = input('num1:')                                 ##输入两个数
num2 = input('num2:')
num1 = float(num1)                                        ##先转换成浮点型
num2 = float(num2)
num1 = int(num1)                                        ##转为整数
num2 = int(num2)
min_num = min(num1, num2)                              ##两者取其中最小print(min_num)for i in range(1,min_num+1):                           ##这个范围不能取0
    if num1 % i == 0 and num2 % i == 0:                    
        a = i
b = num1*num2
c = b/a
print('最大公约数%d' %a)                             ##输出
print('最小公倍数%d' %c)

###########while循环与for循环除了格式不一样外其他一样

i = 0                                                       ##循环起始为0
while i < 3:                                               ##在小于3时循环
    name = input('name:')
    passwd = input('passwd:')
    if name == 'root' and passwd == 'passwd':
        print('please in')
        break
    else:
        print('please try again')
        a = 2-i                                              ##剩多少次
        i = i+1                                              ##定义i新的值
        print('you have %d times' %a)

###################while中的死循环

while True:
    print('!!!')while 2>1:
    print('!!!')

####################倒三角1
在这里插入图片描述

row = 1
while row <= 4:                                ###外层循环从1-4
    cal = 4                                    ####内层循环从4-1      
    while cal >= row:                           ##内层大于外层
        print('',end='')                      ##内层打印从4-1,end为取消环行 
        cal = cal - 1                         ##内层循环自减
    print('')                                  ##外层循环换行符
    row = row + 1                              ##外层自加

########################倒三角2

在这里插入图片描述

row = 1
while row <= 4:
    cal = 1                                ##内层循环从1-4
    while cal <= row:                       ##内层小于等于外层,
        print('',end='')
        cal = cal + 1                       ##自加1
    print('')
    row = row + 1                              ##外层自加1

#############################字符串
#####字符串的显示,与输入

 = 'westos'
print(a)
b = 'what's'
print(b)
"""                             ##########这种的字符串在程序中不起任何作用
nihaoadsafsaf
"""

###########这是输出的结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
westos
what'snihaoadsafsaf
....
Process finished with exit code 0

在这里插入图片描述
##字符串的特性

a = 'hello' ##索引:0,1,2,3,4,。从左到右
print(a[0])    ##取第0个            
print(a[1])     ##取第一个
print(a[4])     ##取最后一个
print(a[-1])    ##去做后一个

结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
h
e
o
o

特性:

a = 'hello' ##索引:0,1,2,3,4,。从左到右
print(a[0:5])                        
print(a[0:5:1])                            ###字符串的索引间隔1为
print(a[0:5:2])                             ##间隔2

结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
hello
hello                                    ##间隔1的结果
hlo                                        ##间隔2空格结果Process finished with exit code 0

特性:

print(a[:])                                      ##显示全部
print(a[0:3])                                    ##显示3个
print(a[:3])                                    ##显示3个
print(a[::-1])                                  ##反转
print(a[1:])                                     ##除了第一个其他的

####结果

hello                                    
hel
hel
olleh
ello

############左闭右开
其他特性:

a = 'hellO'  ##索引:0,1,2,3,4,。从左到右
print(a * 5)                    ##重复
print('*' + 'python')           ##连接
print('he' in a)                   ##判断是否在里面
print('aa' in a)
for i in a:                      ##循环便利
    print(i)

#####结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
hellOhellOhellOhellOhellO
*python
True                              ##he在a的地址空间中
False
h
e
l
l
O

在这里插入图片描述

在这里插入图片描述抓取字符

num = input('num:')                   ##输入
if num == num[::-1]:                  ##倒转后看是否相等
    print('true')                     ##相等输出true
else:
    print('fulse')

####结果

num:121
true

字符串的类型判断

print('Hello'.istitle())     ##看是否时标题,标题首字母大写
print('hello'.istitle())
print('HEELLO'.isupper())    ##看是否时大写
print('hello'.islower())     ##看是否是小写
print('hello'.upper())       ##转换为大写
print('hello'.title())      ##转换为标题
print('   hello')           ##中间加空格
print('   hello'.lstrip())     ##消除左空格
print('hello      '+'')
print('hello      '.rstrip()+'') ##消除右空格
print('   hello    '+'')
print('   hello    '.strip()+'')    ###消除左右空格

####结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
True                                       ##是标题
False                                     ##不是标题
True                                      ##是大写的
True                                       ##是小写的
HELLO                                     ##转换为大写
Hello                                        ##转换为标题
   hello                                    ##左面加空格
hello                                      ##.lstrip左边的空格消除
hello      *                                 ##右面加可空格
hello*                                        ##右面消除
   hello    *                                    ##中间加空格
hello*                                         ##中间空格消除

###########strip的其他用法

print('    hello')                             ##左加空格
print('\thello')                               ##左边加\t
print('   hello   '+'')                     ##左右有空格连接 
print('hello\n'+'')                          #走右没有空格+\n。连接号
print('helloh'.strip('h'))                     ##用strip消除h
print('helloh'.lstrip('h'))                       ##消除左h
print('helloh'.rstrip('h'))                         ##消除右面h

结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
    hello                                      ##左加空格
	hello                                     ##左边加\t 
   hello   *                                     ##左右有空格连接*   
hello                                            #走右没有空格+\n。连接*号
*
ello                                              ##用strip消除h 
elloh                                              ##消除左h
hello                                               ##消除右面h

在这里插入图片描述
变量名定义是否合法:
1.变量名可以由字母 数字 下划线组成
2.变量名只能以字母或者下划线开头
s = ‘321csv’ s[0] s[1:]
s = ‘asfasf%%’
‘’
exit

changenum = input('input:')
if changenum=='exit':                                 ##输入exit推出程序
    exit()
if changenum[0].isalpha() or changenum[0] == '':         ##判断开头
    for i in changenum[1:]:                                ##判断其他部分
        if not (i.isalpha() or i.isalnum() or i ==''):
            print('不合法')                                     ##不和法直接退出
            exit()
    else:
        print('合法')
else:
    print('不合法')                                             ##输入的开头不合法,输出   

在这里插入图片描述

##########################怎么让字符串自动对齐

print('hello'.center(30))
print('hello'.center(30,'#'))
print('hello'.ljust(30,'#'))
print('hello'.rjust(30,'#'))

结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
            hello             
############hello#############
hello#########################
#########################hello
############

找到字符的启示位置

a = 'hello world hello'             ##从左到右找第一位是0
print(a.find('world'))               ##给的是world在里面的第几位开始
print(a.find('hello'))
print(a.rfind('hello'))               ##从右边开始找
print(a.replace('hello','redhat'))    ##字符的替换

###结果

/home/kiosk/PycharmProjects/python/venv/bin/python /home/kiosk/PycharmProjects/python/python2.py
6
0
12
redhat world red     ##替换效果

###统计字符串的个数以及分离,倒转,链接

print('hello'.count('o'))      ##统计指定字符
print(len('gffg'))               ##统计所有字符
a = '172.25.254.100'           ##给字符串
a1 = a.split('.')               ##分离的分隔符是点
print(a1)                      ##输出分离后的
print(a1[::-1])              ##到转输出
print(''.join(a1))                #将a1合起来,不再分离
print('.'.join(a1))               ##分离中间用点连接
###结果
1                                  ##统计
4                                 ##统计
['172', '25', '254', '100']          ##分离
['100', '254', '25', '172']          ##倒转
17225254100                      ##合起来
172.25.254.100                       ##连接来

小米笔试编程题目题目描述:给定一个句子(只包含字母和空格), 将句子中的单词位置反转,
单词用空格分割, 单词之间只有一个空格,前>后没有空格。
比如: (1) “hello xiao mi”-> “mi xiao hello”
输入描述: 输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
输出描述: 对于每个测试示例,要求输出句子中单词反转后形成的句子

- 输入
    hello xiao mi
- 输出
    mi xiao hel
a = input('input:')
print(' '.join((a.split(' '))[::-1]))

##########################简单的加法

or i in range(0,100):
    import random
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    c = print('%s+%s=' % (num1, num2))
    b = num1 + num2
    student =input('num:')
    b = str(b)
    if b == student:
        student = str(student)
        print('%s是正确的' % student)
        zhengque = i + 1
    elif student=='exit':
        break
    else:
        print('%s是错误的,正确的是%s' % (student, b))
        print('正确个数%d' %i)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值