파이썬은 모든 변수가 객체 이다. 그 객체에 들어가는 값에 따라 type이 변하는것 같다.
변수에 숫자를 넣으면 숫자 객체가 되고, 변수에 문자를 넣으면 문자 객체가 되고, 변수에 리스트를 넣으면 리스트 객체가 된다.
my_int = 7
print(type(my_int))
my_float = 1.23
print(type(my_float))
my_int = 100 + 999999999999999999999999999999999999999999999999
print('my_int = ' + str(my_int))
출력값은
<class 'int'>
<class 'float'>
my_int = 1000000000000000000000000000000000000000000000099
type()함수는 변수의 형(클래스)가 무엇인지 확인 할 수 있다.
즉 my_int는 int형 객체가 되었고, my_int는 int형 클래스에서 생성된 객체라고 생각하면 될것 같다.
my_float는 float형 객체가 되었고,
파이썬에서 int형은 long형까지 포함한다고 생각하면 된다. 아주 큰 자리수의 숫자를 가질 수 있다. 100자리의 자리수도 가능하다.
a1 = 'aaa'
a2 = 'bbb'
a3 = a1 + a2
print(a3)
print(type(a3))
출력값은
aaabbb
<class 'str'>
greeting = 'HELLO WORLD!'
print(greeting[0],'\t',greeting[1],'\t',greeting[2],'\t',greeting[3],'\t',greeting[4]) # H E L L O
print(greeting[0:5]) # HELLO
print(greeting[:5]) # HELLO
print(greeting[6:]) # WORLD!
print(greeting[-3:]) # LD!
print(greeting[6:-3]) # WOR
print(len(greeting)) # 문자열 길이 12
print('Input your age !!')
your_age = input() # 입력 대기
print('Your age is ', your_age)
문자열을 원하는 위치에서 자를 수 있다.
greeting[0:5] 는 문자열의 0번째 자리수부터 5번째 자리수 앞까지를 가리킨다. (5번째 자리수는 포함하지 않는다.) (문자열은 0번째 자리부터 시작한다.)
문자열을 입력받기 위해서는 input() 을 이용하면 된다.
# 문자열 나누기 : split()
portal_site = 'naver daum nate'
mylist = portal_site.split(' ')
print(mylist) # ['naver', 'daum', 'nate']
print(mylist[0]) # naver
city_list = ['Seoul', 'Busan', 'Jeju', 'kimpo']
city_string = ', '.join(city_list)
print(city_string)
print('Hello' in 'Hello World') # True
print('대문자로 변환', 'Hello World'.upper()) # 대문자로
print('소문자로 변환','Hello World'.lower()) # 소문자로
출력값은
['naver', 'daum', 'nate']
naver
Seoul, Busan, Jeju, kimpo
True
대문자로 변환 HELLO WORLD
소문자로 변환 hello world
gu='seocho'
dong='yangjae'
bunji=123
a = '{} {} {}'.format(gu, dong, bunji)
b= '{2} {0} {1}'.format(gu, dong, bunji)
c = '{gu1} {dong1} {bunji1}'.format(gu1=gu, dong1=dong, bunji1=bunji)
mydict = {'gu' : 'seocho', 'dong' : 'yangjae', 'bunji' : 123}
d= '{0[gu]} {0[dong]} {0[bunji]}'.format(mydict)
e= f'{gu} {dong} {bunji}'
print(a) # seocho yangjae 123
print(b) # 123 seocho yangjae
print(c) # seocho yangjae 123
print(d) # seocho yangjae 123
print(e) # seocho yangjae 123
if 문으로 값을 비교할때 어떤값이 False인지 기억할 필요가 있다.
@ False인 값들
- None
- 0
- 0.0
- '' ==> 빈 문자열
- [] ==> 빈 리스트
- () ==> 빈 튜플
- {} ==> 빈 딕셔너리
소스 링크
https://replit.com/@dhshin38/Tutorial-Python#num_char.py
Loading...
Replit is a simple yet powerful online IDE, Editor, Compiler, Interpreter, and REPL. Code, compile, run, and host in 50+ programming languages.
replit.com
'Python' 카테고리의 다른 글
Python 정규식 (0) | 2022.01.15 |
---|---|
Python list, tuple, dictionary, 리스트, 튜플, 딕셔너리 (0) | 2022.01.15 |
Python 날짜, 스케쥴, process (0) | 2022.01.15 |
Python 특징 (0) | 2022.01.15 |
Python 개발 환경 (0) | 2022.01.15 |