This is a blog.

Python note(1) 본문

Python/Note

Python note(1)

Calcot 2024. 9. 12. 14:13

 

 

 

append() vs insert()

  • append
    • 배열의 마지막에 요소 추가
    • ex) list.append('수박')
  • insert
    • 원하는 위치에 요소 추가
    • ex) list.insert(1,'수박')

 

 

 

/, // , divmod()

  • / 와 //의 차이점
    • /는 소수까지 출력, //는 소수를 버리고 정수만 출력
  • divmod()
    • divmod(몫,나머지) → divmod(8 // 2, 8 % 2)
    • ex) divmod(8,2) = [4,0]

 

 

 

count()

  • 튜플, 리스트, 집합, 문자열에 사용 가능
  • dictionary, set 자료형에는 사용이 불가
# ex1
    a = 'gggfff'.count('g')
    print(a)
    ----------------------------------------
    3


# ex2
    a = [1,1,1,2,3]
    a.count(1)
    ----------------------------------------
    3
    
    
# ex3
    a = ['ab','a','b','ababab'].count('ab')
    print(a)
    ----------------------------------------
    1

 

 

 

sort(reverse=True) vs reverse()

  • sort(reverse=True)
a1 = [2,5,1,7]
a1.sort(reverse=True)
print(a1)
----------------------------------------
[7,5,2,1]
  • reverse()
a1 = [2,5,1,7]
a1.reverse()
print(a1)
----------------------------------------
[7,1,5,2]

 

 

 

isdigit()

  • String클래스에 있는 메서드
  • '-','.'을 문자로 판단 = 실수나 음수를 판단하지 못한다.
  • 0을 포함한 양수형 정수로만 이루어진 문자열만 True
  • str.isdigit("문자열")
  • "문자열".isdigit()
str.isdigit('0') 	-> True
'0'.isdigit() 		-> True

 

 

 

compile()

#compile을 사용하기 위해서는 import re가 필요
import re

# 정규표현식 한 자리 정수만
p = re.compile('[0-9]{1}');
my_string = "aAb1B2cC34oOp";
answer = re.findall(p,my_string)

print(answer)

----------------------------------------

['1', '2', '3', '4']

 

 

 

 

'Python > Note' 카테고리의 다른 글

Python note(3) with Claude.ai  (0) 2024.10.10
Python note(2)  (0) 2024.09.19
Comments