Algorithm/Python 10

[파이썬] - index 함수

# index 함수 리스트, 배열에서 해당 값의 위치를 찾아주는 함수이다. 중복된 값이 있으면 가장 최소 위치(가장 앞) 반환함 위치 순서는 0부터 시작 alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] position = alphabet.index('f') print(position) # 5 출력 index(값, 시작, 끝) alphabet = ['f', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] position = alphabet.index('f') print(position) # 0 출력 position = alphabet.index('f', 2, 7) print(position) # 6 출력, 2번째..

Algorithm/Python 2022.08.31

[파이썬] - 반올림, 내림, 올림

# 반올림 round() 함수 사용하기 round(1.6) # 2 출력 round(1.789, 2) # 1.79 출력 round(11.789, -1) # 10 출력 # 내림 import math 후 math.floor() 사용하기 import math math.floor(3.1415) # 3 출력 # 올림 import math 후 math.ceil() 사용하기 import math math.ceil(3.1415) # 4 출력 How do you round UP a number? How does one round a number UP in Python? I tried round(number) but it rounds the number down. Example: round(2.3) = 2.0 and not..

Algorithm/Python 2022.08.26

[파이썬] - 리스트 랜덤하게 섞기, 순서 섞기

- 리스트 랜덤하게 섞기 import random 하기 random.shuffle(list) 사용! # 랜덤한 문자로 비밀번호 만들기 import random password_list = ['a', 'b', 'c', 'd', 'e', 'f'] print(password_list) # 랜덤하게 섞기 random.shuffle(password_list) print(password_list) password="" for j in password_list: password += j print(f"Your password is {password}") 리스트를 랜덤하게 섞고 문자열을 출력하려면 다시 문자열로 이어주기 ​

Algorithm/Python 2022.08.26

[파이썬] 리스트 list - 리스트 추가 / 랜덤한 항목 출력하기

- append 함수 리스트 마지막에 항목 추가 ​ - extend 함수 여러 개의 항목을 리스트 마지막에 추가 fruits = ["apple", "banana", "mango"] print(fruits) fruits.append("grape") print(fruits) fruits.extend(["melon", "orange"]) print(fruits) ​ - 랜덤한 항목 출력하기 리스트는 0부터 시작 마지막 항목의 인덱스는 총 개수 - 1 random.choice(fruits) 도 같은 결과 나옴 import random idx = random.randint(0, len(fruits)-1) #idx = random.choice(fruits) 와 같음 print(idx) print(fruits[idx..

Algorithm/Python 2022.08.26

[파이썬] startswith(), endswith() / 특정 문자열 찾기

startswith() : 특정 문자열로 시작하는지 True/False 반환 endswith() : 특정 문자열로 끝나는지 True/False 반환 - Hi로 시작하는 문장 수 세기 if line.startswith('Hi'): count = count + 1 문자열.startswith(찾을 문자열, 검사 시작 위치, 검사 끝 위치) 문자열.endswith(찾을 문자열, 검사 시작 위치, 검사 끝 위치) 시작위치, 끝 위치 생략가능

Algorithm/Python 2022.08.06
728x90