Python function definitions, loops, and conditional logic explained with a short example each.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- How do you define a function in Python?
- Use def, the function name, parentheses, a colon, then indent the body.
- What does a function parameter do?
- A parameter is a named placeholder for a value passed when the function is called.
- How do you return a value from a function?
- Use the return keyword followed by the value to send back to the caller.
- What is a default parameter?
- A parameter with a preset value used when no argument is passed for that parameter.
- Give an example of a function with two parameters
- def add(a, b): return a + b
- What happens if a function has no return statement?
- The function returns None implicitly.
- What is a function docstring?
- A string literal placed right after the def line to document what the function does.
- How do you call a function named greet?
- Write the function name followed by parentheses: greet()
- What are positional arguments?
- Arguments passed to a function in the order they appear in the definition.
- How do you pass keyword arguments to a function?
- Use the parameter name followed by an equals sign and the value: func(name='Alice')
- What does *args allow you to do?
- Pass a variable number of positional arguments to a function.
- What does **kwargs allow you to do?
- Pass a variable number of keyword arguments to a function.
- What is the basic structure of an if statement?
- if condition: followed by indented code that runs only if condition is true
- What is the structure of an if-else statement?
- if condition: runs if true, else: runs if condition is false
- How do you add a third branch to a conditional?
- Use elif (else if) for additional conditions between if and else.