Test answer of the boolean 'true' explaination - python-2.7

Im very confused why the first 3 arnt all correct. As isn't true a keyword, it of course is a Boolean literal and is interchangeable with 1?

True and False are not keywords, they are boolean values because they are associated with 1 and 0 respectively. For a complete list of keywords see Is it possible to get a list of keywords in Python?

Related

Print one result after combining multiple expression results using jq

I want to have the output of either true or false.
I have this syntax
.[] | (if (select(.displayName=="display")) then "yes" else "no" end)
This is the json source
[{"displayName":"display","message":"bla bla"}, {"displayName":"test","message":"bla bla"}]
I only need to query against the array and if the value im looking for exists in one of them I need the output to be "yes", and if it doesnt a "no". So a single output value is what I am looking for.
This evaluates to "yes", however, if the value display is not present in the json, it does not output "no", its just empty. Can anyone tell me why?
Here is a snippet to try: https://jqplay.org/s/WKcZh91hk8L
The idea is right, but you shouldn't be using select for this. The way select works is by evaluating the boolean expression provided inside (..) and returning the object, if the expression evaluates to true and skip the object from being considered/printed on false.
On your code, for the object that evaluates to false, select is evaluated as select(false) which as explained above returns nothing - this is not evaluated to boolean false condition. i.e. the return value of select is not a boolean value.
To use explicit boolean conditions, drop the select from expression and to summarise the result to a single value do something like below
if ( [ .[] | .displayName == "display" ] | any ) then "yes" else "no" end
Demo on jqplay
The way this works, is any can evaluate an array of boolean values and produces true as output if any of the elements of the array are true. We collect the match condition results to the array and apply any on it. i.e. [true, false] evaluates to true, thereby printing yes only once
A concise solution is possible with select:
select( any(.displayName=="display") ) | "yes" // "no"

If Statements in Lua

In all other programming languages I've encountered, if statements always require a boolean to work, however in Lua the following code does not contain any errors. What is being checked in this if statement if both true and false statements can't be made? How can you only check "if (variable) then"? I am new to programming and currently working with Roblox Studio, any help would be very appreciated.
function onTouched(Obj)
local h = Obj.Parent:FindFirstChild("Humanoid")
if h then
h.Health = 0
end
end
script.Parent.Touched:Connect(onTouched)
Most languages have their own rules for how values are interpreted in if statements.
In Lua, false and nil are treated as false. All other values are treated as true.
if h == nil (null)
So if it couldn't find a humanoid in the object that touched the script's parent, it will be false (null), otherwise true (not null).
So
if [ObjectName] then
equals to if [ObjectName] != null then
*Only valid for objects (non primitive values)
It's like that in script languages.
if h then end
is basically equivalent to
if h ~= nil and h ~= false then end
In Lua all values that are not nil or false are considered to be logically true.
if h then end is usually used to check wether h is not nil. So if code depends on wether h has been defined you put it in a condition like that.
In your example h is being index. indexing nil values is not allowed. So befor you index it you should make sure it isn't nil to avoid errors.
Checking return values of functions is good practice.

Python 2.7 Boolean Operators Logic

I am currently in the course of learning Python 2.7 and have come across the Equality and Boolean operators
My question is:
Why False and 1 is False but True and 1 is 1
Likewise, False or 1 is 1 but True or 1 is True
Can someone kindly explain why this is happening
Many thanks
and returns the first 'falsy' (False, zero, empty string or list, etc.) value it sees, or the final value if none were falsy. Further values are not even evaluated, since they can't change the result.
or likewise returns the first 'truthy' (True, non-zero, non-empty string or list, etc.) value it sees (or the final one if there were none), and doesn't evaluate the rest.
This behavior is sometimes more convenient than strictly returning only True or False.

trying to validate string as boolean Coldfusion

I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />

What Integers Are Considered "true" in a ColdFusion "if" statement [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
What are all the values that ColdFusion considers “falsy” and “truthy”?
In ColdFusion, what integers are considered true and what integers are considered false in an if statement? I know that 1 is true and 0 is false, but is this it?
For example, it would be nice to know that I can do an if on a query data structure's record count to determine if the query has at least one record in it. For example, let's say our query is called, employees.
<cfif variables.employees.recordCount>
There ARE employees.
<cfelse>
There ARE NO employees.
</cfif>
Would the statement "There ARE employees" be displayed if the record count was, say, 2?
Any non zero number is true. Only 0 will return false.
The expression inside the cfif tag must evaluate to a Boolean value (a true/false). Boolean values are represented as either "true" or "false", "yes" or "no", or "0" or any number.
Reference
So it looks like 0 is for false, or anything else for true in the case of integers.
Very similar question:
What are all the values that ColdFusion considers "falsy" and "truthy"?
Extensive blog post about how variables are evaluated for truthiness in ColdFusion:
What are all the values that ColdFusion considers "falsy" and "truthy"?