PHP前端开发

python session day-t payilagam for loop and if condition

百变鹏仔 1个月前 (01-16) #Python
文章标签 day

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

语法:
对于序列中的变量

示例:

txt = '1234'for num in txt:    print(num,end=' ')output:1 2 3 4

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

syntax:if condition:      if condition is trueelse:      if condition is false

示例

x=20if 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 4above 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:        continuefor alphabet in name:    if alphabet[-1]=='a':        print(alphabet)    else:        continuefor alpha in name:    for i in alpha:        if i==' ':            print(alpha)        else:            continuefor character in name:    if len(character)>9:        print(character)    else:        continueoutput:Enter the first name: GuhanrajaEnter the second name: Lakshmi PrithaEnter the third name: Guru PrasannaEnter the fourth name: VaratharajanGuhanrajaGuru PrasannaGuhanrajaLakshmi PrithaGuru PrasannaLakshmi PrithaGuru PrasannaLakshmi PrithaGuru PrasannaVaratharajan