Branching, Loops, and Functions

External Resources

Common Questions

Q1: Does Python have a debugger?

A: There's %debug magic command you can use but using print statement to see how your code is performing is a better option.

Q2: Are pass and break statements interchangeable?

A: No. pass does nothing where as break breaks out of the loop.

Q3: What does list1[:-1] mean?

A: It removes the last element from the list list1.

Q4: What is a valid puzzle?

A: A valid puzzle is a puzzle with any number of zeroes, but all other numbers i.e from 1 to 9 should be present only once in a row/column/square block.

Q5: How do we take value from user?

A: You can use input() function to take value from the user.

Q6: Is there a switch case statement in Python?

A: No, there is no switch case statement in Python. Switch case is used to check many conditions one by one. In Python, we have if, elif, elif instead.

Q7: What is the best structure to follow when coding?

A: In general, you want to keep your code small.

Q8: What is %% ?

A: %% is a magic command, if you use this in Python script, it won't be able to understand it but Jupyter understands that it is a directive and executes it.

Q9: Can you put a counter on a while loop?

A: A while loop doesn't provide counter instead you can put a print statement inside the loop that can act as a counter.

Q10: What are lambda functions in Python?

A: In Python, Lambda functions or anonymous functions are functions that are defined without a name but can be assigned a variable name. Lambda functions in Python and other programming languages have their roots in Lambda Calculus.

The standard functions in Python are defined using the def keyword whereas the anonymous/lambda functions are defined using the lambda keyword.

The syntax for Lambda functions is as follows:

lambda arguments: expression

These functions can take any number of arguments but evaluates and returns only one expression without using the return keyword.

The following should be kept in mind while using lambda functions in Python:

  • It can only contain expressions and can’t include statements. Statements like return, pass, assert, or raise will raise a SyntaxError in Python.
  • It is written as a single line of code and can be immediately invoked.
  • It is essentially an expression, that is why it can be named or not.