Python Import Syntax Rules [duplicate] - python

This question already has answers here:
Python module with a dash, or hyphen (-) in its name
(2 answers)
What is the naming convention in Python for variable and function?
(15 answers)
Closed 11 months ago.
I've been doing some searching but I couldn't find any consensus on the syntax allowed for imports.
I threw an syntax error for this import and was wondering if there was documentation on allowable syntax.
import some-module
# Do stuff.

You can't use hyphen('-') symbol in any names in Python as this is regarded as substraction operator. Use underscore ('_') instead.

Related

function code from the book "Natural Language Processing with Transformers" [duplicate]

This question already has answers here:
Why not python implicit line continuation on period?
(6 answers)
Closed 10 months ago.
I have never seen this repeated dot syntax, and I can't find any pointers to it anywhere.
Is it application of unsqueeze followed by expand followed by float() ?
input_mask_expanded = (attention_mask
.unsqueeze(-1)
.expand(token_embeddings.size())
.float())
It's equivalent to
input_mask_expanded = (attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float())
Just in a more readable form

What is the Python ` symbol [duplicate]

This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.

What is the difference between the f" and the format() method in python [duplicate]

This question already has answers here:
f-strings vs str.format()
(5 answers)
Closed 2 years ago.
So i've tried the formats string methods in python but what are the differences between them? Which method is best for me?
example1:
name = 'Dash'
print(f'Hello {name}!')
example2:
name = 'Dash'
print('Hello {}!'.format(name))
Effectively both do the same thing.
The f you mention is an f-string which is available from python 3.6
Print f is just a newer and easier way of inserting a variable into a string. I think it came in in python 3.6 . Both do really the same thing

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

F string prefix in python giving a syntax error [duplicate]

This question already has answers here:
f-strings giving SyntaxError?
(7 answers)
Closed 5 years ago.
I have a variable called method, it's value is POST but when I try to run print(f"{method} method is used.") it keeps giving a syntax error at the last double quote and I can't find the reason on why it does this. I am using python 3.5.2.
F-strings were a new feature introduced in Python 3.6, so of course they're not going to work in 3.5.2.

Categories

Resources