For loop
The for Loops in Python are a special type of loop statement that is used for sequential traversal. Python for loop is used for iterating over an iterable like a String, Tuple, List, Set, or Dictionary.
example :
name = 'varatharajan'
for alphabet in name:
print(alphabet, end=' ')
result
v a r a t h a r a j a n
if,else
The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not.
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
example 1 :
a = 33
b = 200
if b > a:
print("b is greater than a")
result
b is greater than a
example 2 :
txt = '12a4'
for num in txt:
if num>='0' and num<='9':
print(num,end=' ')
else:
print('Not Decimal',end=' ')
result
1 2 Not Decimal 4
Task :
name = input("Enter Name: ")
Lakshmi pritha
Guru prasanna
Guhanraja
Varatharajan
1: Names starting with letter 'G'
2: Names endings with 'a'
3: Names having space in between
4: Names having more than 9 letters
input
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]
# Check if names start with 'G'
for letter in name:
if letter[0]=='G':
print(letter)
# Check if names end with 'a'
for alphabet in name:
if alphabet[-1]=='a':
print(alphabet)
# Check if names contain a space
for alpha in name:
for space in alpha:
if space==' ':
print(alpha)
# Check if names are longer than 9 characters
for character in name:
if len(character)>9:
print(character)
Output
Enter the first name: Varatharajan
Enter the second name: Guru prasanna
Enter the third name: Lakshmi pritha
Enter the fourth name: Guhanraja
Guru prasanna
Guhanraja
Guru prasanna
Lakshmi pritha
Guhanraja
Guru prasanna
Lakshmi pritha
Varatharajan
Guru prasanna
Lakshmi pritha
Task
input
name = input('enter your name :')
print(name)
for alphabet in name:
print(alphabet,end=' ')
print()
for alphabet in name:
print(alphabet,end='*')
print()
for alphabet in name:
print(alphabet)
Output
enter your name :varatha
varatha
v a r a t h a
v*a*r*a*t*h*a*
v
a
r
a
t
h
a
Top comments (0)