Home Python - 기본 문법
Post
Cancel

Python - 기본 문법

1. print() 함수

  • 파이썬의 출력함수
  • 사용법
    1
    
    print('Hello, World!')
    

2. 변수

  • 파이썬은 C나 Java와 다르게 변수의 유형을 선언하지 않음
  • C, Java : int num = 0
  • Python : num = 0
  • 변수 선언
    • 1
      2
      3
      4
      5
      
      n = 1 # 숫자형
      s = 'hello' # 문자형
      true = True  # 불린형
      l = [ 1, 'a', 5.2 ] # 리스트형
      dic = { "key": "value" } # 딕셔너리형
      

bool

  • True, False
    >> ★첫 글자는 무조건 대문자

list

  • 한 가지 유형밖에 넣지 못하는 C나 Java와 다르게 여러 형태의 요소 입력 가능
  • list_name = [ 13, 'string', False, { "dic":9 }, [ 'list', 0 ] ]
  • 순서가 있는 유형 : index 사용 가능
  • 예시)
    1
    2
    
    list_n = [ 1, 2, 3 ]
    print(list_n[0])
    
    • 결과: 1

dictionary

  • 다른 언어와 마찬가지로 “키”,”값”이 한 쌍으로 이루어져있는 유형
  • 순서가 없는 유형 : index 사용 불가, keyvalue를 호출
  • 예시)
    1
    2
    
    dic = { "a":10, "b":20 }
    print(dic[ "a" ])
    
    • 결과: 10

3. 연산

1
2
3
4
5
6
7
8
9
10
n = 3
s = "3"
# 숫자 + 문자
print( n + s ) # TypeError: unsupported operand type(s) for +: 'int' and 'str'

# 숫자 + 숫자
print( n + n ) # 6

# 문자 + 문자
print(s + s ) # 33
This post is licensed under CC BY 4.0 by the author.

JavaScript - 기능 정리

Python - 제어문

Comments powered by Disqus.