Python Practice Problems: Variables, Data Types, and Formatting

Exercise 1: Variable Assignment, Calculation, and F-Strings

Python notebook showing Exercise 1 code and output
  • Overview:

    • Demonstrates fundamental Python concepts including variable declaration, value reassignment, arithmetic operations, and string formatting using f-strings.

  • Source Code Breakdown:

    • 1 #Practice Problems: Single-line comment designating the section header.

    • 3 print("Exercise 1"): Prints the string literal header "Exercise 1".

    • 4 grade=93: Initializes variable grade with integer value 93.

    • 5 print("ITM_251 Grade:", grade): Prints string literal "ITM_251 Grade:" followed by the current value of grade (93).

    • 7 grade=100: Reassigns variable grade to integer value 100, replacing the previous value.

    • 8 print("QMB_210 Grade:", grade): Prints string literal "QMB_210 Grade:" followed by the updated value of grade (100).

    • 10 average_score=((93+100)/2): Calculates the arithmetic average:     average_score=93+1002=96.5average\_score = \frac{93 + 100}{2} = 96.5

    • 11 print("Average Score:", average_score): Prints string literal "Average Score:" followed by 96.5.

    • 13 print("Julie's Average Score is", average_score): Prints text string alongside the evaluated variable 96.5.

    • 15 name="Julie": Assigns string literal "Julie" to variable name.

    • 16 score=93: Assigns integer 93 to variable score.

    • 17 class_name="ITM_251": Assigns string literal "ITM_251" to variable class_name.

    • 18 print(f"Yay, {name}! You scored a {score} in {class_name}."): Uses formatted string literal (f-string) to interpolate variable values directly inside curly braces {}.

  • Console Output:

    • Exercise 1

    • ITM_251 Grade: 93

    • QMB_210 Grade: 100

    • Average Score: 96.5

    • Julie's Average Score is 96.5

    • Yay, Julie! You scored a 93 in ITM_251.

  • Key Technical Takeaways:

    • Variable Reassignment: Variables are updated sequentially; subsequent assignments overwrite previous stored values.

    • Division Output: Arithmetic division / in Python evaluates to a floating-point number (96.5).

    • F-String Interpolation: Prefixing a string with f allows expressions inside curly braces {} to be dynamically inserted into string output.

Exercise 2: Boolean Data Type and Type Inspection

Python notebook showing Exercise 2 and Exercise 3 code and output
  • Overview:

    • Demonstrates Boolean variable instantiation and utilization of the built-in type() function to evaluate data types at runtime.

  • Source Code Breakdown:

    • 1 print("Exercise 2"): Prints section header string "Exercise 2".

    • 3 winter= False: Assigns Boolean keyword False to variable winter.

    • 4 print(winter,type(winter)): Prints the value stored in winter followed by the evaluation of type(winter).

  • Console Output:

    • Exercise 2

    • False <class 'bool'>

  • Key Technical Takeaways:

    • Boolean Data Type: bool objects represent one of two binary logical states: True or False (case-sensitive keywords).

    • Type Inspection: Calling type() returns the class type of any Python object, evaluating to <class 'bool'> for Booleans.

Exercise 3: Multi-Variable Calculation and Format Specifiers

  • Overview:

    • Demonstrates arithmetic operations combining floating-point and integer numbers, alongside numeric string formatting specifiers.

  • Source Code Breakdown:

    • 1 print("Exercise 3"): Prints header string "Exercise 3".

    • 3 item_name = "Widget": Assigns string "Widget" to variable item_name.

    • 4 price=15.99: Assigns float value 15.99 to variable price.

    • 5 quantity = 4: Assigns integer 4 to variable quantity.

    • 7 total_cost = price*quantity: Computes total cost via multiplication:     total_cost=15.99×4=63.96total\_cost = 15.99 \times 4 = 63.96

    • 9 print(f"Item: {item_name}, Quantity: {quantity}, Total Cost: ${total_cost:,.2f}"): Formats total cost within an f-string using specifier :,.2f.

  • Console Output:

    • Exercise 3

    • Item: Widget, Quantity: 4, Total Cost: $63.96

  • Key Technical Takeaways:

    • Mixed-Type Multiplication: Multiplying a float (15.99) by an integer (4) returns a float (63.96).

    • F-String Formatting Specifier (:,.2f):

    • : initiates the formatting options.

    • , adds thousands separators for comma delimitation.

    • .2f specifies floating-point formatting constrained to exactly two decimal places.