π 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:
-
Logical OR test:
The expression
num > 1 or num < 4
evaluates whether at least one of the conditions is true. The result is eitherTrue
orFalse
. -
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. -
Membership tests:
10 in lst
: Checks if the number10
is present in the list.4 in lst
: Checks if the number4
is present in the list.15 not in lst
: Checks if the number15
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:
- ➤ 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