Programming with Python · free preview

Python Fundamentals

Assign values to variables.

1. Overview

Overview

2. Variables

Variables

3. Types of data

Types of data

4. Using Variables in Python

Using Variables in Python

5. Built-in Python functions

Built-in Python functions

6. Getting Help

Getting Help

7. Generative AI

Generative AI

8. Variables as Sticky Notes

Variables as Sticky Notes

9. Check Your Understanding

Check Your Understanding

10. Sorting Out References

Sorting Out References

11. Seeing Data Types

Seeing Data Types

12. Trace the final bindings

After mass = 47.5, age = 122, mass = mass * 2.0, and age = age - 20, what are the final values? Answer as mass, age.

13. Trace, weight, pounds, and a patient identifier

Run this complete program, predict each output line, then compare the trace with your prediction.

Example. Trace, weight, pounds, and a patient identifier

Problem. Run this complete program, predict each output line, then compare the trace with your prediction.

Correct solution.

Complete program.```python weight_kg = 60 patient_id = '001' weight_lb = 2.2 * weight_kg patient_id = 'inflam_' + patient_id print('weight in kilograms:', weight_kg) print('weight in pounds:', weight_lb) print('patient identifier:', patient_id) print('type check:', type(weight_kg), type(patient_id))

code

Expected output.```output
weight in kilograms: 60
weight in pounds: 132.0
patient identifier: inflam_001
type check: <class 'int'> <class 'str'>

Trace.

  1. weight_kg = 60 binds a numeric value to the name weight_kg.
  2. patient_id = '001' creates a string so the leading zero survives.
  3. weight_lb = 2.2 * weight_kg reads weight_kg, computes 132.0, and binds that value to weight_lb.
  4. patient_id = 'inflam_' + patient_id evaluates the concatenation first, then rebinds patient_id.
  5. The print calls show that later assignments do not retroactively change weight_kg or weight_lb.

关键提醒。

Assignment binds a value to a name. Reassignment evaluates the new right-hand expression first and then replaces the name's current value.

14. Retrieval, one rebinding operation

Without looking back, write the Python statements that start from dose_mg = 12.5, double the dose, and print the current value. State the values before and after the doubling.

Give the exact assignment statement, the exact print statement, and the two values.

15. Mastery, apply assignment to a new identifier

Apply assignment to a new study record, start with study_id = '207', add the prefix case_, and print both the old and current values. Give the exact statements and exact output.

Use one extra assignment for the old value so it survives the reassignment.

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free