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

 

πŸ“˜ Python Crash Course – Chapter 5: if Statements – Exercise 5-2 Solution

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


πŸ“ Exercise 5-2

Task:
You don’t have to limit the number of tests you create to 10 . If you want to try more comparisons, write more tests and add them to conditional_tests.py . Have at least one True and one False result for each of the following:
• Tests for equality and inequality with strings
• Tests using the lower() function
• Numerical tests involving equality and inequality, greater than and less than, greater than or equal to, and less than or equal to
• Tests using the and keyword and the or keyword
• Test whether an item is in a list
• Test whether an item is not in a list

✅ Solution Code:


str = "JaRVis"
num= 5

print(num == 10)
print(num !=5)
print(num==5)

print("----------------------------------------------------------------")

print(str == "jsrvis")
print(str != "jarvis")
print(str.lower() == "jarvis")

print("----------------------------------------------------------------")

print(num >=6)
print(num<10)
print(num>=3)
print(num>1)

print("----------------------------------------------------------------")

print(num >3 and num <10)
print(num>1 or num <4)

print("----------------------------------------------------------------")

lst = [1,2,3,4,5,6,7]
print(10 in lst)
print(4 in lst)
print(15 not in lst)

print("----------------------------------------------------------------")

🧠 Code Explanation:

This Python code demonstrates various conditional tests, including logical operators and membership tests. Here's how it works:

  1. Logical OR test:

    The expression num > 1 or num < 4 evaluates whether at least one of the conditions is true. The result is either True or False.

  2. Define a list:

    The variable lst is assigned a list of numbers: [1, 2, 3, 4, 5, 6, 7]. This list is used for membership tests.

  3. Membership tests:
    • 10 in lst: Checks if the number 10 is present in the list.
    • 4 in lst: Checks if the number 4 is present in the list.
    • 15 not in lst: Checks if the number 15 is not present in the list.

This code demonstrates how to use logical operators (and, or) and membership operators (in, not in) to perform conditional tests in Python.

πŸ” Output:


False
False
True 
----------------------------------------------------------------
False
True
True
----------------------------------------------------------------
False
True
True
True
----------------------------------------------------------------
True
True
----------------------------------------------------------------
False
True
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

“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