Python OOPs


Examples


Others


Python For Loop


The for loop in Python is used to iterate over a sequence (list, tuple, string,set, dict) or other iterable objects.

Syntax
for x in seq:
    body of loop
for i in range(start,stop,step):
    body of loop

range() function:

We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers).

Write a Python program that accepts a string and calculate the number of digits and letters.

Example 1
num = int(input("Enter a number: "))
f = 1
for i in range(1,num+1):
    f = f*i
print("The factorial of "+str(num)+" is "+str(f))
Output
Enter a number: 5
The factorial of 5 is 120

Write a Python program that accepts a string and calculate the number of digits and letters.

Example 2
s = input("Enter a string : ")
d=l=0
for c in s:
    if c.isdigit():
        d=d+1
    elif c.isalpha():
        l=l+1
print("No of Letters "+str(l))
print("No of Digits "+str(d))
Output
Enter a string : sample123
No of Letters 6
No of Digits 3