Unit summary
- if, elif and else choose between blocks, and the first True condition runs.
- A conditional expression selects between two values in one line.
- while repeats while a condition is True and for visits the items of a sequence.
- range(a, b, step) generates numbers up to but not including b.
- break leaves a loop, continue skips a pass, and pass does nothing.
- Functions are defined with def and give values with return.
- Parameters may have defaults, and arguments may be passed by keyword.
- Command line arguments are stored as strings in sys.argv.
- Local variables live only during a call, and global changes need the global keyword.
- lambda, assert and modules support short code, checks and reuse.
Glossary
| Term | Meaning |
|---|---|
| Argument | A value supplied when a function is called. |
| assert | A statement that checks a condition and raises an error if it fails. |
| break | A statement that ends a loop. |
| Condition | An expression that is True or False. |
| continue | A statement that skips to the next pass of a loop. |
| Default parameter | A parameter with a preset value. |
| elif | A test made when the earlier conditions fail. |
| Function | A named block of code that performs a task. |
| Global variable | A variable created outside all functions. |
| Lambda | A small anonymous function written in one line. |
| Local variable | A variable created inside a function. |
| Loop | A structure that repeats a block of statements. |
| Module | A Python file whose names can be used in other programs. |
| Nesting | Placing one statement structure inside another. |
| Parameter | A name in a function definition that receives a value. |
| pass | A statement that does nothing. |
| range() | A function that produces a sequence of numbers. |
| return | A statement that sends a value back from a function. |
| Scope | The region where a name can be used. |
| sys.argv | The list of command line arguments. |
Question bank
Short Answer Questions (2 marks each)
- What is the use of the elif statement?
- Write the syntax of the for loop.
- What does range(1, 10, 3) generate?
- Distinguish between break and continue.
- What is the use of the pass statement?
- What is a default parameter?
- What is the scope of a variable?
- What is a lambda function?
- What does the assert statement do?
- What are command line arguments?
Medium Answer Questions (5 marks each)
- Explain the conditional statements of Python with examples.
- Explain the for and while loops with examples.
- Explain break, continue and pass.
- Explain the definition and calling of a function with default parameters.
- Explain local and global variables with examples.
- Explain the use of a user defined module.
Long Answer Questions (15 marks each)
- Explain conditional statements and nested conditions. Write a program to find the largest of three numbers.
- Explain loops and loop control statements. Write programs to print the first n terms of the Fibonacci series and to check whether a number is prime.
- Explain functions in Python including default arguments, keyword arguments and variable arguments. Write a function to find the factorial of a number.
- Explain the scope and lifetime of variables, lambda functions, the assert statement and user defined modules with examples.
Sources
- Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit II
