본문 바로가기

프로그램 개발/Python

파이썬(Python) 기본 문법 3 탈출문자 자료구조

728x90
반응형
#[탈출문자]-------------------------------------------
# \n : 줄바꿈 
print("줄바꿈을 합니다.\n 다음 문장")

#\" \' 특수문자 사용 큰/작은 따옴표 
print("줄바꿈을 합니다.\"따옴표\" 표시")
print("줄바꿈을 합니다.\'따옴표\' 표시")

#\r : 커서를 맨 앞으로
#\t : 탭키
#\b : 백스페이스 

# 자료구조 --------------------------------------------
#[리스트] 순서를 가진 집합 
aList = [123]
print(aList)

aFoodList = ["라면","쫄면","국수"]
print(aFoodList)

#메뉴에 라면 찾기 
print(aFoodList.index("라면"))

#메뉴에 짜장면 추가 
aFoodList.append("짜장면")
print(aFoodList)

#쫄면앞에 콩국수 추가 
aFoodList.insert(1,"콩국수")
print(aFoodList)

#주문한 메뉴는 뒤에서 부터 제외 시킴 
print(aFoodList.pop())
print(aFoodList)

#갯수 세기 
print(aFoodList.count("라면"))
#정렬 sort ()
#순서 뒤집기 reverse()
#값 삭제 clear()
#합치기 extend(대상)

#[사전] ------------------------------------
# {key 값 , 내용}
dictionary1 = {1:"사람"2:"동물"6:"물고기"
print(dictionary1)

print(dictionary1.get(1))
#동일한 방법 
print(dictionary1[1])
#가져오기 예외처리 
print(dictionary1.get(3,"비어있음"))
#있는지 유무 
print1 in dictionary1) #True
print3 in dictionary1) #False 

#키 값을 문자로 
dictionary2 = {"AA1":"사람""AA2":"동물""AA4":"물고기"
print(dictionary2)

print(dictionary2["AA1"])

#삭제하기 
del dictionary2["AA1"]
print(dictionary2)

#키값만 출력 
print(dictionary2.keys())

#키와 값을 묶어서 출력 
print(dictionary2.items())

#클리어 
dictionary2.clear()
print(dictionary2)

#[튜플] ------------------------------------
#리스트보다 속도가 빠름 
#내용이 변경되지 않을경우 사용함 

#리스트 list
aFoodList = ["라면","쫄면","국수"]
#튜플 tuple
bFoodList = ("라면","쫄면","국수")

print(bFoodList)
print(bFoodList[2])

(menu, cost) = ("라면"5000)
print(menu, cost)


[결과]
바꿈을 합니다. 다음 문장 줄바꿈을 합니다."따옴표" 표시 줄바꿈을 합니다.'따옴표' 표시 [1, 2, 3] ['라면', '쫄면', '국수'] 0 ['라면', '쫄면', '국수', '짜장면'] ['라면', '콩국수', '쫄면', '국수', '짜장 면'] 짜장면 ['라면', '콩국수', '쫄면', '국수'] 1 {1: '사람', 2: '동물', 6: '물고기'} 사람 사람 비어있음 True False {'AA1': '사람', 'AA2': '동물', 'AA4': ' 물고기'} 사람 {'AA2': '동물', 'AA4': '물고기'} dict_keys(['AA2', 'AA4']) dict_items([('AA2', '동물'), ('AA4', '물 고기')]) {} ('라면', '쫄면', '국수') 국수 라면 5000


반응형