This question already has answers here:
Python - is there a "don't care" symbol for tuple assignments?
(9 answers)
Is it possible to unpack a tuple in Python without creating unwanted variables?
(4 answers)
Closed 3 years ago.
In Python it is good practice to use a single underscore for intentionally unused variables:
first_word, _ = 'only only is important'.split(' ', 1)
On the other hand, gettext uses the underscore as a short alias for its translation function, leading to clashes if the underscore is used for a variable before:
print(_('gettext will try to translate this string.'))
Question: What is the convention for the name of throwaway variables if gettext is used in the same namespace?
Related
This question already has answers here:
Is it ok to use dashes in Python files when trying to import them?
(7 answers)
Closed 2 years ago.
This question is based on the assumption that you don't want to rename your modules and want to have # in their name.So how do you import these modules in Python? I tried using single/double inverted commas but still get a SyntaxError.
from "#Calc" import *
from '#Calc' import *
Please advise.
You could try importlib or calc = __import__('#Calc')
But the PEP 8 Style-Guide recommends not to use Upper-Case Letters or Symbols.
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
This question already has answers here:
How to set a variable name with space in python [closed]
(2 answers)
Valid characters in a python class name
(4 answers)
Closed 2 years ago.
Can a space be given in variable naming. I tried a python program but it's showing an error I have attached the screenshot of the same.
enter image description here
You can't give a space. It's violating the naming convention. What you can do, add underscore (_) between words. Here is the correction:
Gal_Gadot = "Gal Gadot"
print(Gal_Gadot);
Hope this makes sense.
This question already has answers here:
How do I create variable variables?
(17 answers)
Using a string variable as a variable name [duplicate]
(3 answers)
Closed 5 years ago.
How can I name a list with the string value of an int?
Example pseudo-code:
int i = 4;
list + i = [] // i want the name of the list to be thus 'list4'
First of all, you don't declare type of variables in python, so i = 4 works just fine, also word "list" is reserved in python, so try to not use words which may collide with python built-ins. If you realy need to do that (which i don't recommend), you can achieve it by usin eval()
This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
What does a semicolon do?
(5 answers)
Closed 8 years ago.
I sometimes see this ; symbol on tutorials and such, what does it indicate?
Semi-colons are used in Python to separate statements in the same line.
print 1; print 2
Common use example:
import pdb; pdb.set_trace()
The semicolon can be used as a statement separator in Python, called stmt_list in the language reference, but its use is generally discouraged (PEP 8, compound statements). We prefer one statement per line.
This question already has answers here:
What is the naming convention in Python for variable and function?
(15 answers)
Closed 8 years ago.
So which is better and why?
def my_function():
or
def myFunction():
for everything related to Python's style guide: i'd recommend you read PEP8.
To answer your question:
Function names should be lowercase, with words separated by
underscores as necessary to improve readability.
PEP 8 advises the first form for readability. You can find it here.
Function names should be lowercase, with words separated by
underscores as necessary to improve readability.
Function names should be lowercase, with words separated by
underscores as necessary to improve readability. mixedCase is allowed
only in contexts where that's already the prevailing style
Check out its already been answered, click here