π 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:
-
Define a variable:
The variable
result_prediction
is assigned the value10
. This value will be used in the conditional tests. -
Perform equality tests:
A series of equality tests are performed using the
==
operator to compare the value ofresult_prediction
with various numbers. Each test evaluates to eitherTrue
orFalse
depending on whether the comparison is correct. -
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:
- ➤ Exercise 5-1 | Conditional Tests
- ➤ Exercise 5-2 | More Conditional Tests
- ➤ Exercise 5-3 | Alien Colors #1
- ➤ Exercise 5-4 | Alien Colors #2
- ➤ Exercise 5-5 | Alien Colors #3
- ➤ Exercise 5-6 | Stages of Life
- ➤ Exercise 5-7 | Favorite Fruit
- ➤ Exercise 5-8 | Hello Admin
- ➤ Exercise 5-9 | No Users
- ➤ Exercise 5-10 | Checking Usernames|
- ➤ Exercise 5-11 | Ordinal Numbers
- ➤ Exercise 5-12 | Styling if statements
- ➤ Exercise 5-13 | Your Ideas
π 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
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment