syntax

Is there a difference between `import a as b` and `import a ; b = a`?

Is there a difference between `import a as b` and `import a ; b = a`? Question: It’s common for me to write in REPL, for example: from datetime import datetime as dt Is there a difference between this sentence and from datetime import datetime dt = datetime ? For now I can list two: …

Total answers: 1

Why does Python have some functions that are syntactically arranged as enclosed?

Why does Python have some functions that are syntactically arranged as enclosed? Question: I’m taking an Introduction to Python course, and I’ve just learned the upper() function. However, I was really tripped up when I saw that the correct usage of upper() was not upper(string), but rather, string.upper(). Is there some particular logic or reasoning …

Total answers: 2

is redudant to use `__all__` and `import specific_module`?

is redudant to use `__all__` and `import specific_module`? Question: Background Reading the answers for this question, and looking at the Pandas and Django repository. If __all__ is defined then when you import * from your package, only the names defined in __all__ are imported in the current namespace. I immediately draw the conclusion that if …

Total answers: 1

why does variable instantation contain syntax error?

why does variable instantation contain syntax error? Question: I’m learning python. It gives syntax error in this script. I’m unable to figure out. conn = psycopg2.connect( host = str(Ip) user = "test" password="test1234") the error: user^= "test" ^ SyntaxError: invalid syntax There is something wrong with the user variable instantiation according to the compiler.. I …

Total answers: 1

how do I format a nested if-else statement in python to make it work?

how do I format a nested if-else statement in python to make it work? Question: I am taking a intro to scripting class and have a prompt to make a program that will accept an input month and then an integer day and tell what season it falls in, or to write ‘Invalid’ if the …

Total answers: 2

How can I diagnose common errors in JSON data?

How can I diagnose common errors in JSON data? Question: I have to deal with putative JSON from a lot of different sources, and a lot of the time it seems that there is a problem with the data itself. I suspect that it sometimes isn’t intended to be JSON at all; but a lot …

Total answers: 1

Python List Question: Which part of code is more logical?

Python List Question: Which part of code is more logical? Question: list_of_names = [‘Vladimir’, ‘Victor’, ‘Alexander’, ‘Darius’, ‘Dimitriy’] #1 for list in list_of_names: print(list) # 2 for list in range(len(list_of_names)): print(list_of_names[list]) Which part(#1 or #2) is better to announce value? Which will be correct? Asked By: Lazy_Kitty || Source Answers: You are clearly using less …

Total answers: 2

Python class __call__ method and dot notation

Python class __call__ method and dot notation Question: My Goal is to use dot notation to select strings from dictionarys using the SimpleNamespace modeule while having the ability to change which dictionary to use. To do this i have tried modifying the class __call__ method to change the output based on a previously set variable. …

Total answers: 1

Why is print(x += 1) invalid syntax?

Why is print(x += 1) invalid syntax? Question: This works just fine x = 0 while True: x += 1 print(x) while this x = 0 while True: print(x += 1) doesn’t I want a program that counts to infinity or at least until max digits Asked By: cryptoboomer || Source Answers: Because the argument …

Total answers: 2