0% found this document useful (0 votes)
2 views9 pages

03-Input-Boolean-Operators

The document provides an overview of various Python concepts including user input, Boolean values, and different types of operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the purpose and functionality of each operator type and includes active recall questions and practice exercises for better understanding. The content is aimed at enhancing Python programming skills through practical examples and exercises.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

03-Input-Boolean-Operators

The document provides an overview of various Python concepts including user input, Boolean values, and different types of operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the purpose and functionality of each operator type and includes active recall questions and practice exercises for better understanding. The content is aimed at enhancing Python programming skills through practical examples and exercises.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

0006- Python User Input

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:

 Drives control flow (if/while),

 Enables logical decisions,

 Underpins truth-testing across Python’s core (e.g., empty sequences evaluate to


False).

4. Active Recall Questions

1. What are the only two instances of the bool class?


2. How does bool() decide truthiness for numbers and collections?

3. What’s the difference between is and == when comparing booleans?

4. What will True and 5 evaluate to, and why?

5. Name three Python values that evaluate to False in a boolean context.

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.

2. Operator Reference Table

5. 🧠 Active Recall Questions

1. What’s the difference between / and //?


2. How do you compute “a to the power of b”?

3. What does 7 % -3 evaluate to, and why?

4. How would you get the absolute value of a number using arithmetic operators?

5. Why might 2 ** 1000 be problematic in some contexts?

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.

🔸 2. Assignment Operators Overview

🔹 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.

3. Demonstrate the use of &=, |=, and ^= with binary numbers.

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,

and for loops.

🔸 2. List of Comparison Operators


🔹 3. Code Examples

🧠 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.

3. Use chained comparisons to check if a value falls outside a specific range.

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.

🔸 2. List of Logical Operators

🔹 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.

3. Demonstrate the use of not to invert a Boolean expression.

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.

🔸 2. Identity Operators Overview

🔹 3. Code Examples

a is b returns False because a and b are different objects in memory, even though their
contents are equal.

a is c returns True because c references the same object as a.

a == b returns True because the contents of a and b are equal.


🧠 6. Practice Exercises

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.

3. Use is not to check if two variables reference different objects.

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.

🔸 2. List of Membership Operators

🔹 3. Code Examples

🧠 6. Practice Exercises

1. Write a function that checks if a given character is a vowel by testing its


membership in a list of vowels.

2. Determine if a user's input is not in a list of prohibited words.

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.

🔸 2. List of Bitwise Operators

🔹 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.

You might also like