-
파이썬 코딩블록 들여쓰기 하는 방법Dung--[IT] 2023. 4. 6. 14:58
파이썬 코딩블록 들여쓰기 하는 방법
파이썬은 코딩블록으로 들여쓰기를 활용합니다. 이것은 C, C++, Java, C# 과 같은 다수의 언어들이 사용하고 있는 Curly Bracket({...}) 와 다른 스타일 입니다. 파이썬의 가장 큰 특징이며, 편리하고 가독성을 높일 수가 있습니다.
단점으로는 동일한 블럭의 들여쓰기는 모든 동일한 수의 공백을 사용하여야 하는것입니다. 팀 작업을 하실 때도 동일한 들여쓰기 사용을 위하여 규칙을 정하는 것이 좋습니다. 소스를 수정하고자 들여쓰기가 바뀌면 에러가 나며 찾아내기도 쉽지 않습니다.
1. 코딩 블록을 시작하는 문장은 콜론(;) 을 활용합니다. 그리고 내부의 코딩 블록은 동일한 공백을 활용합니다. 일반적으로 들여쓰기는 4개의 공백을 활용하는데 중간에 하나라도 틀리게 되면 바로 에러가 발생하게 됩니다.
2. 그림과 같이 중간에 공백이 들어가게 되는 경우 에러가 발생합니다. IndentationError:unexpected indent 라고 하는 메시지가 표시되는데 파이썬 컴파일러가 올바르게 들여쓰기를 강제하기 위하는 것입니다.
3. 예외적으로는 한 줄짜리 구문을 표현하고자 할때는 다음과 같이 활용할 수 있습니다.
4. 파이썬은 기본적으로 구문 뒤에 아무것도 같다 붙이지 않습니다. C,C++,Java, C# 코딩에 익숙한 사람들에게는 습관적으로 세미콜론(;)을 붙입니다. 파이썬에 사용하여도 크게 문제가 되지 않습니다. 그러나 세미콜론을 꼭 사용해야 되는 경우가 있습니다. 아래 그림과 같이 한줄에 여러 구문을 쓰고자 할때 세미콜론을 사용하여 구분을 합니다.
'Dung--[IT]' 카테고리의 다른 글
윈도우 11, KB5023706 업데이트 진행 오류 SSD 속도 저하 (0) 2023.04.10 인터넷 연결 끊김 증상 원인 및 진단방법 (0) 2023.04.10 추천 무료이미지 다운로드 가능한 사이트 (국내 및 해외) (0) 2023.04.06 이클립스 실행 파라미터의 값 (0) 2023.04.06 이클립스 인텔리전스 간편 기능 옵션 세팅 (0) 2023.04.06