In [1]:
minutes = 105
print(minutes / 60)


1.75

In [2]:
print(minutes // 60)


1

In [3]:
print(minutes % 60)


45

In [4]:
x = 11

if 10 < x < 20:
    print("참 입니다.")


참 입니다.

In [5]:
bool(0)


Out[5]:
False

In [6]:
bool(1)


Out[6]:
True

In [7]:
bool('a')


Out[7]:
True

In [8]:
bool(-1)


Out[8]:
True

In [9]:
bool('asdf')


Out[9]:
True

In [10]:
pprint([1,2,3])


Pretty printing has been turned OFF

In [11]:
a = -5
a is -5


Out[11]:
True

In [12]:
a = -6
a is -6


Out[12]:
False

In [13]:
a = -7
a is -7


Out[13]:
False

In [14]:
a = -5
a is -5


Out[14]:
True

In [15]:
a = -2 
a is -2


Out[15]:
True

In [16]:
-5 is -5


Out[16]:
True

In [17]:
-6 is -6


Out[17]:
False

In [18]:
-5 == -5


Out[18]:
True

In [19]:
-6 == -6


Out[19]:
True

In [20]:
def countdown(n):
    if n <= 0:
        print("발사~")
    else:
        print(n)
        countdown(n-1)
        print(n , "카운트 다운 함수 끝")

In [21]:
countdown(3)


3
2
1
발사~
1 카운트 다운 함수 끝
2 카운트 다운 함수 끝
3 카운트 다운 함수 끝

In [35]:
def func(f, n):
    if n <= 0:
        return None
    f()
    func(f, n - 1)

In [36]:
def t():
    print("a")

In [40]:
func(t, 2)


a
a

In [41]:
bool(None)


Out[41]:
False

In [42]:
0/1


Out[42]:
0.0

In [61]:
def divide(a, b):
    try:
        return a/b
    except ZeroDivisionError as e:
        raise ValueError('Invalid inputs') from e

In [124]:
try:
    result = divide(1, 2)
except ValueError as e:
    print(e)
else:
    print(result)


0.5

In [68]:
-1 is None


Out[68]:
False

In [69]:
import os

In [71]:
os.getpid() #현재 실행중인 파이썬 인터프리터에 대한 pid


Out[71]:
89950

In [72]:
os.getcwd() #현재 작업중인 디렉토리


Out[72]:
'/Users/jaegyuhan/PythonEx_1/토요_파이썬'

In [73]:
os.getuid() #사용자 ID


Out[73]:
501

In [74]:
os.getgid() #그룹 ID


Out[74]:
20

In [75]:
import subprocess

In [76]:
ret = subprocess.getoutput('date')  #쉘에서 프로그램을 실행하여 생성된 결과를 얻고 싶은 경우 gtoutput를 한다. 
print(ret)


2017년 11월 22일 수요일 14시 13분 52초 KST

In [78]:
ret = subprocess.getoutput('date -u | wc')
print(ret)


       1       9      52

In [81]:
ret = subprocess.check_output(['date','-u']) #표준출력으로 바이트 타입을 반환한다. 쉡을 사용하지 않는다.
print(str(ret, 'utf-8'))


2017년 11월 22일 수요일 05시 25분 16초 UTC


In [82]:
ret = subprocess.getstatusoutput('date') #프로그램의 종료 상채를 표시한다. 
print(ret)


(0, '2017년 11월 22일 수요일 14시 32분 22초 KST')

In [87]:
ret = subprocess.getstatusoutput('python test.py') 
print(ret)


(0, 'end!')

In [89]:
ret = subprocess.call(['python','test.py'])  #결과가 아닌 상태코드만 저장하고 싶을때 상태코드가 0이 나오면 정상 
print(ret)


0

In [91]:
ret = subprocess.call("python test.py", shell=True) #위에처럼 명령어를 별도의 문자열로 분할하지않는다.

In [92]:
print(ret)


0

In [93]:
text = input()


asdf

In [94]:
print(text)


asdf

In [95]:
name = input("이름을 입력해 주세요!")


이름을 입력해 주세요!한재규

In [96]:
name


Out[96]:
'한재규'

In [97]:
int(-7)


Out[97]:
-7

In [98]:
None == False


Out[98]:
False

In [99]:
None == True


Out[99]:
False

In [100]:
None is False


Out[100]:
False

In [101]:
None == None


Out[101]:
True

In [102]:
None is None


Out[102]:
True

In [103]:
None == 0


Out[103]:
False

In [104]:
None is 0


Out[104]:
False

In [106]:
def compare(x, y):
    if x > y:
        return 1
    elif x == y:
        return 0
    else:
        return -1

In [107]:
compare(1,1)


Out[107]:
0

In [108]:
compare(1,4)


Out[108]:
-1

In [109]:
compare(4,1)


Out[109]:
1

In [110]:
def distance(x1, y1, x2, y2):
    return 0.0

In [111]:
distance(1,2,4,6)


Out[111]:
0.0

In [115]:
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    print('dx is', dx)
    print('dy is', dy)
    return 0.0

In [116]:
distance(1,2,4,6)


dx is 3
dy is 4
Out[116]:
0.0

In [117]:
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    dsqared = dx**2 + dy**2
    print('dsqared is:', dsqared)
    return 0.0

In [118]:
distance(1,2,4,6)


dsqared is: 25
Out[118]:
0.0

In [122]:
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    dsqared = dx**2 + dy**2
    result = math.sqrt(dsqared)
    return result

In [123]:
distance(1,2,4,6)


Out[123]:
5.0

In [127]:
-5 is -5


Out[127]:
True

In [128]:
-6 is -6


Out[128]:
False

In [129]:
1 and 3


Out[129]:
3

In [130]:
0 and 3


Out[130]:
0

In [132]:
True is 1


Out[132]:
False

In [133]:
1 and True


Out[133]:
True

In [134]:
1 and 3


Out[134]:
3

In [135]:
3 and 1


Out[135]:
1

In [136]:
def is_triangle(x,y,z):
    if x + y < z or y + z < x or x + z < y:
        return False
    else:
        return True

In [137]:
print(is_triangle(1,2,3))


True

In [149]:
print(is_triangle(6,6,12))


True

In [143]:
print(is_triangle(3,5,8))


True

In [153]:
x,y,z = input().split(",")
print(is_triangle(int(x),int(y),int(z)))


6,6,12
True

In [156]:
def ack(m, n):
    if m == 0:
        return n + 1
    if m > 0 and n == 0:
        return ack(m-1, 1)
    if m > 0 and n > 0:
        return ack(m-1, ack(m, n-1))

In [157]:
ack(3,4)


Out[157]:
125

In [163]:
# Python program to find Excel column name from a 
# given column number
 
MAX = 50
 
# Function to print Excel column name for a given column number
def printString(n):
 
    # To store result (Excel column name)
    string = ["\0"]*MAX
 
    # To store current index in str which is result
    i = 0
 
    while n > 0:
        # Find remainder
        rem = n%26
 
        # if remainder is 0, then a 'Z' must be there in output
        if rem == 0:
            string[i] = 'Z'
            i += 1
            n = (n//26)-1
        else:
            string[i] = chr((rem-1) + ord('A'))
            i += 1
            n = n//26
    string[i] = '\0'
 
    # Reverse the string and print result
    string = string[::-1]
    print("".join(string))

In [164]:
printString(1)


A

In [167]:
printString(52)


AZ

In [184]:
def alpha_string46(s):
    #함수를 완성하세요
    
    if (len(s) == 4 or len(s) == 6):
        for i in s:
            if i in '0123456789':
                return True
            else:
                return False
    else:
        return False

In [185]:
# 아래는 테스트로 출력해 보기 위한 코드입니다.
print( alpha_string46("a234"))
print( alpha_string46("1234"))


False
True

In [170]:
a = 'a123'

In [171]:
ord('a')


Out[171]:
97

In [178]:
ord('0')


Out[178]:
48

In [179]:
b = '0123456789'

In [180]:
'4' in b


Out[180]:
True

In [182]:
'a' in b


Out[182]:
False

In [186]:
def alpha_string46(s):
    return s.isdigit() and len(s) in [4, 6]

In [187]:
print( alpha_string46("a234"))
print( alpha_string46("1234"))


False
True

In [188]:
len('gg') in [4,5]


Out[188]:
False

In [189]:
def fibonacci(num):
    if num < 2:
        return 1

    return fibonacci(num - 1) + fibonacci(num - 2)

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print(fibonacci(3))


3

In [190]:
print(fibonacci(4))


5

In [191]:
for i in range(10):
    print(fibonacci(i))


1
1
2
3
5
8
13
21
34
55

In [224]:
def fibo(n):
    s = [0] * (n+1)
    for i in range(1,n+1):
        if i in [1,2]:
            s[i] = 1
        else:
            s[i] = s[i-2] + s[i-1]
    
    return s[n]

In [237]:
for i in range(1,11):
    print(fibo(i))


1
1
2
3
5
8
13
21
34
55

In [229]:
b = [0] * 4

In [230]:
b


Out[230]:
[0, 0, 0, 0]

In [231]:
fibo(1)


Out[231]:
1

In [232]:
fibo(2)


Out[232]:
1

In [233]:
fibo(3)


Out[233]:
2

In [234]:
fibo(4)


Out[234]:
3

In [235]:
fibo(10)


Out[235]:
55

In [238]:
def fibonacci(num):
    a, b = 0, 1
    for i in range(num):
        a, b = b, a+b
    return a

In [239]:
for i in range(1,11):
    print(fibonacci(i))


1
1
2
3
5
8
13
21
34
55

In [240]:
def fibo2(n):
    a = 0
    b = 1
    for i in range(n):
        a = b
        b = a+b
    return a

In [242]:
for i in range(10):
    print(fibo2(i))


0
1
2
4
8
16
32
64
128
256

In [243]:
def sum_digit(number):
    '''number의 각 자릿수를 더해서 return하세요'''
    

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print("결과 : {}".format(sum_digit(123)));


결과 : None

In [244]:
s = '123'

In [247]:
result = 0
for i in s:
    result += int(i)

print(result)


6

In [248]:
ii = 123

In [250]:
123 // 100


Out[250]:
1

In [271]:
def split_digit(num):
    s = []
    
    while num != 0:
        temp = num % 10
        s.append(temp)
        num -= temp
        num = num // 10
    
    return s

In [274]:
split_digit(12302)


Out[274]:
[2, 0, 3, 2, 1]

In [255]:
123 % 10


Out[255]:
3

In [256]:
123 - 3


Out[256]:
120

In [257]:
120 // 10


Out[257]:
12

In [258]:
12 % 10


Out[258]:
2

In [259]:
12 -2


Out[259]:
10

In [260]:
10 // 10


Out[260]:
1

In [261]:
1 % 10


Out[261]:
1

In [262]:
1 - 1


Out[262]:
0

In [263]:
0 // 10


Out[263]:
0

In [275]:
def sum_digit(num):
    if num < 10:
        return num
    return (num % 10) + sum_digit(num // 10)

In [276]:
print(sum_digit(123))


6

In [301]:
def string_middle(str):
    # 함수를 완성하세요
    temp = len(str) // 2
    result = ""
    if len(str) % 2 == 0:
        result = str[temp-1] + str[temp]
    else:
        result = str[temp]
    return result

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print(string_middle("power"))


w

In [299]:
s = 'abcde'

In [296]:
s[1]


Out[296]:
'b'

In [297]:
len(s) // 2


Out[297]:
2

In [302]:
def string_middle(str):
    return str[(len(str)-1)//2:len(str)//2+1]

In [303]:
def average(list):
    # 함수를 완성해서 매개변수 list의 평균값을 return하도록 만들어 보세요.
    l = len(list)
    sum = 0
    for i in list:
        sum += i
    
    return sum / l

# 아래는 테스트로 출력해 보기 위한 코드입니다.
list = [5,3,4] 
print("평균값 : {}".format(average(list)));


평균값 : 4.0

In [306]:
sum([1,2,3]) / len([1,2,3])


Out[306]:
2.0

In [311]:
def is_pair(s):
    # 함수를 완성하세요
    
    cnt = 0 
    if s[0] == ')':
        return False
    for i in s:
        if i == '(':
            cnt += 1
        elif i == ')':
            cnt -= 1
    
    if cnt == 0:
        return True
    else:
        return False


# 아래는 테스트로 출력해 보기 위한 코드입니다.
print( is_pair("(hello)()"))
print( is_pair(")("))


True
False

In [308]:
st = []

In [310]:
cnt = 0
for i in "())":
    if i == '(' and cnt > 0:
        cnt += 1
    elif i == ')' and cnt > 0:
        cnt -= 1

if cnt == 0:
    print("ok")
else:
    print("err")


err

In [322]:
s ="adbcdfb"

In [323]:
s.find("b")


Out[323]:
2

In [316]:
s = "()("

In [317]:
s.find('(') < s.find(')')


Out[317]:
True

In [324]:
def is_pair(s):
    return (s.find("(")<s.find(")") and s.count("(")==s.count(")"))

In [331]:
def quest(target):
    num = int(input("숫자를 입력 하세요 : "))
    
    if target > num:
        print("조금 큰수를 입력하세요!")
        quest(target)
    elif target < num:
        print("조금 작은수를 입력하세요!")
        quest(target)
    else:
        print("참 잘했어요")

quest(20)


숫자를 입력 하세요 : 19
조금 큰수를 입력하세요!
숫자를 입력 하세요 : 20
참 잘했어요

In [332]:
bin(10)


Out[332]:
'0b1010'

In [333]:
bin(3)


Out[333]:
'0b11'

In [334]:
bin(4)


Out[334]:
'0b100'

In [335]:
help(bin)


Help on built-in function bin in module builtins:

bin(number, /)
    Return the binary representation of an integer.
    
    >>> bin(2796202)
    '0b1010101010101010101010'


In [1]:
def binary(num):
    
    result = []
    
    while num >= 2:
        result.append(num % 2)
        num = num // 2
    
    result.append(num)
    
    return "".join(str(x) for x in result[::-1])

In [2]:
print(binary(3))


11

In [356]:
print(binary(8))


1000

In [357]:
print(binary(7))


111

In [ ]: