FAQ: Modifying DataFrames - Reviewing Lambda Function: If Statements

This community-built FAQ covers the “Reviewing Lambda Function: If Statements” exercise from the lesson “Modifying DataFrames”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise Reviewing Lambda Function: If Statements

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Got a SyntaxError on:

mylambda = lambda x: “Welcome to BattleCity!”/
if x >= 13/
else “You must be over 13”

(Have tried with print/return and all sort of ([parenthesis])

Whats wrong?

1 Like

Hi, I suggest trying without the line continuations if you haven’t already (they’re also shown as back slashes in the instructions).

Also note that if you use a backslash in this case you would still get a syntax error as you will need a space before or after it because the backslash enables the line of code to work on multiple lines. e.g.


You can write the whole expression in one line however this can get messy and can be harder to understand when the filled statements get larger in size. this is why it might be easier to break it down into smaller portions which is given in the example below.

lambda x: [OUTCOME IF TRUE] \     #has space before backslash
    if [CONDITIONAL] \            #will now space out the word 'if' from last line of code
1 Like

so I tried:

mylambda = lambda age: print('Welcome to BattleCity!') \
  if age >= 13 \
  else print('You must be over 13')


and I get an invalid syntax error. If I take out the print functions and instead just have the strings it will work. Why can’t lamba contain another function such as print? Or am I just doing this wrong?


I think this happens because the role of the lambda function is to return the outcome. I am pretty sure you can’t return a print function. Correct me if I am wrong…

Works on IDLE & PyCharm, direct cut & paste from your post, second call added by me:

>>> mylambda = lambda age: print('Welcome to BattleCity!') \
  if age >= 13 \
  else print('You must be over 13')
Welcome to BattleCity!
>>> mylambda(7)
You must be over 13
1 Like

Can we use if-elif-else in lambda fuction?

1 Like

I’ve typed out what I think is the correct answer ( not using the backslashes) but I still get a syntax error on line 1. Why is this? it says you don’t need to use the backslashes but it doesn’t seem to accept the answer any other way

I’m a little confused. I get the outcome that is expected, but I am not correct according to the codecademy workspace. I see people mentioning ‘/’ in the discussion and when I viewed the solution it had this syntax, however the example and discussion are aligned with the syntax that I had originally. What am I doing wrong in my original solution?

There is no need to use the command Print in the lambda function.