TIL
[TIL] 6일차_질문답변상자 만들기
Molybdenum_j
2023. 2. 28. 15:50
# 1. 답변 리스트 생성 : answers = []
answers = ['안돼.','돼.','좋아.','나빠]
# 2. 질문 받기 : question = input()
question = input('고민을 말해보세요.')
# 3. 답변 리스트의 갯수 만큼의 범위에서 랜덤한 정수 1개 생성 : random.randint()
idx = random.randint(0, len(answers) - 1)
# 4. 랜덤한 정수에 해당하는 답변 리스트의 데이터 출력 : answers[random_number]
answers[idx]
- 랜덤한 숫자생성
import random : list에서 random하게 불러온다.
import random
data = random.randint(0, 2)
data
1
- 데이터 입력받기 : input()
input은 입력!
print는 출력!
# 데이터 입력 받기 : input()
data = input('insert data :')
data
insert data :python
'python'
★ 실수할만한 코드 = 얕은복사, 깊은복사★
data1 = [1, 2, 3]
data2 = data1
print(data1, data2)
data2[1] = 4
print(data1, data2)
[1, 2, 3] [1, 2, 3]
[1, 4, 3] [1, 4, 3]
data1 = [1, 2, 3]
data2 = data1 # 얕은복사 : 주소값 복사
data3 = data1.copy() # 깊은 복사 : 데이터 복사
print(data1, data2,data3)
data1[1] = 4
print(data1, data2, data3)
[1, 2, 3] [1, 2, 3] [1, 2, 3]
[1, 4, 3] [1, 4, 3] [1, 2, 3]
- id() : 식별자의 주소값 출력
▶ 문자열 데이터 표현
d1,d2 = "python1",'python2'
d3, d4 = "jupyter's notebook", 'jupyter "note" book'
d5 = 'jupyter\'s "note" book' # \ : 이스케이핑 문자
print(d3, d4, d5)
jupyter's notebook jupyter "note" book jupyter's "note" book
- 멀티라인 문자열
d6 = '''
jupyter
notebook
'''
print(d6)
jupyter
notebook
- 변수의 데이터를 문자열에 삽입
ex) 몇 경기를 뛰었나요?
data1, data2 = '10','2'
string = '저는'+ data1 +'경기 중에'+ data2 + '경기를 뛰었습니다.'
string
'저는 10 경기 중에 2 경기를 뛰었습니다.'
string = '저는 {}경기 중에 {}경기를 뛰었습니다.'.format(data1, data2) <- format()함수가 중괄호 부분을 대입 시켜주는 기능을 함
string
'저는 10경기 중에 2경기를 뛰었습니다.'
string = f'저는 {data1}경기 중에 {data2}경기를 뛰었습니다.'
string
'저는 10경기 중에 2경기를 뛰었습니다.'
▶ PEP 8 - 파이썬스러운 코드를 만들 수 있도록 도와주는 가이드
PEP 8 – Style Guide for Python Code | peps.python.org
PEP 8 – Style Guide for Python Code Author: Guido van Rossum , Barry Warsaw , Nick Coghlan Status: Active Type: Process Created: 05-Jul-2001 Post-History: 05-Jul-2001, 01-Aug-2013 Table of Contents This document gives coding conventions for the Python co
peps.python.org
출처 - 멋쟁이사자처럼_AISCHOOL_박두진강사님