Language/Python

# 함수 정의def add(a, b): result = a + b return result# 함수 호출sum = add(3, 5)print("합계:", sum) # 출력: 합계: 8
text = 'abcd'text.isalpha() >>> True
1. 키워드 사용 불가2. 숫자로 시작 할 수 없다3. 공백을 포함 할 수 없다.4. 특수문자 _만 허용
Error :SyntaxError: Non-ASCII character '\xed' in file t.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details코드 상단에 아래와 같이 작성문자 인코딩이 utf-8# -*- coding: utf-8 -*-문자 인코딩이 euc-kr# -*- coding: euc-kr -*-예시# -*- coding: utf-8 -*-import json# JSON 파일 경로file_path = "data_test.json"
import datetimecurrent_time = datetime.datetime.now()print("Current time:", current_time)
# 두 수를 더하는 간단한 람다 함수add = lambda x, y: x + yprint(add(5, 3)) # 출력: 8# 리스트의 각 요소에 2를 곱하는 람다 함수를 map 함수와 함께 사용하는 예제numbers = [1, 2, 3, 4, 5]doubled_numbers = list(map(lambda x: x * 2, numbers))print(doubled_numbers) # 출력: [2, 4, 6, 8, 10]# 람다 함수를 정렬 기준으로 사용하는 예제points = [(1, 2), (3, 1), (5, 3), (4, 0)]points.sort(key=lambda x: x[1]) # 두 번째 요소를 기준으로 정렬print(points) # 출력: [(4, 0), (3, 1), (1, 2)..
def snake_to_camel(snake_case): components = snake_case.split('_') camel_case = components[0] + ''.join(x.title() for x in components[1:]) return camel_case# 사용 예시snake_case = "my_variable_name"camel_case = snake_to_camel(snake_case)print(camel_case) # result : myVariableName
# 입력한 숫자가 양수인지 음수인지를 판별하는 예제num = int(input("숫자를 입력하세요: "))if num > 0: print("입력한 숫자는 양수입니다.")elif num
# 1부터 5까지의 숫자를 출력하는 예제num = 1while num
# 리스트의 각 요소에 대해 반복하여 출력하는 예제fruits = ["사과", "바나나", "딸기"]for fruit in fruits: print(fruit)
def to_snake_case(text): result = ''.join(['_' + c.lower() if c.isupper() else c for c in text]).lstrip('_') return resultoriginal_text = "getTimeStamp"snake_case_text = to_snake_case(original_text)print(snake_case_text) # result : get_time_stamp
자료형숫자형 (int, float, complex)문자열 (str)리스트 (list)튜플 (tuple)집합 (set)사전 (dictionary)숫자형# 두 숫자를 더한 결과 출력num1 = 10num2 = 20result = num1 + num2print("두 숫자의 합:", result)문자열# 문자열 합치기str1 = "Hello"str2 = "World"result = str1 + " " + str2print(result)리스트# 리스트 순회하며 합계 계산numbers = [1, 2, 3, 4, 5]total = 0for num in numbers: total += numprint("리스트의 합계:", total)튜플# 튜플 순회하며 값 출력tup = (1, 2, 3, 4, 5)for valu..
try: # 예외가 발생할 수 있는 코드 result = 10 / 0 # 예외를 발생시킵니다.except Exception as e: # 모든 예외를 처리하는 예외 처리 print("예외가 발생했습니다:", e)
import json # 주어진 리스트 data = [ {"name": "콜라", "price": 26}, {"name": "김밥", "price": 27}, {"name": "사이다", "price": 28} ] # 각 객체를 JSON 문자열로 변환하고 합치기 json_string = '{' + ', '.join(json.dumps(obj, ensure_ascii=False) for obj in data) + "}" # 결과 출력 print(json_string) #결과 {{"name": "콜라", "price": 26}, {"name": "김밥", "price": 27}, {"name": "사이다", "price": 28}}
728x90
반응형
공손(gongson)
'Language/Python' 카테고리의 글 목록