본문 바로가기

[자기계발]/유튜브로 코딩배우기

<Nomad Coders> 파이썬 1.9~ 1.12

반응형

#1.10 if else and or

def age_check(age):

 print(f"you are {age}")

 if age < 18:

   print ("you cant drink")

 elif age == 18 or age ==19:

   print("you are new to this!")

 elif age > 20 and age < 25:

   print("you are still kind of young")

 else:

   print("enjoy your drink")

 

age_check(29)

 

 

결과:  you are 29

enjoy your drink

 


 

def stock_check(stock):

print(f"your stock price is {stock}")

 

if stock < 20000:

 

print ("you should buy")

 

elif stock == 20000 or stock ==21000:

 

print("you are okay to buy")

 

elif stock > 21000 and stock < 30000:

 

print("bit risky to buy")

 

else:

 

print("time to sell")



stock_check(21100)

 

결과: your stock price is 21100

bit risky to buy


if는 여러개를 쓸 수 있고 이 if문들이 true가 아니면 else문(flase)으로 간다. (if, elif, else) 순

else if = elif 도 조건문이라서 condition을 써줘야하고 flase면 그 다음으로 넘어간다.

 

stackoverflow.com/questions/132988/is-there-a-difference-between-and-is

 

Is there a difference between "==" and "is"?

My Google-fu has failed me. In Python, are the following two tests for equality equivalent? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold ...

stackoverflow.com

'is' 와 '==' 는 같은 용도로 쓰일까?

비슷하지만 다르다.

  • == is for value equality. Use it when you would like to know if two objects have the same value.
  • is is for reference equality. Use it when you would like to know if two references refer to the same object.
  • The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=, except when you’re comparing to None.
  • 'is' checks that they are the same object and == checks that they are the same value.

#1.11 for in

days = ("Mon", "Tue", "Wed", "Thu", "Fri)"

 

for day in days:
 if day is "Wed":

   break

 else:

   print(day).

 

#for 변수 in sequence
'변수'는 for문이 실행될 때 선언된다. 이름이 뭐가 됐든 상관이 없다.

 

#for 문은 하나의 루프를 생성하는 것으로 이해하면 된다.
함수를 실행할 때 설정한 범위내에서 함수를 반복적으로 시행하는 데, 이 때 for를 실행시킬 때 사용된 변수는 계속 바뀐다.

#해당 루프가 실행되던 도중 break함수가 실행될 경우 해당 루프는 즉시 시행 종료된다.
* python을 활용하여 데이터 크롤링 루프를 실행했을 때 원치않는 데이터가 감지되었을 때 즉시 해당 크롤링을 멈추는 명령을 활용할 때에도 활용될 수 있을것이라 예상된다.

 

#1.12 Modules

python 내부에는 활용할 수 있는 수많은 모듈들이 있다.
해당 모듈내에는 다양한 함수들이 있는데 이를 활용하기 위해서는 모듈을 불러와야한다.
*import를 활용하여 모듈을 불러올 수 있다.

import math

 

print(math.ceil(1.2))

print(math.fabs(-1.2))



이 때 한개의 함수를 사용하기 위해 모듈을 불러오는 것은 매우 비효율적인데 그 이유로는 모듈 내에는 수많은 함수들이 있는데 사용하지 않는 함수까지 불러오기 때문이다.고로 from을 써서 필요한 것만 불러오라.
이를 해결하기 위해서 "import 모듈이름.원하는 함수 as 원하는 이름" 이렇게 불러오면 된다.

from math import ceil, fsum

 

print(ceil(1.2))

print(fsum([1, 2, 3, 4, 5, 6, 7]))

 

from math import ceil, fsum as sexy-sum

 

print(ceil(1.2))

print(fsum([1, 2, 3, 4, 5, 6, 7]))



또한, 우리 임의적으로 원하는 이름.py 파일의 def로 만든 함수도 만들어서 불러 올 수 있다.

sung.py 라는 파일을 만들고 안에다 

 

def plus(a, b):

  return a + b 

 

def minus(a, b):

  return a - b

를 입력하고

 

from sung import plus, minus

print(plus(1, 2), minus(1,2))

를 입력하면 3 -1 이 나온다


여러 모듈들

- math — Mathematical functions
[math - Mathematical functions - Python 3.9.0 documentation](https://docs.python.org/3/library/math.html)

- datetime — Basic date and time types : 자주쓰는 모듈
[datetime - Basic date and time types - Python 3.9.0 documentation](https://docs.python.org/3/library/datetime.html)

- CSV File Reading and Writing
[csv - CSV File Reading and Writing - Python 3.9.0 documentation](https://docs.python.org/3/library/csv.html)

- json — JSON encoder and decoder
[json - JSON encoder and decoder - Python 3.9.0 documentation](https://docs.python.org/3/library/json.html)

 

반응형