dimanche 15 novembre 2020

How to write an if condition such that python should execute if statement if the element in a listcan be converted to type int

How can i write the code for an if condition where i want to see if i can convert the type of an element in a list to int. If its possible, then if condition should be executed.Otherwise, it should go to else statement

For eg:

lst=['@','#','^','%','20']

''' Now i am going to iterate through the list and check if i can convert the elements  to type int. If it can be converted then i know there is a number in the list and i do some operations on the number'''

for ele in lst:
    if(type(int(ele)=='int'):
        #do some operation on the number eg:20/2 and print the value
    else:
        continue

Please give me an idea as to how to write the if condition.

Aucun commentaire:

Enregistrer un commentaire