Exercise 5-1 Solution Python Crash Course Chapter 5: if Statements

 

📘 Python Crash Course – Chapter 5: if Statements – Exercise 5-1 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


📝 Exercise 5-1

Task:
Write a series of conditional tests .
Print a statement describing each test and your prediction for the results of each test . Your code should look something like this:
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
• Look closely at your results, and make sure you understand why each line evaluates to True or False.
• Create at least 10 tests . Have at least 5 tests evaluate to True and another 5 tests evaluate to False

✅ Solution Code:


result_prediction = 10

print(f"Is predicted result is 4 : {result_prediction == 4}")
print(f"Is predicted result is 10 : {result_prediction == 10}")
print(f"Is predicted result is 5 : {result_prediction == 5}")
print(f"Is predicted result is 6 : {result_prediction == 6}")
print(f"Is predicted result is 10 : {result_prediction == 10}")
print(f"Is predicted result is 8 : {result_prediction == 8}")
print(f"Is predicted result is 9 : {result_prediction == 9}")
print(f"Is predicted result is 10 : {result_prediction == 10}")
print(f"Is predicted result is 10 : {result_prediction == 10}")
print(f"Is predicted result is 10 : {result_prediction == 10}")

🧠 Code Explanation:

This Python code demonstrates a series of conditional tests using comparison operators. Here's how it works:

  1. Define a variable:

    The variable result_prediction is assigned the value 10. This value will be used in the conditional tests.

  2. Perform equality tests:

    A series of equality tests are performed using the == operator to compare the value of result_prediction with various numbers. Each test evaluates to either True or False depending on whether the comparison is correct.

  3. Display test results:

    Each test result is displayed along with a descriptive message indicating the expected outcome of the test.

This code demonstrates how to use conditional tests to evaluate expressions and display their results in Python.

🔍 Output:


Is predicted result is 4 : False
Is predicted result is 10 : True
Is predicted result is 5 : False
Is predicted result is 6 : False
Is predicted result is 10 : True
Is predicted result is 8 : False
Is predicted result is 9 : False
Is predicted result is 10 : True
Is predicted result is 10 : True
Is predicted result is 10 : True

📚 Related Exercises from Chapter 5:


🔗 Connect With Us:

“Programs must be written for people to read, and only incidentally for machines to execute.”
— Harold Abelson

📌 Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips

Previous Post Next Post

Contact Form