03-Input-Boolean-Operators
03-Input-Boolean-Operators
The input() function in Python is a built-in method used to capture user input from the
console. When invoked, it displays an optional prompt to the user, waits for input, and
returns the entered data as a string.
Code:
0007-Python Booleans
What is a Boolean?
The bool type is a built-in subclass of int with exactly two instances: True (1) and False
(0).
Why it matters:
0008-Python Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic operators
Arithmetic operators let you perform basic mathematical operations on numeric values in
Python—essential for any calculation logic.
4. How would you get the absolute value of a number using arithmetic operators?
Assignment Operators
Assignment operators in Python are used to assign values to variables. They can also
perform operations like addition or bitwise shifts and assign the result to the variable in a
single step.
🔹 3. Examples
🧠 5. Practice Prompts
1. Write a loop that doubles a number until it exceeds 100, using *=.
2. Use the walrus operator to read input and process it only if it's not empty.
Comparison Operators
Comparison operators, also known as relational operators, are used to compare two
values. They return a Boolean value:True if the comparison is valid, and False
otherwise. These operators are fundamental in control flow statements like if, while,
🧠 6. Practice Exercises
1. Write a function that checks if a number is within a given range using comparison
operators.
2. Compare two strings and determine which one comes first alphabetically.
Logical Operators
Logical operators in Python are used to combine or modify Boolean expressions. They are
essential for controlling the flow of programs based on multiple conditions.
🔹 3. Code Examples
🧠 6. Practice Exercises
1. Write a function that returns True if a number is between 10 and 20, inclusive.
2. Use logical operators to determine if a string is non-empty and starts with a capital
letter.
Identity Operators
In Python, identity operators are used to compare the memory locations of two
objects. They determine whether two variables reference the same object in memory,
not just if they have equal values.
🔹 3. Code Examples
a is b returns False because a and b are different objects in memory, even though their
contents are equal.
1. Create two identical lists and verify whether they are the same object using is.
2. Compare two variables assigned the same string value and observe the result of is.
Membership Operators
Membership operators in Python are used to test whether a value or variable exists
within a sequence such as a string, list, tuple, set, or dictionary. They return a Boolean
value: True if the value is found in the sequence, and False otherwise.
🔹 3. Code Examples
🧠 6. Practice Exercises
3. Check if a specific key exists in a dictionary before attempting to access its value.
Bitwise operators
Bitwise operators in Python perform operations on the binary representations of integers.
They allow direct manipulation of individual bits, making them useful for tasks like low-
level programming, cryptography, and data compression.
🔹 3. Code Examples
🧠 6. Practice Exercises
1. Write a function that checks if a given integer is a power of two using bitwise
operators.
2. Implement a function that counts the number of set bits (1s) in the binary
representation of an integer.
3. Create a function that swaps two numbers without using a temporary variable,
utilizing bitwise XOR.