Example Task: Write a program to compute the area of a circle.
Steps:
Get the circle’s radius from the user.
Compute area using the formula: area = radius * radius * π
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.
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
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)
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: 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
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.
Syntax: var1, var2, ..., varn = exp1, exp2, ..., expn
Example:
x, y = 1, 2
x, y = y, x # Swap values
Definition: Identifiers representing permanent values used in a program.
Benefits:
Reduces repeated typing of the same value.
Easier to update values in one place.
Improves code readability.
Types: Integers and floating-point numbers.
Operators: Used with numeric types: +, -, *, /, //, **, %.
Example: 1.0
is float; 1
is integer.
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")
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.
Order of Operations:
Exponentiation (**
)
Multiplication, Float Division, Integer Division, Remainder
Addition and Subtraction
Overview: Combine arithmetic operators with assignment.
Operators: +=
, -=
, *=
, /=
, //=
, %=
, **=
.
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.
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
).
Write a program to sum digits of an integer.
Example: Input (932
), Output: Sum (14
).
Calculate Body Mass Index (BMI) based on weight and height.
Conversion: 1 pound = 0.45359237 kg, 1 inch = 0.0254 m.