Skip to content

Commit 74647d4

Browse files
authored
Merge pull request #16 from khd33j/feature
Feature: Add Python notes
2 parents 3344de5 + df09053 commit 74647d4

File tree

7 files changed

+1109
-0
lines changed

7 files changed

+1109
-0
lines changed

Python/Basic Operators.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Basic Operators
2+
3+
## Table of Contents
4+
5+
- **[Arithmetic Operators](#arithmetic-operators)**
6+
- **[Comparison Operators](#comparison-operators)**
7+
- **[Logical Operators](#logical-operators)**
8+
- **[Operator Precedence](#operator-precedence)**
9+
- **[Highest to Lowest Precendence:](#highest-to-lowest-precendence))**
10+
11+
---
12+
13+
## Arithmetic Operators
14+
15+
Arithmetic operators perform mathematical operations on numbers (integers, floats).
16+
17+
| Operator | Symbol | Description | Example |
18+
|:----------|:-------|:------------|:--------|
19+
| **Addition** | `+` | Adds two operands | `3 + 2 = 5` |
20+
| **Subtraction** | `-` | Subtracts second operand from first | `5 - 2 = 3` |
21+
| **Multiplication** | `*` | Multiplies two operands | `3 * 2 = 6` |
22+
| **Division** | `/` | Divides first operand by second, returns float | `5 / 2 = 2.5` |
23+
| **Modulus** | `%` | Returns remainder of division | `5 % 2 = 1` |
24+
| **Floor Division** | `//` | Divides first operand by second, returns integer (floor) | `5 // 2 = 2` |
25+
| **Exponentiation** | `**` | Raises first operand to the power of second | `3 ** 2 = 9` |
26+
27+
**Example:**
28+
29+
```python
30+
a = 10
31+
b = 3
32+
print(a + b) # 13
33+
print(a - b) # 7
34+
print(a * b) # 30
35+
print(a / b) # 3.3333333333333335
36+
print(a % b) # 1
37+
print(a // b) # 3
38+
print(a ** b) # 1000
39+
```
40+
41+
---
42+
43+
## Comparison Operators
44+
45+
Comparison operators are used to compare two values. They return either `True` or `False`.
46+
47+
| Operator | Symbol | Description | Example |
48+
|:----------|:-------|:------------|:--------|
49+
| **Equal to** | `==` | Checks if two values are equal | `3 == 3` → True |
50+
| **Not equal to** | `!=` | Checks if two values are not equal | `3 != 2` → True |
51+
| **Greater than** | `>` | Checks if left operand is greater | `5 > 3` → True |
52+
| **Less than** | `<` | Checks if left operand is smaller | `2 < 5` → True |
53+
| **Greater than or equal to** | `>=` | Checks if left operand is greater or equal | `5 >= 5` → True |
54+
| **Less than or equal to** | `<=` | Checks if left operand is smaller or equal | `3 <= 5` → True |
55+
56+
**Example:**
57+
58+
```python
59+
x = 5
60+
y = 3
61+
print(x == y) # False
62+
print(x != y) # True
63+
print(x > y) # True
64+
print(x < y) # False
65+
print(x >= y) # True
66+
print(x <= y) # False
67+
```
68+
69+
---
70+
71+
## Logical Operators
72+
73+
Logical operators combine multiple conditions to return a single boolean result. They’re often used with comparison operators.
74+
75+
| Operator | Symbol | Description | Example |
76+
|:----------|:-------|:------------|:--------|
77+
| **AND** | `and` | True if both conditions are true | `True and False` → False |
78+
| **OR** | `or` | True if at least one condition is true | `True or False` → True |
79+
| **NOT** | `not` | Negates the condition (True to False, vice versa) | `not True` → False |
80+
81+
**Example:**
82+
83+
```python
84+
a = 5
85+
b = 3
86+
c = 8
87+
print(a > b and c > a) # True, both conditions are True
88+
print(a > b or b > c) # True, one condition is True
89+
print(not (a > b)) # False, as a > b is True, but `not` negates it
90+
```
91+
92+
---
93+
94+
## Operator Precedence
95+
96+
Operator precedence determines the order in which operations are performed.
97+
Operators with higher precedence are evaluated first. If operators have the same precedence, they are evaluated from left to right.
98+
99+
### Highest to Lowest Precendence:
100+
101+
1. **Exponentiation:** **
102+
2. **Unary Operators:** +, - (positive, negative)
103+
3. **Multiplication, Division, Modulus, Floor Division:** *, /, %, //
104+
4. **Addition, Subtraction:** +, -
105+
5. **Comparison Operators:** <, <=, >, >=
106+
6. **Equality Operators:** ==, !=
107+
7. **Logical NOT:** not
108+
8. **Logical AND:** and
109+
9. **Logical OR:** or
110+
111+
**Example of operator precendence:**
112+
113+
```python
114+
result = 5 + 3 * 2 ** 2 - 4 / 2
115+
# Step-by-step evaluation:
116+
# 1. 2 ** 2 = 4
117+
# 2. 3 * 4 = 12
118+
# 3. 4 / 2 = 2
119+
# 4. 5 + 12 = 17
120+
# 5. 17 - 2 = 15
121+
print(result) # Output: 15
122+
```
123+
124+
- **Using parentheses to override precedence:** Parentheses `()` can be used to explicitly control the order of evaluation.
125+
126+
```python
127+
result = (5 + 3) * (2 ** 2) - (4 / 2)
128+
print(result) # Output: 30.0
129+
```

Python/Control Flow.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Control Flow
2+
3+
## Table of Contents
4+
5+
- **[Basic if-else Conditions](#basic-if-else-conditions)**
6+
- **[Nested and Multiple Conditions (elif)](#nested-and-multiple-conditions)**
7+
- **[Logical Expressions in Control Flow](#logical-expressions-in-control-flow)**
8+
- **[Operators](#operators)**
9+
- **[Use Case Examples](#use-case-examples)**
10+
- **[Ternary Expressions](#ternary-expressions)**
11+
12+
---
13+
14+
## Basic if-else Conditions
15+
16+
The `if` statement allows you to execute a block of code only if a specified condition is true. The `else` statement provides an alternative block of code if the condition is false.
17+
18+
**Syntax:**
19+
20+
```python
21+
if condition:
22+
# Code to execute if condition is true
23+
else:
24+
# Code to execute if condition is false
25+
```
26+
27+
**Example:**
28+
29+
```python
30+
age = 18
31+
if age >= 18:
32+
print("You are eligible to vote.")
33+
else:
34+
print("You are not eligible to vote.")
35+
```
36+
37+
Here, the code checks if `age` is greater than or equal to 18. If true, it prints a message about eligibility. If false, it provides an alternate message.
38+
39+
---
40+
41+
## Nested and Multiple Conditions
42+
43+
Python allows you to nest `if` statements within each other for more complex conditions or use `elif` (short for “else if”) for multiple conditions.
44+
45+
**Syntax:**
46+
47+
```python
48+
if condition1:
49+
# Code for condition1
50+
elif condition2:
51+
# Code for condition2
52+
else:
53+
# Code if neither condition1 nor condition2 is true
54+
```
55+
56+
**Example of `elif`:**
57+
58+
```python
59+
score = 85
60+
if score >= 90:
61+
print("Grade: A")
62+
elif score >= 80:
63+
print("Grade: B")
64+
elif score >= 70:
65+
print("Grade: C")
66+
else:
67+
print("Grade: D")
68+
```
69+
70+
This code assigns a grade based on `score`. It checks each condition in order, and once a true condition is found, the corresponding block is executed, skipping any remaining conditions.
71+
72+
**Example of nested conditions:**
73+
74+
```python
75+
age = 20
76+
has_permission = True
77+
if age >= 18:
78+
if has_permission:
79+
print("Access granted.")
80+
else:
81+
print("Permission denied.")
82+
else:
83+
print("Access denied due to age.")
84+
```
85+
86+
Here, `has_permission` is checked only if the first condition (`age >= 18`) is true. This allows for handling complex scenarios with dependencies.
87+
88+
---
89+
90+
## Logical Expressions in Control Flow
91+
92+
Logical expressions use operators (`and`, `or`, `not`) to combine multiple conditions in a single `if` statement, enhancing flexibility.
93+
94+
### Operators
95+
96+
- `and`: Evaluates to true if both conditions are true.
97+
- `or`: Evaluates to true if at least one condition is true.
98+
- `not`: Inverts the boolean value of a condition.
99+
100+
### Use Cases Examples
101+
102+
- **Example 1:**
103+
104+
```python
105+
age = 20
106+
has_ID = True
107+
if age >= 18 and has_ID:
108+
print("Allowed entry.")
109+
else:
110+
print("Entry not allowed.")
111+
```
112+
113+
Here, both `age >= 18` and `has_ID` must be true for entry to be allowed.
114+
115+
- **Example 2:**
116+
117+
```python
118+
has_ticket = False
119+
vip_pass = True
120+
if has_ticket or vip_pass:
121+
print("Entry granted.")
122+
else:
123+
print("Entry denied.")
124+
```
125+
126+
This example allows entry if the user has either a ticket or a VIP pass.
127+
128+
- **Example 3:**
129+
130+
```python
131+
is_member = False
132+
if not is_member:
133+
print("Please sign up to become a member.")
134+
```
135+
136+
The `not` operator inverts `is_member`. If `is_member` is `False`, the message is shown.
137+
138+
---
139+
140+
## Ternary Expressions
141+
142+
A ternary expression is a shorthand way to write an `if-else` statement in a single line. It’s useful for simple conditions and assignments.
143+
144+
**Syntax:**
145+
146+
```python
147+
value_if_true if condition else value_if_false
148+
```
149+
150+
**Example:**
151+
152+
```python
153+
age = 17
154+
status = "Adult" if age >= 18 else "Minor"
155+
print(status) # Output: Minor
156+
```
157+
158+
Here, `status` is assigned "Adult" if `age >= 18` is true; otherwise, it’s assigned "Minor". Ternary expressions are concise and useful when assigning values based on conditions.
159+
160+
**Another Example:**
161+
162+
```python
163+
is_even = "Even" if num % 2 == 0 else "Odd"
164+
```
165+
166+
This checks if a number is even or odd, assigning the appropriate string based on the condition `num % 2 == 0`.

0 commit comments

Comments
 (0)