Hello everyone,
I’m new here ant with python in general and I would be happy to receive support and guidance from you all.
I wrote a very easy function that should return a list of objects found in both the provided lists.
However, the code has no error but it does not return the expected outcome: the new list.
I cannot figure out the problem.
def com_country(a, b):
country = []
for x in a:
if x in b:
country.append(x)
return country
Andrea = ["Spain", "UK", "USA", "Israel", "Italy"]
Thea = ["South Africa", "USA", "Morocco", "Italy", "Israel"]
com_country(Andrea, Thea)
Are you just not storing the output from the function call in a new list, nor printing it?
com.py
def com_country(a, b):
country = []
for x in a:
if x in b:
country.append(x)
return country
Andrea = ["Spain", "UK", "USA", "Israel", "Italy"]
Thea = ["South Africa", "USA", "Morocco", "Italy", "Israel"]
print(com_country(Andrea, Thea))
I’m not sure if I should open another thread, or continue with this since it is related to the topic.
I have this classic password_creator function and there is something not clear about the last step.
import random
# I define the function name and an EMPTY object that will represent my password at the end: pw = str()
def password(length):
pw = str()
characters = "abcdefghijklmnopqrstuvwxyz0123456789!@#"
for i in range(length):
# this will call for looping into the "characters" object as many time as established by "length"
# range has 3 values; (start, stop, step) if not defined, start and step are = 1
pw = random.choice(characters)
print (pw)
so, if I finish this way and I call the function by typing for example
password(7)
I will only get a string of 2 characters however, if I replace the last part with this one
import random
# I define the function name and an EMPTY object that will represent my password at the end: pw = str()
def password(length):
pw = str()
characters = "abcdefghijklmnopqrstuvwxyz0123456789!@#"
for i in range(length):
# this will call for looping into the "characters" object as many time as established by "length"
# range has 3 values; (start, stop, step) if not defined, start and step are = 1
pw = pw + random.choice(characters)
print (pw)
password(7)
then I actually get a seven character password.
My question is: why I get only 2 character in the first example?
You mean I’m replacing the letters “p” and “w” with the two randomly chosen ones? So why, in the second example, it doesn’t add the seven characters to the two of “pw”?
I’m wondering what is the rationale behind it. Sorry if I bother but I’m trying to understand the “grammar/rules” of this language, for a better use of it.
Instead of hardcoding out all the individual characters you want, you can instead use the constants in the standard library string module (make sure to import it first, of course). For example, to duplicate exactly the above:
For a stronger password (and to meet many requirements), you probably want to use uppercase letters as well; using the constants really shines in this case since instead of typing out a ton more letters you can just swap out string.ascii_lowercase for string.ascii_letters to get both lower and uppercase letters. Likewise, if you want to use all standard ASCII punctuation characters, you can replace the hardcoded symbols with string.punctuation.
If you’re writing a function to generate a password pw, its a good idea to have the function return pw and then do the print() at the top level (global scope). That way, the caller of the function gets to decide what to do with the generated password, and you (or others) can use the function to generate a password in contexts other than just printing it to the user on a command line terminal.
Instead of going through the extra verbosity and inefficiency of building up your string character by character, a more idomatic way is to use "".join([...]) on a list of single characters you generate one by one, inside a list comprehension or generator expression—which is a lot like a one-line for loop for building a list or sequence. I.e.
def password(length):
characters = string.ascii_lowercase + string.digits + "!@#"
return "".join(random.choice(characters) for _ in range(length))
The answers given are correct. However, please don’t use that password generator in production. The random module is only pseudo random: its output is predictable based on the starting data it used. The secrets module is what you want for generating real-life passwords. Use it like this:
import secrets
# Using hex digits
pw = secrets.token_hex(32)
# 'b490fe94fcd1a15b0c29ad157753a8249588baf7211a91aa51bd1e269674c3b9'
# Using URL-safe characters
pw = secrets.token_urlsafe(32)
# 'R9TFCTs-ILHeGT93j2mkvxQ2chHR7naCZBl8im7qj9w'