
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 loop and if condition的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号