목록Programming/Python (20)
Allen's 데이터 맛집
파이썬에서 함수는 코드의 재사용성을 높이고, 코드의 가독성을 향상시키며, 모듈화를 촉진하는 중요한 요소입니다. 함수는 특정 작업을 수행하기 위해 그룹화된 문장들의 블록으로, 필요할 때 여러 번 호출할 수 있습니다. 함수는 다음과 같은 방식으로 정의됩니다. def 함수이름(매개변수): # 함수의 몸체 # 함수가 수행할 작업을 포함하는 코드 블록 return 반환값 여기서 함수 정의에 사용된 요소들은 다음과 같습니다: def: 함수 정의를 시작하기 위한 키워드입니다. 함수이름: 함수의 식별자입니다. 이는 함수를 호출할 때 사용됩니다. 매개변수: 함수가 작동하기 위해 필요한 입력 값들을 가리킵니다. 이는 함수에 정보를 전달하는 방법입니다. 몸체(body): 함수의 코드 블록이며, 함수가 수행할 작업을 포함합니..
리스트 자료형에서 random.choice 함수를 쓰면 리스트에서 랜덤하게 하나의 데이터가 출력됩니다. import random word = ['apple','banana','kiwi'] choiceList = random.choice(word) print(choiceList)
이번 포스팅에서는 파이썬에서 쓰이는 세가지 대표적인 자료형에 대해 알아보겠습니다. 1. 리스트(List) 리스트는 대괄호 [] 안에 쉼표로 구분된 값들의 순서가 있는 모임입니다. 리스트는 변경 가능한(mutable) 자료형이며, 다양한 타입의 요소를 포함할 수 있습니다. # 리스트 생성 my_list = [1, 2, 3, 'a', 'b', 'c'] # 리스트 요소 접근 print(my_list[0]) # 1을 출력합니다. # 리스트 요소 변경 my_list[0] = 100 # 리스트 길이 print(len(my_list)) # 리스트의 길이를 출력합니다. # 리스트에 요소 추가 my_list.append('d') # 리스트에 'd'를 추가합니다. # 리스트에서 요소 제거 my_list.remove('a'..
라이브러리를 사용하여 내림, 올림, 제곱근, 그리고 임의의 수를 생성 해 보겠습니다. from math import ceil, floor, sqrt print(floor(4.99)) # 4. 내림 print(ceil(4.14)) # 5. 올림 print(sqrt(16)) # 4. 제곱근 from random import * # random 라이브러리 사용 print(random()) # 0.0 ~ 1.0 미만의 임의의 값 생성 print(int(random()*100)) # 0 ~ 100까지 정수 값 생성 >>> 4 >>> 5 >>> 4.0 >>> 0.81895934611... >>> 24