首页 > 后端开发 > Python教程 > python session day-t payilagam for循环和if条件

python session day-t payilagam for循环和if条件

Susan Sarandon
发布: 2024-12-11 03:43:14
原创
837 人浏览过

python session day-t payilagam for loop and if condition

for 循环:
for 循环用于迭代序列(即列表、元组、字典、集合或字符串)。

语法:
对于序列中的变量

示例:

txt = '1234'

for num in txt:
    print(num,end=' ')

output:
1 2 3 4
登录后复制

如果条件:
If 语句的工作原理是检查表达式以查看是否满足条件,并根据获得的输出返回一个值。

syntax:
if condition:
      if condition is True
else:
      if condition is False

登录后复制

示例

x=20
if x>=20:
   print(x is within 20)
else :
   print(x is above the limit)

output:
x is within 20
登录后复制
txt = '12a4'

for num in txt:
    if num>='0' and num<='9':
        print(num,end=' ')
    else:
        print('Not Decimal',end=' ')

Output:

1 2 Not Decimal 4
above code is the example of combines if and for.


登录后复制

name = input("请输入您的名字:")
打印(姓名)
名称中的字母:
print(字母表, end='*')

输出:
R*A*J*A

代码检查每个索引并打印输出。

name1 = input("Enter the first name: ")
name2 = input("Enter the second name: ")
name3 = input("Enter the third name: ")
name4 = input("Enter the fourth name: ")
name = [name1, name2, name3, name4]


for letter in name:
    if letter[0]=='G':
        print(letter)
    else:
        continue
for alphabet in name:
    if alphabet[-1]=='a':
        print(alphabet)
    else:
        continue
for alpha in name:
    for i in alpha:
        if i==' ':
            print(alpha)
        else:
            continue
for character in name:
    if len(character)>9:
        print(character)
    else:
        continue

output:

Enter the first name: Guhanraja
Enter the second name: Lakshmi Pritha
Enter the third name: Guru Prasanna
Enter the fourth name: Varatharajan
Guhanraja
Guru Prasanna
Guhanraja
Lakshmi Pritha
Guru Prasanna
Lakshmi Pritha
Guru Prasanna
Lakshmi Pritha
Guru Prasanna
Varatharajan



登录后复制

以上是python session day-t payilagam for循环和if条件的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板