Conditionals - CS50 Python

この記事は、米ハーバード大学のCS50　Introduction to Programming with Pythonの **[Week 1 Conditionals](https://cs50.harvard.edu/python/2022/weeks/1/)** のメモです。授業の中で、出てきたコードや情報などをまとめました。現在受講中の方、これから受ける方、復習などにお使いください。


**[Lecture 1 - Conditionals YouTube](https://www.youtube.com/watch?v=_b6NgY_pMdw)**
**[Visual Studio Code for CS50](https://cs50.dev/)**

```python
> # greater than
>= # greater than or equal to
< # less than
<= # less than or equal to
== # equal to
!= # not equal to
```

## if
[if - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=167s)

```bash
code compare.py
```

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
	print("x is less than y")
if x > y:
	print("x is greater than y")
if x == y:
	print("x is equal to y")
```

```bash
code compare.py
What's x? 1
What's y? 2
x is less than y
```

[if - google slide](https://docs.google.com/presentation/d/1iagFk7-WnjrWA4fxeHzbYG1MkaO9GYk1DCuRnrFWOQQ/edit#slide=id.gf3bed7358d_0_606)

## elif
[elif - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=596s)

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
	print("x is less than y")
elif x > y:
	print("x is greater than y")
elif x == y:
	print("x is equal to y")
```

[elif - google slide](https://docs.google.com/presentation/d/1iagFk7-WnjrWA4fxeHzbYG1MkaO9GYk1DCuRnrFWOQQ/edit#slide=id.gf3bed7358d_0_610)

## else
[else - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=906s)

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
	print("x is less than y")
elif x > y:
	print("x is greater than y")
else
	print("x is equal to y")
```

[else - google slide](https://docs.google.com/presentation/d/1iagFk7-WnjrWA4fxeHzbYG1MkaO9GYk1DCuRnrFWOQQ/edit#slide=id.gf3bed7358d_0_614)

## or
[or - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=1110s)

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y or x > y:
	print("x is not equal to y")
else:
	print("x is equal to y")
```

[or - google slide](https://docs.google.com/presentation/d/1iagFk7-WnjrWA4fxeHzbYG1MkaO9GYk1DCuRnrFWOQQ/edit#slide=id.gf3bed7358d_0_254)

## Not Equal
[Not Equal - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=1326s)

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x != y:
	print("x is not equal to y")
else:
	print("x is equal to y")
```

```python
x = int(input("What's x? "))
y = int(input("What's y? "))

if x == y:
	print("x is equal to y")
else:
	print("x is not equal to y")
```

[Not Equal - google slide](https://docs.google.com/presentation/d/1iagFk7-WnjrWA4fxeHzbYG1MkaO9GYk1DCuRnrFWOQQ/edit#slide=id.gf3bed7358d_0_269)

## and
[and - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=1513s)

```bash
code grade.py
```

```python
score = int(input("Score: "))

if score >= 90 and score <= 100:
	print("Greade: A")
elif score >= 80 and score < 90:
	print("Greade: B")
elif score >= 70 and score < 80:
	print("Greade: C")
elif score >= 60 and score < 70:
	print("Greade: D")
else:
	print("Greade: F")
```

```python
score = int(input("Score: "))

if 90 <= score and score <= 100:
	print("Greade: A")
elif 80 <= score and score < 90:
	print("Greade: B")
elif 70 <= score and score < 80:
	print("Greade: C")
elif 60 <= score and score < 70:
	print("Greade: D")
else:
	print("Greade: F")
```

## Chaining Comparison Operators
[Chaining Comparison Operators - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=1728s)

```python
score = int(input("Score: "))

if 90 <= score <= 100:
	print("Greade: A")
elif 80 <= score < 90:
	print("Greade: B")
elif 70 <= score < 80:
	print("Greade: C")
elif 60 <= score < 70:
	print("Greade: D")
else:
	print("Greade: F")
```

```python
score = int(input("Score: "))

if score >= 90:
	print("Greade: A")
elif score >= 80:
	print("Greade: B")
elif score >= 70:
	print("Greade: C")
elif score >= 60:
	print("Greade: D")
else:
	print("Greade: F")
```

## Modulo
[Modulo - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=2056s)

```bash
code parity.py
```

```python
+
-
*
/
%
```

```python
x = int(input("What's x?"))

if x % 2 == 0:
	print("Even")
else:
	print("Odd")
```

```bash
python parity.py
```
## Boolean
[Boolean - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=2400s)

```python
def main():
	x = int(input("What's x?"))
	if is_even(x):
		print("Even")
	else:
		print("Odd")

def is_even(n):
	if n % 2 == 0:
		return True
	else:
		return False

main()
```

## Pythonic Expressions
[Pythonic Expressions - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=2650s)

```python
def main():
	x = int(input("What's x?"))
	if is_even(x):
		print("Even")
	else:
		print("Odd")

def is_even(n):
	return True if n % 2 == 0 else False

main()
```

```python
def main():
	x = int(input("What's x?"))
	if is_even(x):
		print("Even")
	else:
		print("Odd")

def is_even(n):
	return n % 2 == 0

main()
```

## match
[match - youtube](https://www.youtube.com/watch?v=_b6NgY_pMdw&t=2895s)

```bash
code house.py
```

```python
name = input("What's your name? ")

if name == "Harry":
	print("Gryffindor")
elif name == "Hermione":
	print("Gryffindor")
elif name == "Ron":
	print("Gryffindor")
elif name == "Draco":
	print("Slytherin")
else:
	print("Who?")
```

```bash
python house.py
What's your name? Harry
Gryffindor
```

```python
name = input("What's your name? ")

if name == "Harry" or name = "Hermione" or name == "Ron":
	print("Gryffindor")
elif name == "Draco":
	print("Slytherin")
else:
	print("Who?")
```

```python
name = input("What's your name? ")

match name:
	case "Harry":
		print("Gryffindor")
	case "Hermione":
		print("Gryffindor")
	case "Ron":
		print("Gryffindor")
	case "Draco":
		print("slytherin")
	case _:
		print("Who?")
```

```python
name = input("What's your name? ")

match name:
	case "Harry" | "Hermione" | "Ron":
		print("Gryffindor")
	case "Draco":
		print("slytherin")
	case _:
		print("Who?")
```



---

* YouTube
    
    * [Week 1 Conditionals](https://www.youtube.com/watch?v=_b6NgY_pMdw)
        
    * [Visual Studio Code for CS50](https://www.youtube.com/watch?v=Oi1lvJS4uS8)
        
* [cs50.dev](https://cs50.dev/)
    
* [Problem set 1](https://cs50.harvard.edu/python/2022/psets/1/)
    
    * [Deep Thought](https://cs50.harvard.edu/python/2022/psets/1/deep/)
        
    * [Home Fe](https://cs50.harvard.edu/python/2022/psets/1/deep/)[deral](https://cs50.harvard.edu/python/2022/psets/1/bank/) [Savings Ban](https://cs50.harvard.edu/python/2022/psets/1/deep/)[k](https://cs50.harvard.edu/python/2022/psets/1/bank/)
        
    * [File E](https://cs50.harvard.edu/python/2022/psets/1/bank/)[xt](https://cs50.harvard.edu/python/2022/psets/1/extensions/)[ensi](https://cs50.harvard.edu/python/2022/psets/1/bank/)[ons](https://cs50.harvard.edu/python/2022/psets/1/deep/)
        
    * [Math](https://cs50.harvard.edu/python/2022/psets/1/deep/) [Interpr](https://cs50.harvard.edu/python/2022/psets/1/extensions/)[eter](https://cs50.harvard.edu/python/2022/psets/1/deep/)
        
    * [Meal](https://cs50.harvard.edu/python/2022/psets/1/deep/) [Time](https://cs50.harvard.edu/python/2022/psets/1/bank/)
        
* [Notes Lecture 1](https://cs50.harvard.edu/python/2022/notes/1/)