is divmod a python builtin function? [duplicate] - python

This question already has an answer here:
Python3: What is the difference between keywords and builtins?
(1 answer)
Closed 3 years ago.
divmod seems to be a builtin as divmod(5,1) gives the correct tuple output with no prior imports (at least in Python 3.7.4).
On the other hand
import keyword
keyword.kwlist
does not list divmod. Also keyword.iskeyword('divmod') returns False. Actually even keyword.iskeyword('int') and keyword.iskeyword('hex') return false. Why is this? What is a keyword and where can we find the complete list of reserved string?

divmod is not a keyword, neither is int or hex. These are objects. You can tell by the fact that they are used with parenthesis to call (). Things like in and for and import are keywords.

Related

Function requires a path. How do I make it into a raw path? [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!

Is there a function to evaluate a string into a integer (python 3.7) [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 2 years ago.
You may haven't understood the question correctly. So here is it in detailed way:
There is a string, let's say, x='200+350'
so now if I do int(x), it'll give me an error.
I want it to evaluate to 550 which is integer.
How may I do that?
try to use eval method
eval("200+350")
You can use eval()
x = '(2+3)*2'
print( eval(x) )
prints out 10

Example of Match.expand usage [duplicate]

This question already has answers here:
Use Python Match object in string with backreferences
(1 answer)
Regex in python: is it possible to get the match, replacement, and final string?
(2 answers)
Closed 3 years ago.
What is an example usage of Match.expand ? It doesn't give any examples in the python docs (which is the first I've heard of the method), and only states:
Match.expand(template)
Return the string obtained by doing backslash substitution on the template string template, as done by the sub() method.
How would this actually be used and how could it be useful?
you can find more explanations and examples here:
match.expand
In general:
it allows you to expand the match you have found and modify its' prefix

lower versus length syntax in python? [duplicate]

This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.

Replace string value in regular expression with integer value in python [duplicate]

This question already has answers here:
Safety of Python 'eval' For List Deserialization
(5 answers)
Closed 8 years ago.
In my python code, a user enters a mathematical expression. I want to replace the variables with integer values and calculate the result. I can user regular expression in python to replace the variables with integer values but I cant calculate the sum as the replaced string I get is of type string. I can do it in tcl. It has a built in expr command where I simply pass the string and it automatically converts it to mathematical expression and calculates the result. Is there a way to do the same in python?
Thank you
Yes there is eval.
for example:
a=3
b=4
s="(a*a+b*b)**.5"
eval(s)
But be warned it maybe an security risk.
You may better use SymPy
http://sympy.org/en/index.html

Categories