Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have variables x and y defined, how do I create a stub if statement that evaluates whether x is less than y and does not do anything, even if the condition is true?
This has a condition for if x is less than y and does nothing
if x < y:
pass
pass is used as a placeholder and nothing happens when pass is executed. If you want to learn more about the pass statement, you can check out https://www.programiz.com/python-programming/pass-statement.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
When I want to check if an object x is not None, I can either use
if x:
do_something()
or
if x is not None:
do_something()
The first variant feels more pythonic but I think the second variant is easier to read. What is the consensus here?
The two have a different meaning, the first one won't be triggered by most falsy objects ('', False, 0, etc.), the second will. So the logic is different. If you really care about not being None, use the second one.
x = False
if x:
print('one')
if x is not None:
print('two')
output:
two
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I saw an example online that looks like this:
def DecimalToBinary(num):
if num >= 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')
print(DecimalToBinary(12))
I'll be happy if someone explain it to me.
This is called recursion. There is no problem as long as there is a break condition, in other words the calls to itself stop at some point. Otherwise you have an infinite loop.
The function will run normally as if you called it outside of it
It is called recursion. A type of concept of function calling itself. If it has no proper if statement that terminates the function itself, loop will not end or it will error. In your case it terminated.
01100None
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How do I get the returned result from the below function?
def various_return_types(n):
if(n==1):
return True
else:
return False
various_return_types(1)
The function call does return a value but since you are not assigning it to a variable, you can't use it.
If you use
ret = various_return_types(1)
then, ret will contain the boolean value you can check.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In the following Python code, what does the (x) mean?
Dropout(0.8)(x)
(x) uses the preceding function reference (returned by Dropout(0.8)) to make a function call with x as the argument.
For example:
def Dropout(a):
def func(b):
return a + b
return func
x = 0.2
print(Dropout(0.8)(x))
would output 1.0.