Python - Type

닉네임유저·2023년 8월 3일

Python - 기초 문법

목록 보기
1/13
post-thumbnail

Python의 타입에는 여러가지가 있다.

숫자로 이루어진 숫자형

문자로 이루어진 문자형

논리적인 값을 나타내는 불린형 등 여러가지 타입이 존재한다.


Python - (Number Types)

# 정수형 (Integer): 정수를 표현하는 자료형입니다. 양수, 음수, 0을 모두 포함합니다.

num_int = 10

# 소수형 (Floating - point) (Float라고 씁니다.) : 소수점이 있는 실수를 표현하는 자료형

num_float : 3.14

# 복소수형 (Complex) : 실수와 허수로 이루어진 복소수를 표현하는 자료형

num_complex = 2 + 3j

# input() 으로 입력 받는법 
num_input = int(input('정수를 입력하시오 : '))

# 데이터 타입 확인
print(type(num_input))

Python - (String Types)

# 문자열은 문자들의 시퀀스로 이루어진 데이터 타입으로 , 따옴표 를 써서 표현합니다.

temp = "Hello, World!"
name = "Nick you"

# 파이썬의 문자열을 변경이 불가능한 (immutable) 시퀀스 자료형입니다. - 한 번 생성된 문자열은 수정할 수 없습니다.
# 다만 문자열을 조합하거나 새로운 문자열을 생성하는것은 가능합니다.

greeting = "Hello"
name_2 = "velog"

message_2 = greeting + ", " + name_2 + "!"  # 문자열을 조합하여 새로운 문자열 생성
print(message_2)  # Output: Hello, velog!

# 문자열 연산
str_1 = "Nick"
str_2 = "Name"
str_3 = "User"

print(str_1 * 5)       # 문자열 "Nick"을 5번 반복해서 출력
print("N" in str_2)    # 문자열 str_1에 "N"이 있는지 여부를 True 또는 False로 출력
print("User" is str_3) # 문자열 "Nick"과 str_1이 동일한 객체인지를 True 또는 False로 출력 (동일성 비교)

# 데이터 타입 확인
print(type(message_2))

Python - (Boolean Types)

# 불린 타입은 논리적인 값을 나타내는 자료형으로 , 두 가지의 값만 가질 수 있습니다.
# 조건 검사 및 상태표현 , 제어문 등에 주로 사용됩니다.
# True(참 ,1) , False(거짓, 0)

# bool 이란 클래스로 표현됩니다.
# 논리 연산자인 and, or , not 으로 구분하여 봅시다.

x = True
y = False

print(x and y)  # False
print(x or y)   # True
print(not x)    # False

# 데이터 타입 확인
print(type(x))

숫자형 변환

x = 1
y = 3.14
z = 9.17

print(float(x))
print(int(y))
print(complex(z))

문자형 변환

str_con = str(100)

print(type(str_con))


# 문자열 -> 숫자형 비교 연산
# 자료출저 : 백준 25206번
str_ = 'ObjectOrientedProgramming1 3.9 A+'
list_ = str_.split(' ')
print(list_)
if 1 < len(list_[0]) < 50:
    print('맞음')
else:
    print('과목의 길이가 맞지 않음')
print(list_[1])
score = list_[1]
f_st = score[2]
print(type(f_st))
print(float(f_st))
b_e = float(f_st)
print(type(b_e))
print(b_e)

if b_e >= 8.0:
    print(f' {int(score[0])+1} ')
else:
    print(f' {int(score[0])} ')

문자열 슬라이싱

temp_str = "Nick_name_user"

print(temp_str[0:5])
print(temp_str[3:7])
print(temp_str[-1:7])

문자열 함수

# len() : 문자열의 길이를 반환
temp = "Hello,world!"
len_ = len(temp)
print(len_)

# lower(), upper() : 문자열을 각각 소문자로 변환하거나, 대문자로 변환

text = "Hello, World!"
print(text.lower())  # "hello, world!
print(text.upper())  # "HELLO, WORLD!"

# strip(),lstrip(),rstrip() : 문자열 앞뒤의 공백을 제거

text = "   Hello, World!   "
print(text.strip())   # "Hello, World!"
print(text.lstrip())  # "Hello, World!   "
print(text.rstrip())  # "   Hello, World!"

# join() : 리스트의 각 요소를 지정된 구분자로 합쳐 하나의 문자열로 반환
fruits = ['apple', 'banana', 'orange']
text = ','.join(fruits)
print(text)  # "apple,banana,orange"

# replace() : 문자열 내의 특정 부분을 다른 문자열로 치환
text = "Hello, World!"
new_text = text.replace("Hello", "Hi")
print(new_text)  # "Hi, World!"
profile
이것저것 다해보는 개발자

0개의 댓글