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)

  1. What is the use of the elif statement?
  2. Write the syntax of the for loop.
  3. What does range(1, 10, 3) generate?
  4. Distinguish between break and continue.
  5. What is the use of the pass statement?
  6. What is a default parameter?
  7. What is the scope of a variable?
  8. What is a lambda function?
  9. What does the assert statement do?
  10. What are command line arguments?

Medium Answer Questions (5 marks each)

  1. Explain the conditional statements of Python with examples.
  2. Explain the for and while loops with examples.
  3. Explain break, continue and pass.
  4. Explain the definition and calling of a function with default parameters.
  5. Explain local and global variables with examples.
  6. Explain the use of a user defined module.

Long Answer Questions (15 marks each)

  1. Explain conditional statements and nested conditions. Write a program to find the largest of three numbers.
  2. 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.
  3. Explain functions in Python including default arguments, keyword arguments and variable arguments. Write a function to find the factorial of a number.
  4. 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