Print' Function in 'Ifelse'

How to write inline if statement for print?

Python does not have a trailing if statement.

There are two kinds of if in Python:

  1. if statement:

    if condition: statement
    if condition:
    block
  2. if expression (introduced in Python 2.5)

    expression_if_true if condition else expression_if_false

And note, that both print a and b = a are statements. Only the a part is an expression. So if you write

print a if b else 0

it means

print (a if b else 0)

and similarly when you write

x = a if b else 0

it means

x = (a if b else 0)

Now what would it print/assign if there was no else clause? The print/assignment is still there.

And note, that if you don't want it to be there, you can always write the regular if statement on a single line, though it's less readable and there is really no reason to avoid the two-line variant.

Why statement ifelse(i == 1,print(yes!),print(no.)) in R print no. twice?

print does two things:

  • It prints the parameter
  • It returns the parameter

If you just write print("a") on the console, the return value is not shown.

But if you write

a = print("a")
a

You see both values.

How do I get an if else statement to Print in the Else Portion?

Is this what you're trying to figure out?

recursive.factorial <- function(x) {
if (x == 0 || x > 10)
return (1)
else
return (print(x * recursive.factorial(x-1)))
}
recursive.factorial(9)
[1] 1
[1] 2
[1] 6
[1] 24
[1] 120
[1] 720
[1] 5040
[1] 40320
[1] 362880

recursive.factorial(13)
[1] 1

Why is ifelse generating print output?

Try wdir <- ifelse(Sys.info()[1]=="Linux", "/path/1", "/path/2"). The reason the output is printed is that if you don't assign the ifelse output to some variable, it will just print it on the screen. It is like writing a <- 1 + 2 vs 1 + 2.

How to get only one iteration in ifelse statement

Your function can be made simpler by using the ifelse() function.

Using your dataframe df <- as.data.frame(cbind("a" = 1:5,"b" = 6:10)), we can rewrite your function as:

testingIf <- function(x,dt){
ifelse(x %in% colnames(dt),"Hi","No")
}

Now testing the code, we have:

> testingIf("a",df)
[1] "Hi"
> testingIf("This is true",df)
[1] "No"

Hope this helps!

Can I define a function inside print() function or regular if-else statements & not using Ternary Operators in Python?

def to_smash(total_candies):
print("Splitting", total_candies, (lambda total_candies: "candies" if total_candies > 1 else "candy")(total_candies))
return total_candies % 3

to_smash(1)
to_smash(15)

However, note that Python isn't as versatile as Javascript in regards to passing functions -- lambda has its limitations, specifically being only a one-liner function. I would instead recommend just defining your function outside of the print statement all together.

def to_smash(total_candies):
def plural_or_singular(total_candies):
if total_candies>1:
return "candies"
else:
return "candy"

print("Splitting", total_candies, plural_or_singular(total_candies))
return total_candies % 3

to_smash(1)
to_smash(15)


Related Topics



Leave a reply



Submit