목록프로그래밍 언어/Python (143)
RUBY
in, not in 키워드 in, not in 키워드를 이용하면 아이템의 존재 유/무를 알 수 있다. studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은') searchName = input('학생 이름 입력: ') if searchName in studentsTuple: print('{}학생은 우리반 학생입니다.'.format(searchName)) else: print('{}학생은 우리반 학생이 아닙니다.'.format(searchName)) 학생 이름 입력: 김지은 김지은학생은 우리반 학생입니다. 학생 이름 입력: 김보람 김보람학생은 우리반 학생이 아닙니다. in, not in 키워드는 문자열에서도 사용 가능하다. pythonStr = '안녕하세요 저는 김지은입..

튜플(Tuple) 인덱스 튜플도 리스트와 마찬가지로 아이템에 자동으로 부여되는 번호표가 있다. 튜플 아이템은 인덱스를 이용해서 조회 가능하다. students = ('홍길동', '박찬호', '이용규', '박승철', '김지은') print('students[0]: {}'.format(students[0])) print('students[1]: {}'.format(students[1])) print('students[2]: {}'.format(students[2])) print('students[3]: {}'.format(students[3])) print('students[4]: {}'.format(students[4])) students[0]: 홍길동 students[1]: 박찬호 students[2]:..

튜플(Tuple) 리스트(List)와 비슷하지만 아이템 변경이 불가하다. ‘()’를 이용해서 선언하고, 데이터 구분은 ‘,’를 이용한다. students = ('홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은') numbers = (10, 20, 30, 40, 50, 60) strs = (3.14, '십', 20, 'one', '3.141592') 숫자, 문자(열), 논리형 등 모든 기본 데이터를 같이 저장할 수 있다. datas = (10, 20, 30, (40, 50, 60)) 튜플에 또 다른 컨테이너 자료형 데이터를 저장할 수도 있다.

리스트 곱셈연산 리스트를 곱셈 연산하면 아이템이 반복된다. index() 함수 index(item) 함수를 item의 인덱스를 알아낼 수 있다. 1부터 10까지의 정수가 중복되지 않고 섞여 있을 때 행운의 숫자 7의 위치를 찾자! import random sampleList = random.sample(range(1, 11), 10) selectIdx = int(input('숫자 7의 위치 입력: ')) searchIdx = sampleList.index(7) if searchIdx == selectIdx: print('정답!') else: print('틀렸어요ㅠㅠ') print('sampleList: {}'.format(sampleList)) print('searchIdx: {}'.format(sear..

리스트 슬라이싱[n:m] [n:m]을 이용하면 리스트에서 원하는 아이템만 뽑아낼 수 있다. [n:m]을 이용하면 문자열도 슬라이싱이 가능하다. str = 'abcdefghijklmnopqrstuvwxyz' print('str length: {}'.format(len(str))) print('str: {}, str length: {}'.format(str, len(str))) print('str: {}, str length: {}'.format(str[2:4], len(str))) print('str: {}, str length: {}'.format(str[:4], len(str))) print('str: {}, str length: {}'.format(str[2:], len(str))) print('st..

reverse() 함수 reverse() 함수를 이용하면 아이템을 순서를 뒤집을 수 있다. 다음은 전쟁에서 사용되는 암호이다. 암호를 해독하는 프로그램을 만들어보자. * 먼저 암호의 규칙을 알아보자 → 2 * 7 = 14 → 1 * 5 = 5 → 6 * 2 = 12 → 3 * 1 = 3 즉, 14는 2 * 7 5는 1 * 5 12는 6* 2 3은 3* 1 뒤부터 적어나가면 13326125157214의 순으로 적을 수 있다. secret = '27156231' secretList = [] solvedList = '' for cha in secret: secretList.append(int(cha)) secretList.reverse() val = secretList[0] * secretList[1] sec..

sort() 함수 sort() 함수를 이용하면 아이템을 정렬할 수 있다. 아래 점수표에서 최저 및 최고 점수를 삭제한 후 총점과 평균을 출력해 보자 playerScore = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0] print('playerScore: {}'.format(playerScore)) maxScore = max(playerScore) minScore = min(playerScore) def minS(playerScore): return list(filter(lambda x: x > min(playerScore), playerScore)) minScoreIdx = len(minS(playerScore)) -1 print("playerScore: {}".format(minS(playe..

extend() 함수 extend() 함수를 이용하면 리스트에 또 다른 리스트를 연결(확장)할 수 있다. 덧셈 연산자를 이용해서 리스트를 연결할 수도 있다. 나와 친구가 좋아는 번호를 합치되 번호가 중복되지 않게 하는 프로그램을 만들자 myFavoriteNumbers = [1, 3, 5, 6, 7] friendFavoriteNumbers = [2, 3, 5, 8, 10] print('myFavoriteNumbers: {}'.format(myFavoriteNumbers)) print('friendFavoriteNumbers: {}'.format(friendFavoriteNumbers)) addList = myFavoriteNumbers + friendFavoriteNumbers print('addList ..