Python

python - 기본 (숫자 연산, 문자열 제어)

에이미103 2019. 7. 14. 17:24

# 주석

#한 줄 주석
""" 
여러줄 주석
"""

# 내장 상수

  • True, False, None(값이 존재하지 않음을 의미)

  • 대소문자 구분함

a=True
b=False
c=None #데이터 타입 명시하지 않음

# 출력 

for tmp in range(10): #0~9
    print("tmp:{}".format(tmp), end=",")
#print함수는 기본적으로 출력한다음 한줄을 넘김
#print함수의 마지막 인자로 출력에 대한 제어 가능

built-in data type & Variable

 - Numeric

 - Sequence

 - Text Sequence

 - Set

 - Mapping

 - Class

 

# built-in data type : Numeric type(숫자형)

 - int (정수)

 - float (실수)

 - complex (복소수)

a = 123 #정수(int)
b = 3.14152 #실수(float)
c = 3.14E10 #실수
d = 1 + 2j #복소수(complex)
e = 0o34 #8진수
f = 0xAB #16진수

div = 10/3
print(div)

result = 10 ** 3 #3의 4제곱(제곱)
print(result)

result = 10 % 3 #나머지 연산 : %
print(result)

result = 10 // 3 #나눗셈의 몫: //, 정수값으로 리턴
print(result)

 

built-in data type : Text Sequence type 

#built-in data type - Text Sequence type :  str
a = "안녕하세요"
b = '한 줄 주석'
c = """여러줄에 걸친
문자열도
만들수 있어요"""

print(a)
print(b)
print(c)

 

#문자열 연산

first ="이것은"
second ="소리없은"
third ="아우성"
print(first + second + third)

number = 100
print(first + str(number))

text = "python"
print(text * 3)

sample_text = "Show me the Money!"
print(sample_text[0])
print(sample_text[-1])
print(sample_text[1:3])
print(sample_text[:3])
print(sample_text[3:])
print(sample_text[:])

print("sample" in sample_text)
print("sample" not in sample_text)

apple = 5
banana = "여섯"
my_text = "나는 사과 %d개, 바나나 %s개 가지고 있어요" %(apple,banana)
print(my_text)       

#문자열 제어

sample_text = "cocacola"
print(len(sample_text)) #문자열 길이
print(sample_text.count("c")) #특정 문자열 count
print(sample_text.find("o")) #특정 문자열 첫 index 위치
print(sample_text.find("x"))

print("="*30)
a =":"
b ="abcd"
# # =>a:b:c:d
print(a.join(b))

print("="*30)
a = "   hoBBy    "
print(a.upper()) #모두 대문자
print(a.lower()) #모두 소문자
print(a.strip()) #문자열의 앞뒤 공백 제거

'Python' 카테고리의 다른 글

python - dict  (0) 2019.07.14
python - range  (0) 2019.07.14
python - Tuple  (0) 2019.07.14
python - list 함수  (0) 2019.07.14
python - list, indexing, slicing  (0) 2019.07.14