# Questions & Answers

How can I easily iterate over this loop on Python?

I created a small program to get a password input from the user and then evaluate the password according to some metrics. Them being upper case, lower case, numbers and special characters. I then calculate the strength of the password by calculating how many of these criteria are satisfied. How can I do the iteration of the loop where I find how many upper case, lower case, numbers and special characters are there?

I already have a working code but I think the process could be done more efficiently. What do you think?

``````import string

upper_case = any([1 if c in string.ascii_uppercase else 0 for c in password])
lower_case = any([1 if c in string.ascii_lowercase else 0 for c in password])
special = any([1 if c in string.punctuation else 0 for c in password])
digits = any([1 if c in string.digits else 0 for c in password])

characters = [upper_case, lower_case, special, digits]

score = 0
``````
2023-01-21 00:30:06
I'm using Python 3.11
2023-01-21 00:30:06
Which loop are you referring to? I don't understand what you mean by "how can I iterate over this loop" because if you have a loop you are already iterating by definition.
2023-01-21 00:30:06
Sorry. Loop wasn't the best description. I was referring to the part where I'm defining and counting numbers, special characters, etc.
2023-01-21 00:30:06
You can make it clear by giving some sample in-/outputs?
2023-01-21 00:30:06
You could put `string.ascii_uppercase, string.ascii_lowercase`, etc. into a list and then iterate over that list, though I think the idea from the answer below of just using predicates like `.isupper()` is preferable.
2023-01-21 00:30:06
@DanielHao for example: +1 if there are numbers in the password, +1 if there is a special character, +1 if the password is above a certain length.
2023-01-21 00:30:06
Seems that you already found/accepted the answer ... Not sure that's what you've expressed... anyway.

I think you're complicating things a bit too much:

``````import string