Written by: Elisardo

Edited by: Sooin

Week3

Recap

Today we will learn

Answers


3.1) if & elif

<aside> ❓ if and elifs are used to give condition to your code. It allows to give two or more condition so python goes and check which condition matches your data.

Ex) Let’s say you want to check if a number is even or not. That in python can be written as below:

a = 19 
if a % 2 == 0 :
  print("even")
else:
  print("false")

b = 20
if b % 2 ==0 :
  print("even")
else:
  print("false")

The logic of this code can be interpreted as follows:

<aside> 📌 Indentation & Colon

When using if and else it is important that you wrap the sub-functions to be executed under one condition with the same indentations. So for example Also you must not forget the : after the “condition” and else

if condion: 
	sub-function 1
	sub-function 2
else:
	sub-function 1
	sub-function 2

</aside>

elif

africa = ["nairobi", "kinshasa"]
asia = ["hong-kong", "minato"]
LAC = ["mexico city", "lima"]

joo = "seoul"
kisung = "minato"
joohyung = "mexico city"

if joo in africa:
	print("african")
elif joo in asia:
	print("asian")
elif joo in LAC:
	print("LAC")
else:
	pass #pass is used to pass the function without any returns

3.1.1) Comparatives in If function.

You can use comparatives to compare the data given with the condition set.

Comparatives use
a < b
a < = b a less than b
a is less or equal to b
a > b
a > = b a greater than b
a is greater or equal to b
a == b a is equal to b
a ! = b a not equal to b

3.1.2) and, or, not in if function

a or b if a or b is true returns true
a and b both a and b must be true to return true
not a if x is false it returns true

3.1.3) in, not in (in if function)

| a in list

a not in list
a in tuple
a not in tuple
a in string
a not in string
##list 
a = 2
li = [1,2,3,4,5]
a in li 
##tuple
b = 3
tu = (1,2,4,5,6)
b not in tu
##string
c = 'j'
name="kisung"
c in name
soo = 22
soo_status = "student"
eli = 30
eli_status = "student"

## 1) example or
if soo <= 24 or soo_status == "student":
	print("student")
else:
	print("not a student")

## 2) example or (ver.2)
if eli <= 24 or eli_status == "student":
	print("student")
else:
	print("not a student")

## 3) example and
if soo <= 24 and soo_status == "student":
	print("student")
else:
	print("not a student")

## 3) example and (ver.2)
if eli <= 24 and eli_status == "student":
	print("student")
else:
	print("not a student")

3.2 for loop

Basic for loop: