파이썬 - 2일차

2020. 1. 22. 18:26카테고리 없음

index -> 위치 찾아주는 것

 

(정리)

(1) 튜플(tuple) 데이터 타입의 특성

[ ] 인덱싱(indexing)

[ ] 슬라이싱(slicing)

[ ] 연결연산자(+)

[ ] 반복연산자(*)

[x] 리스트(list)의 요소를 직접 바꿀 수 없다. (예: a[0] = 1)

[ ] 리스트(list)의 요소를 중복될 수 있다. (예: a = ('a', 'b', 'a'))

(2) 대표적인 tuple 관련 함수들

 index( )

tuple.index(value)

 

list 

dict -> 인덱스가 없다, 키 값이 중복될 수 없다.

 

list => list1[0] = 'k'   -> 0 => index number

list1의 0번 index 

dict => dict1[0] = 'k'   ->   0=> key

 

a = {'name': 'baik', 'phone': '01088156754', 'birth': '0710'}

for k, v in a.items():
    # print(k, v)
    if v == '0710':
        print(k)

results = [k for k, v in a.items() if v =='0710']
print(results)

results = ''.join([k for k, v in a.items() if v =='0710'])
print(results) 

 

a = {'one':1, 'two':2, 'three':3, 'four':4, 'five':4}

"""
results = []
for k, v in a.items():
    # print(k, v)
    if v == 4:
        # print(k)
        results.append(k)
print(results)
"""

results = [k for k,v in a.items() if v == 4]
print(results)

 

encbook = {'A': 'F', 'B': 'G', 'C': 'H', 'D': 'I', 'E': 'J', 'F': 'K', 'G': 'L',
           'H': 'M', 'I': 'N', 'J': 'O', 'K': 'P', 'L': 'Q', 'M': 'R', 'N': 'S',
           'O': 'T', 'P': 'U', 'Q': 'V', 'R': 'W', 'S': 'X', 'T': 'Y', 'U': 'Z',
           'V': 'A', 'W': 'B', 'X': 'C', 'Y': 'D', 'Z': 'E'}

decbook = {}
for k, v in encbook.items():
    # print(k, v)
    decbook[v] = k
print(decbook)

 

encbook = {'A': 'F', 'B': 'G', 'C': 'H', 'D': 'I', 'E': 'J', 'F': 'K', 'G': 'L',
           'H': 'M', 'I': 'N', 'J': 'O', 'K': 'P', 'L': 'Q', 'M': 'R', 'N': 'S',
           'O': 'T', 'P': 'U', 'Q': 'V', 'R': 'W', 'S': 'X', 'T': 'Y', 'U': 'Z',
           'V': 'A', 'W': 'B', 'X': 'C', 'Y': 'D', 'Z': 'E'}
"""
decbook = {}
for k, v in encbook.items():
    # print(k, v)
    decbook[v] = k
print(decbook)
"""

decbook = {v: k for k, v in encbook.items()}
print(decbook)

icecreams = {'Melona': [1000, 10],
             'Pollapo': [1200, 7],
             'Ppangppare': [1800, 6],
             'Tankboy': [1200, 5],
             'Heathunting': [1200, 4],
             'Worldcorn': [1500, 3]}

for name, box in icecreams.items():
    # print(name, box[1])
    if box[1] == 7:
        print(name)

for name, box in icecreams.items():
    # print(name, box[1])
    if box[0] == 1200:
        print(name, box[1])

print(icecreams['Worldcorn'][1])

print(chr(0x41))
print(ord('a'))

a = 10
print(id(a)) # a의 주소값

 

d = {'one': 1, 'two': 2}

if 'one' in d:
    print(d['one'])

 

"""
print(chr(0x41))
print(ord('a'))

a = 10
print(id(a)) # a의 주소값


d = {'one': 1, 'two': 2}

if 'one' not in d:
    print('fail')
"""
answer = None

if answer == None:
    pass

 

import socket

s = socket.socket()
s.connect(('192.168.10.200', 21))
ans = s.recv(1024)
# print(ans)

if 'vsFTPd 3.0.2' in str(ans):
    print('[ WARN ] vulnerable')
else:
    print('[  OK  ] not vulnerable')

 

"""
results = []
for i in filenames:
    extension = i.split('.')[-1]
    # print(i.split('.')[-1])
    if extension == 'h' or extension == 'c':
        # print(i)
        results.append(i)
"""

#!/usr/bin/python3

def check_extension_and_results(filenames, ext):
    # input : list(filenames), str(ext)
    # output : list(results)
    # function :
    #   * filenames 리스트를 입력 받아서,
    #   * 확장자가 .h or .c로 되어 있는 파일의 목록을
    #   * results 리스트에 저장한다.
    """
    results = []
    for i in filenames:
        extension = i.split('.')[-1]
        if extension == 'h' or extension == 'c':
            results.append(i)
    """

    return [i for i in filenames if i.split('.')[-1] == ext]

def main():
    filenames = ['intra.h', 'intra.c', 'input.txt', 'run.py']
    results = check_extension_and_results(filenames)
    print(results)

if __name__ == '__main__':
    main()

 

def check_even_or_odd(int_num):
    # input : int(num)
    # output : print("even or odd")
    # function :
    #   * num 숫자를 입력 받아서
    #   * 짝수인지 홀수인지 판단하여
    #   * 짝수면 "even number", 홀수면 "odd number"
    if int_num % 2 == 0:
        print("even number")
    else:
        print("odd number")

num1 = input("Input num1 : ")
num2 = input("Input num2 : ")

int_num1 = int(num1)
int_num2 = int(num2)


if int_num1 >= int_num2:
    print(int_num1)
    check_even_or_odd(int_num1)

else:
    print(int_num2)
    """
    if int_num2 % 2 == 0:
        print("even number")
    else:
        print("odd number")
    """
    check_even_or_odd(int_num2)

 

def check_even_or_odd(int_num):
    # input : int(num)
    # output : print("even or odd")
    # function :
    #   * num 숫자를 입력 받아서
    #   * 짝수인지 홀수인지 판단하여
    #   * 짝수면 "even number", 홀수면 "odd number"
    if int_num % 2 == 0:
        print("even number")
    else:
        print("odd number")

def before_greate_and_after(int_num1, int_num2):
    # input : int(int_num1), int(int_num2)
    # output : True|False
    # function :
    #   * 숫자 2개를 입력 받아서
    #   * 앞의 숫자가 크면 True, 뒤에 숫자가 크면 False
    if int_num1 >= int_num2:
        return True
    else:
        return False


num1 = input("Input num1 : ")
num2 = input("Input num2 : ")

int_num1 = int(num1)
int_num2 = int(num2)


if before_greate_and_after(int_num1, int_num2):
    print(int_num1)
    check_even_or_odd(int_num1)

else:
    print(int_num2)
    """
    if int_num2 % 2 == 0:
        print("even number")
    else:
        print("odd number")
    """
    check_even_or_odd(int_num2)

 

input_score = input("score : ")
# print(score)
score = int(input_score)

if score >= 81:
    print('grade is A')
elif score >= 61:
    print('grade is B')
elif score >= 41:
    print('grade is C')
elif score >= 21:
    print('grade is D')
elif score >= 0:
    print('grade is E')
else:
    pass

 

marks = [90, 25, 67, 25, 80]

for index, mark in enumerate(marks):
    # print(index, mark)
    if mark >= 60:
        print("%d is fail" % (index + 1))
    elif 0 <= mark < 60:
        print("%d is true" % (index + 1))
    else:
        sys.exit("Error: Wrong marks")

 

import sys

marks = [90, 25, 67, 25, 80]

index = 1
for mark in marks:
    # print(mark)
    if mark >= 60:
        print("%d number student: pass")
    elif 0 <= mark < 60:
        print("%d number student: nonpass")
    else:
        sys.exit(2)
    index += 1

 

import sys

marks = [90, 25, 67, 25, 80]

index = 0
for mark in marks:
    index += 1
    if mark < 60:
        continue
    print("%d number student: pass" % index)

 

index = 5

while index:
    a = int(input("돈을 넣어 주세요: "))
    if a >= 300:
        if a == 300:
            print("커피를 줍니다.")
            index -= 1
        else:
            a -= 300
            print("거스름돈 %d를 주고 커피를 줍니다." % a)
            index -= 1
print("커피가 다 떨어졌습니다. 판매를 중지 합니다.")

 

def make_book():
    # input : ?
    # output : dict(encbook), dict(decbook)
    # function :
    encbook = {'a': '#', 'p': 's', 'l': '?', 'e': 'k'}
    decbook = {}

    for k, v in encbook.items():
        # print(k, v)
        decbook[v] = k
    # print(decbook)

    return encbook, decbook


def encrypt_str(msgs, encbook):
    # input : str(msgs), dict(encbook)
    # output :
    # function :
    enc_list = []
    for i in msgs:
        # print(encbook[i])
        enc_list.append(encbook[i])
    enc_msgs = ''.join(enc_list)
    return enc_msgs


def decrypt_str(encrypted_msgs, decbook):
    # input : str(encrypted_msgs), dict(decbook)
    # output : str(plaintext)
    # function :
    plaintext = ''
    for i in encrypted_msgs:
        # print(decbook[i])
        plaintext += decbook[i]
    # print(plaintext)

    return plaintext


def main():
    plaintext = 'apple'
    enc_table, dec_table = make_book()
    # print(enc_table)
    # print(dec_table)
    encrypted_msgs = encrypt_str(plaintext, enc_table)
    decrypted_msgs = decrypt_str(encrypted_msgs, dec_table)

    print("Original : |%s|" % plaintext)
    print("Encrypted : |%s|" % encrypted_msgs)
    print("Decrypted : |%s|" % decrypted_msgs)
    
    
if __name__ == '__main__':
    main()