School ICT Self Study

Imperative Programming and Control Structures in Python

0

  1. What is the purpose of comments in a Python program?

  2. Name three primitive data types in Python.

  3. What is operator precedence, and why is it important?

  4. Write a Python program to accept a number from the keyboard and print its square.

  5. Explain the difference between selection and repetition control structures with Python examples.

Spread the love
Ruwan Suraweera Changed status to publish 1 day ago
0

Answers and Descriptions:

  1. Answer: Comments explain code for readability and future reference.
    Description: In Python, single-line comments use #, and multi-line use β€œβ€β€. They don’t affect execution but help developers understand code logic.

  2. Answer: Integer (int), float, string (str).
    Description: These are basic data types; int stores whole numbers, float stores decimals, and str stores text, each with specific operations.

  3. Answer: Operator precedence determines the order of operations in an expression.
    Description: For example, in 3 + 4 * 2, multiplication (*) has higher precedence than addition (+), so the result is 11, not 14. Parentheses can override precedence.

  4. Answer:

    Description: The program uses input() to get a number, converts it to an integer, calculates its square, and prints the result.

  5. Answer: Selection chooses a path based on a condition (e.g., if); repetition repeats code (e.g., while).
    Description: Example: if x > 0: print(β€œPositive”) is selection, executing once if true. while x < 5: x += 1 is repetition, looping until the condition is false.

Spread the love
Ruwan Suraweera Changed status to publish 1 day ago
Write your answer.