파이썬 기본 문법

2023. 1. 25. 18:35프로젝트 공부/파이썬 입문

반응형

1. 변수 선언 시 따로 명령어가 필요없다. 

   - 쌍반점( ; ) 또한 Python에서는 권장하지 않는다

// Javascript 변수 선언 시
const message = "Hello World!";
let messageType = "text";

// Python 변수 선언 시
message = "Hello World!"
messageType = "text"

 

2. Boolean 타입 선언 시 앞글자는 대문자로 써줘야 한다.

# 잘못된 예시
isError = true
isError = false

# 올바른 예시
isError = True
isError = False

 

3. Function 타입 선언 시 중괄호( '{' ) 는 사용하지 않고 공백( tab 또는 띄어쓰기 2번 )으로 내부를 판단한다.

# 잘못된 예시
def say_myname(){
print("Hello World")
}

# 올바른 예시
def say_myname():
  print("Hello World")

 

4. if문 선언 시 중괄호( '{' ) 는 사용하지 않고  공백( tab 또는 띄어쓰기 2번 )으로 내부 및 조건을 판단한다.

-  ( && , || ) 대신 ( and, or ) 를 사용한다

# Javascript에서 if문 사용 시
if( a == 1 && a != 0 ) {
 console.log( 'a is 1' ); 
}
else if( b == 1 || b == 1 ){
 console.log( 'b is 1' ); 
}
else{
 console.log( 'nothing' ); 
}

# Python에서 if문 사용 시
if a == 1 and a != 0 :
  print( 'a is 1' )
elif b == 1 or b == 1:
  print( 'b is 1' )
else:
  print( 'nothing' )

 

 

반응형

'프로젝트 공부 > 파이썬 입문' 카테고리의 다른 글

설치 없이 파이썬 연습하기  (0) 2023.01.25