How to write inline if statement for print?
Python does not have a trailing if
statement.
There are two kinds of if
in Python:
if
statement:if condition: statement
if condition:
blockif
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
R Calculate the Average of One Column Corresponding to Each Bin of Another Column
Calculate the Derivative of a Data-Function in R
R: Interactive Plots (Tooltips): Rcharts Dimple Plot: Formatting Axis
Determining Minimum Values in a Vector in R
How Does Settimelimit Work in R
R: Ggplot2: Adding Count Labels to Histogram with Density Overlay
How to Replace the String Exactly Using Gsub()
How to Save a Data Frame in a Txt or Excel File Separated by Columns
Apply a Function to All Variables Starting with Specific Pattern in R
Does R-Server or Shiny Server Create a New R Process/Instance for Each User
Specifying the Colour Scale for Maps in Ggplot
Dplyr . and _No Visible Binding for Global Variable '.'_ Note in Package Check
Plot Only One Side/Half of the Violin Plot
Error: X Must Be Atomic for 'Sort.List'
Sending in Column Name to Ddply from Function
How to Read the Files in a Directory in Sorted Order Using R