
간단한 파이썬 문법 정리입니다.
1. 숫자, 문자, 불린형 자료형
- 정수형, 실수형
print 출력시에 sep를 사용해서 공백문자를 지정할 수 있다.
print(1, 3, 0, -1)
print(1, 3, 0, -1, sep="") // sep : 공백문자
print(1.1, 3.1, 0, -1.4)
출력결과
1 3 0 -1
130-1
1.1 3.1 0 -1.4
- 문자열, 불린형 자료형
print 출력시에 end를 사용해서 줄바꿈을 생략할 수 있다.
print("python", end="")
print("python1")
print("python")
print(True)
출력결과
pythonpython1
python
True
2. 변수 - 데이터를 저장할 공간
언제든지 데이터를 변경할 수 있다.
name = "python"
level = 5
health = 800
attack = 90
print(name, level, health, attack)
출력결과
python 5 800 90
3. 연산과 연산자
산술연산
- 숫자연산
x = 5
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y) // 몫
print(x % y) // 나머지
print(x ** y) // 제곱
출력결과
7
3
10
2.5
2
1
25
- 문자열연산
tag1 = "#first"
tag2 = "first2"
tag3 = "first3"
tag = tag1 + tag2 + tag3
print(tag)
message = "we all love python.\n" * 5
print(message)
출력결과
#firstfirst2first3
we all love python.
we all love python.
we all love python.
we all love python.
we all love python.
복합할당연산자
level2 = 10
level += 1
health2 = 2000
health2 -= 300
attack2 = 300
attack2 *= 2
speed2 = 420
speed2 /= 2
print(level2, health2, attack2, speed2)
출력결과
10 1700 600 210.0
비교연산
print(2 > 3) -> False
print("121212" == "121212") -> True
논리연산
print(4 < 6 and 10 >= 10) // True and True -> True
print("naver" != "never" or "true" == "true") // False or True -> True
print(not 5==5) // not True -> False
멤버쉽 연산
print("a" in "abc") // 포함되어있으면 True -> True
print("d" not in "abc") // 포함되어있지 않다면 True -> True
데이터 입력 받기
x = input("입력1:")
y = input("입력2:")
자료형 확인하기 : type(x)
- type()함수로 변수의 자료형을 확인할 수 있습니다.
- print(type(x))으로 확인 가능
숫자형으로 변환 : int(데이터)
result = int(x) + int(y)
- 이런식으로 숫자형으로 변환해야 입력받은 데이터를 계산 가능
'코딩 > 파이썬 python' 카테고리의 다른 글
파이썬 모듈, 패키지, 파일 입출력 (0) | 2021.08.08 |
---|---|
파이썬 상속, 오버라이딩, 클래스변수 (0) | 2021.07.13 |