[ Python ] cx_Oracle을 이용한 oracle 연결 Installing oracle module pip install cx_Oracle Example import cx_Oracle conn = cx_Oracle.connect('userid','password','host/sid') cursor = conn.cursor() cursor.execute(' ') rows = cursor.fetchall() print(row) cursor.close() conn.close() Python/Python Programming 2022.06.27
[Python] 줄 바꿈(\n) 을 포함 입력 x = "" while True: y = input('[input]') if y: x += y +"\n" else: break print(x) [input]Python [input]is [input]Beautiful [input] Python is Beautiful Python/Python Programming 2020.11.28
[Python] Celsius temperature To Fahrenheit's temperature Basic Function (섭씨, 화씨) 섭씨와 화씨를 계산하는 간단한 함수 # Celsius temperature To Fahrenheit's temperature # 섭씨를 화씨 def c2f(degree): return round((9/5) * degree + 32,2) print(c2f(40)) 104.0 # Fahrenheit's temperature To Celsius temperature # 화씨를 섭씨 def f2c(degree): return round((5/9) * (degree - 32),2) print(f2c(120.0)) 48.89 Python/Python Programming 2020.11.22
[Python] 할당된 변수들을 반복문으로 실행하기. (eval 함수이용) eval(문자열) : 문자열을 실행 이미할당된 반복가능한 형태의 변수들을 실행 test0 = 1 test1 = 2 test2 = 3 test3 = 4 test4 = 5 for i in range(5): print(eval('test'+str(i))) 1 2 3 4 5 test0 = 1 test1 = 2 test2 = 3 test3 = 4 test4 = 5 total = 0 for i in range(5): total += eval('test'+str(i)) total 15 Python/Python Programming 2020.11.02
[Python] string 모듈을 이용한 임의의 패스워드 만들기 from random import choice from string import punctuation from string import ascii_letters from string import digits password_digit = ''.join(choice(digits) for i in range(3)) # 임의 수만큼 숫자 생성 password_alpha = ''.join(choice(ascii_letters) for i in range(3)) # 임의 수만큼 영대소문자 생성 password_symbols = ''.join(choice(punctuation) for i in range(2)) # 임의 수만큼 특수문자 생성 print(password_digit+password_alpha+passw.. Python/Python Programming 2020.10.16
[Python] Python Dictionary의 clear 메소드와 { } 차이 딕셔너리의 clear 메소드와 { }는 객체를 초기화 한다는 것은 동일하지만, 재사용에서 활용이 다를 수 있다. dic = {'a':1} dic2 = dic print(dic) print(dic2) {'a': 1} {'a': 1} dic 객체를 { }로 클리어 했을 때, dic = {} print(dic) print(dic2) {} {'a': 1} dic = { } 는 빈 딕셔너리 객체를 새로 생성하고, dic2는 기존 객체를 유지한다. dic 객체를 clear() 메소드로 클리어 했을 때, dic.clear() print(dic) print(dic2) {} {} 하지만 clear는 참조되어 있는 모든 객체를 삭제한다. dic2.clear() print(dic) print(dic2) {} {} dic2를.. Python/Python Programming 2020.08.03
Jupyter notebook 이미지 삽입하는 방법 !pip install IPython from IPython.display import Image Image("pydole.jpg") # 이미지명 Python/Python Programming 2020.07.22
[Python] for문에 리스트 순회시 remove가 정상적으로 반영되지 않는 이유 리스트 원본 자체를 loop에서 remove 정상적으로 반영되지 않기 때문에, 스텝 슬라이싱을 이용하면 된다. # lst 원본 자체를 for문 lst = [ i for i in range(10) ] for i in lst: lst.remove(i) lst [1, 3, 5, 7, 9] # 원하는 결과가 나오지 않았다. 스텝 슬라이싱으로 for문 lst = [ i for i in range(10) ] for i in lst[::]: lst.remove(i) lst [] docs.python.org/3/tutorial/controlflow.html#for-statements Python/Python Programming 2020.05.18
[Python] PC의 호스트네임(hostname)과 MAC Address 얻기 PC의 호스트네임(hostname) 얻기 import socket print(socket.gethostname()) PC-AA PC의 MAC Address 얻기 import psutil nics = psutil.net_if_addrs() print(nics['이더넷'][0][1]) xx-xx-xx-xx-xx-xx Python/Python Programming 2020.05.14
[Python] random 함수 random.choice : 문자열, 리스트, 튜플과 같은 순서가 있는 반복개체에서 임의 요소를 리턴 # list A = [1,2,3,4,5,6,7,8] print(random.choice(A)) 4 # Strings A = '12345678' print(random.choice(A)) 3 # Tuple A = (1,2,3,4,5,6,7,8) print(random.choice(A)) 6 random.shuffle : 순서 섞기 from random import shuffle lst = [1,2,3] shuffle(lst) lst [3, 1, 2] Python/Python Programming 2020.05.13