Skip to content

Commit 9264319

Browse files
committed
✔️ Boolean Variable Type
Chapter one Variables completed
1 parent 7af538b commit 9264319

2 files changed

Lines changed: 125 additions & 3 deletions

File tree

01.Variables/README.md

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ Welcome to the Python Variables section! In this section, you'll learn about the
1313
- [String Variables](#-string-variables)
1414
- [Float Variables](#-float-variables)
1515
- [Integer Variables](#-integer-variables)
16-
- [Boolean Variables](#boolean-variables)
17-
- [Summary](#summary)
18-
16+
- [Boolean Variables](#-boolean-variables)
1917
## 📚 Introduction
2018

2119
Variables are containers for storing data values. In Python, you don't need to declare the type of a variable, as Python is dynamically typed. This means you can assign different types of values to the same variable.
@@ -223,4 +221,85 @@ print(min_value) # Output: 1
223221
string_number = "123"
224222
integer_number = int(string_number)
225223
print(integer_number) # Output: 123
224+
```
225+
# ✅ Boolean Variables
226+
227+
A boolean variable is used to store one of two values: `True` or `False`. Booleans are often used in conditional statements and loops to control the flow of a program.
228+
229+
### Creating Boolean Variables
230+
231+
- You can create a boolean variable by assigning `True` or `False` to a variable name.
232+
233+
```python
234+
# Example of creating boolean variables
235+
is_student = True
236+
has_passed = False
237+
print(is_student) # Output: True
238+
print(has_passed) # Output: False
239+
```
240+
241+
### Logical Operations with Booleans
242+
243+
- You can perform various logical operations with booleans, such as `and`, `or`,`not`.
244+
245+
```python
246+
# Logical operations with booleans
247+
a = True
248+
b = False
249+
250+
and_operation = a and b
251+
or_operation = a or b
252+
not_operation = not a
253+
254+
print(and_operation) # Output: False
255+
print(or_operation) # Output: True
256+
print(not_operation) # Output: False
257+
```
258+
259+
### Comparison Operators
260+
261+
Comparison operators are used to compare values. They return a boolean value (True or False). Common comparison operators include:
262+
263+
- `==`: Equal to
264+
- `!=`: Not equal to
265+
- `>`: Greater than
266+
- `<`: Less than
267+
- `>=`: Greater than or equal to
268+
- `<=`: Less than or equal to
269+
270+
```python
271+
# Comparison operations
272+
x = 10
273+
y = 20
274+
275+
equal = x == y
276+
not_equal = x != y
277+
greater_than = x > y
278+
less_than = x < y
279+
greater_than_or_equal = x >= y
280+
less_than_or_equal = x <= y
281+
282+
print(equal) # Output: False
283+
print(not_equal) # Output: True
284+
print(greater_than) # Output: False
285+
print(less_than) # Output: True
286+
print(greater_than_or_equal) # Output: False
287+
print(less_than_or_equal) # Output: True
288+
```
289+
290+
### Boolean Methods
291+
292+
Python provides several built-in functions that return boolean values. Here are some common ones:
293+
294+
- `bool()`: Converts a value to a boolean.
295+
-`isinstance()`: Checks if an object is an instance of a specific class or type.
296+
297+
```python
298+
# Boolean methods
299+
value = 5
300+
boolean_value = bool(value)
301+
is_integer = isinstance(value, int)
302+
303+
print(boolean_value) # Output: True
304+
print(is_integer) # Output: True
226305
```

01.Variables/booleans.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Creating boolean variables
2+
is_student = True
3+
has_passed = False
4+
print(is_student) # Output: True
5+
print(has_passed) # Output: False
6+
7+
# Logical operations with booleans
8+
a = True
9+
b = False
10+
11+
and_operation = a and b
12+
or_operation = a or b
13+
not_operation = not a
14+
15+
print(and_operation) # Output: False
16+
print(or_operation) # Output: True
17+
print(not_operation) # Output: False
18+
19+
# Comparison operations
20+
x = 10
21+
y = 20
22+
23+
equal = x == y
24+
not_equal = x != y
25+
greater_than = x > y
26+
less_than = x < y
27+
greater_than_or_equal = x >= y
28+
less_than_or_equal = x <= y
29+
30+
print(equal) # Output: False
31+
print(not_equal) # Output: True
32+
print(greater_than) # Output: False
33+
print(less_than) # Output: True
34+
print(greater_than_or_equal) # Output: False
35+
print(less_than_or_equal) # Output: True
36+
37+
# Boolean methods
38+
value = 5
39+
boolean_value = bool(value)
40+
is_integer = isinstance(value, int)
41+
42+
print(boolean_value) # Output: True
43+
print(is_integer) # Output: True

0 commit comments

Comments
 (0)