ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python2

2022-06-11 16:02:33  阅读:193  来源: 互联网

标签:item str1 字符串 score print 循环 Python2


Python2

循环语句

for循环

for循环的代码为:for 变量(自定义) in 想要循环的变量。

 str1="世界你好!"
 ​
 for item in str1:
    print(item)

 

 

获取被循环对象的内容和其对应的索引信息:enumerate

 str1="世界你好!"
 ​
 ​
 '''输出字符串的同时输出索引信息'''
 for index,item in enumerate(str1):
    print(index,item)

 

 

获取一定的范围range()

 '''获取一定的范围:range()'''
 for item in range(3):#从0开始取三个数字
    print(item)
 ​
 for item in range(2,5):#左闭右开取2-5的数字
    print(item)

 

 

while循环

while循环的关键字是:while True,他是一个死循环。跳出循环的关键字是:break。继续执行循环的关键字是:continue。也可以在for循环中使用,但是需要结合逻辑判断使用。

死循环

 str1="世界你好!"
 ​
 '''
 while:死循环
 break:跳出循环
 continue:继续
 '''
 while True:
    for item in str1:
      print(item)

 

 

停止循环break继续循环continue

逻辑控制

在计算机中,针对不同情况,使用逻辑判断的方式,逻辑判断使用的关键字是:if——elif(else if)——else

 while True:
    score=int(input("请输入成绩\n"))
    if score==100:
      print('满分')
      continue
    elif score >= 85 and score<100:
      print("优秀")
    elif score >=75 and score<85:
      print("良好")
    elif score >=60 and score<75:
      print("及格")
    else:
      print("不及格")
      break

 

 

debug调试

 

 

 

 

字符串

dir

 '''针对字符串的各种方法操作,是专门有一个str的类提供了很多针对字符串的操作'''
 str1="hello"
 #查看字符串有哪些可以使用
 print(dir(str1))

 

 

upper:使字符串变为大写

 str1="hello"
 ​
 #将小写改为大写
 print(str1.upper())

 

 

 

lower:使字符串大写变小写

 str2="HELLO"
 print(str2.lower())

 

 

len:查看字符串长度

str1="hello"

# 获取对象长度
print(len(str1))

 

 

判断字符串以什么开头:startswith

str2="HELLO"

print(str2.startswith("H")) #字符串以什么开头

 

 

判断字符串以什么结束:endswith

str2="HELLO"

print(str2.endswith("O")) #字符串以什么结束

 

 

判断字符串类型是否为数字:isdigit()

str3="3"
#判断字符串类型是否为数字
print(str3.isdigit())
print(int(str3)) #将字符串类型转换为整型

 

 

replace:字符串内容替换

str4="hello world"
#字符串替换:replace
print(str4.replace("world","python"))

 

 

count:查看字符串某个内容的个数

str4="hello world"

print(str4.count('o')) #查看字符串个数

 

 

index:查看字符串某个内容的索引

str4="hello world"

#查看字符串里面对象的索引-----线性查找方式(找到第一个就输出)
print(str4.index('e'))

 

 

 

split:字符串拆分

str5="Java,Python,Go"
# chaifen=str5.split(",")
print(str5.split(','))

 

 

join:字符串合并

#合并:join----把列表的数据类型转为str
list=['Java','Go',"Python",'1551325']
str6='_'.join(list) #根剧_来连接
print(str6,type(str6))

 

 

标签:item,str1,字符串,score,print,循环,Python2
来源: https://www.cnblogs.com/XJY1/p/16366098.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有