Python-Chapter2 (1)

Chapter 2: Elementary Programming

Writing a Simple Program

  • Example Task: Write a program to compute the area of a circle.

    • Steps:

      1. Get the circle’s radius from the user.

      2. Compute area using the formula: area = radius * radius * π

      3. Display the result.

  • Sample Code (ComputeArea.py):

    radius = 20  # Assign a value to radius
    area = radius * radius * 3.14159 # Compute area
    print("The area for the circle of radius", radius, "is", area) # Display results
  • Tip: Always outline your program as an algorithm before starting to code.

Reading Input from the Console

  • Input Method: Use input() function to read user input:

    variable = input("Enter a value:")  # Input as string
  • Evaluating Input: Use eval() to convert to a numeric value.

    • Examples:

      • eval("34.5") returns 34.5.

      • eval("345") returns 345.

      • eval("3 + 4") returns 7.

  • Links: eval Function | input Function

Handling Console Input for Area Calculation

  • Sample Code (ComputeAreaWithConsoleInput.py):

    radius = eval(input("Enter a value for radius:"))
    area = radius * radius * 3.14159 # Compute area
    print("The area for the circle of radius", radius, "is", area)

Identifiers

  • Definition: Identifiers are names for elements like variables and functions.

  • Examples: radius, area, input, eval, print

  • Rules:

    • Must start with a letter or underscore.

    • Cannot be a keyword (e.g., import).

    • Can be of any length.

Variables, Assignment Statements, and Expressions

  • Variables: Reference values that can change.

  • Assignment Operator: = is used to assign values.

  • Syntax: variable = expression

    • Example assignments:

      • y = 1

      • radius = 1.0

      • x = 5 * (3 / 2) + 3 * 2

  • Multiple Variable Assignment: i = j = k = 1

Scope of Variables

  • Definition: The part of the program where the variable can be used.

  • Caution: A variable must be created before use to avoid references to uninitialized variables.

Simultaneous Assignments

  • Syntax: var1, var2, ..., varn = exp1, exp2, ..., expn

  • Example:

    x, y = 1, 2
    x, y = y, x # Swap values

Named Constants

  • Definition: Identifiers representing permanent values used in a program.

  • Benefits:

    1. Reduces repeated typing of the same value.

    2. Easier to update values in one place.

    3. Improves code readability.

Numeric Data Types and Operators

  • Types: Integers and floating-point numbers.

  • Operators: Used with numeric types: +, -, *, /, //, **, %.

  • Example: 1.0 is float; 1 is integer.

Display Time Example

  • Sample Code (DisplayTime.py):

    seconds = eval(input("Enter an integer for seconds:"))
    minutes = seconds // 60
    remainingSeconds = seconds % 60
    print(seconds, "seconds is", minutes, "minutes and", remainingSeconds, "seconds")

Scientific Notation

  • Format: Can be represented as a x 10^b. Examples:

    • 1.23456E2 = 123.456

    • 1.23456E-2 = 0.0123456

  • Caution: Use of numbers beyond memory capacity can lead to overflow errors.

Evaluating Expressions and Operator Precedence

  • Order of Operations:

    1. Exponentiation (**)

    2. Multiplication, Float Division, Integer Division, Remainder

    3. Addition and Subtraction

Augmented Assignment Operators

  • Overview: Combine arithmetic operators with assignment.

    • Operators: +=, -=, *=, /=, //=, %=, **=.

Type Conversions and Rounding

  • Rules: If an operand is a float, the result is a float. Integers convert to float during operations.

  • Conversion Example: int(value) for integer part.

  • Rounding Example: round(value) to round to the nearest whole number.

Programming Exercises

  1. Write a program to compute gratuity based on subtotal and gratuity rate.

    • Example: Enter subtotal (10), gratuity rate (15%).

    • Output: Gratuity (1.5), Total (11.5).

  2. Write a program to sum digits of an integer.

    • Example: Input (932), Output: Sum (14).

  3. Calculate Body Mass Index (BMI) based on weight and height.

    • Conversion: 1 pound = 0.45359237 kg, 1 inch = 0.0254 m.

robot