인터프리터 언어
오픈소스 기반
코랩의 성능
셀의 종류
이미지 다루기
# 이미지와 연결
from IPython.display import Image
# 이미지 링크와 연결
Image('https://www.python.org/static/img/python-logo@2x.png')
유튜브 동영상 로딩하기
# 유튜브와 연결
from IPython.display import YouTubeVideo
# 유튜브 링크와 연결하여 출력
YouTubeVideo('pQ-w2z0Z47g', width=500)
파일 다루기
# 코랩 폴더에 있는 이미지파일을 가져옴
Image('background.jpeg',width=500)
from google.colab import drive
drive.mount('/content/drive')
Image('/content/drive/MyDrive/Colab Notebooks/PPML.gif')
print 함수
print('안녕하세요 파이썬!')
print(10)
print('10')
# 한 줄 주석문
'''
이 곳은 여러줄 주석문으로 처리됩니다
프로그램에 영향을 주지 않습니다.
'''
print('hello', 'Python')
print('hello','hi','Python')
# hello Python😤hello Python
print('hello', 'Python',end='😤')
print('hello', 'Python')
# hello Python
# hello Python
print('hello', 'Python',end='\n')
print('hello', 'Python')
print(10+3) # 덧셈
print(10-3) # 뺄셈
print(103) # 곱셈
**print(10/3) # 나눗셈 (파이썬의 나눗셈은 실수로 계산됨)
print(10%3) # 나머지
print('10%3')
print('10'+3) # TypeError: can only concatenate str (not "int") to str
출력 서식 지정하기
print('숫자 : %d' % 100) # 숫자 : 100
print('숫자 : %d, %d' % (100, 50)) # 숫자 : 100, 50
print('실수 : %f' % 100.3) # 실수 : 100.300000
print('문자열 : %s' % 'Python') # 문자열 : Python
print('문자열 : %s' % 10) # 문자열 : 10
print('%d * %d = %d' % (5, 3, 5*3)) # 5 * 3 = 15
print('%5d' % 100) # 100 (5글자를 채우기때문에 왼쪽에 2개의 공백이 있음)
print('%5d' % 10000) # 10000
print('%05d' % 100) # 00100
# 6: 소수점을 포함한 모든 실수자리 / 2: 소수자리
print('%6.2f' % 123.45) # 123.46
print('%6.2f' % 123.4567) # 123.46
print('%6.2f' % 123.4) # 123.40
print('%d / %d = %d' % (5, 3, 5/3)) # 5 / 3 = 1
print('%d / %d = %f' % (5, 3, 5/3)) # 5 / 3 = 1.666667
print('%d / %d = %4.2f' % (5, 3, 5/3)) # 5 / 3 = 1.67
format 함수와 출력 인덱스 사용
print('{0}{1}'.format('김사과','반하나'))
# 0:3s : 0번째 값의 3글자 스트링형으로 출력
# 1:03d :1번째 값에 첫번째 글자에 0 삽입 후 3글자 정수형으로 출력
print('{0:3s}님은 {1:03d}살입니다.')
변수
변수의 이름을 작성하는 방법
num1 = 10
num2 = 3
print('%d / %d = %4.2f' % (num1,num2, num1/num2))
변수의 데이터 타입
name = '김사과' # str
age = 20 # int
weight = 50.5 # float
isLover = False # bool
변수의 삭제
del age
input 함수
name = input('이름을 입력하세요: ')
print('%s님 안녕하세요!' % name)
name, age, weight = input('이름, 나이, 몸무게를 입력하세요 : ').split(' ');
print('{0}님의 나이는 {1}살이며, 몸무게는 {2}kg입니다.'.format(name, age, weight))
kor, eng, math = input('국어, 영어, 수학점수를 입력하세요 : ').split(' ')
# print('총점: ', kor+eng+math) # str 자료형이므로 연결됨
tot = int(kor) + int(eng) + int(math)
avg = tot / 3
print('총점: {}, 평균: {}'.format(tot, avg))
문자열 인덱싱
str = '문자열에 번호를 매겨 특정 문자를 찾을 수 있는 기능'
print(str[0]<) # 문
print(str[12]) # 특
print(str[-1]) # 능
문자열 슬라이싱
str = '문자열에서 원하는 부분의 범위를 설정하여 추출하는 기능'
print(str[0:5]) # 인덱스 0번부터 5 직전까지 (0부터 4까지)
print(str[7:13]) # 7부터 13까지
print(str[:13]) # 처음부터 13 직전까지
print(str[6:]) # 6부터 끝까지
문자열 함수
str = '문자열 함수는 문자열과 관련된 여러가지 기능(함수)'
print(len(str)) # 28
print(str.count('문자')) # 2
print(str.count('문장')) # 0
print(str.find('문')) # 0
print(str.find('뷁')) # -1
print(str.index('문')) # 0
print(str.index('뷁')) # 에러남
print(str.replace('함수', 'function'))
print(val.upper())
print(val.lower())
print(val.lstrip())
print(val.rstrip())
print(val.strip())
print(val.join('😆'))
파이썬은 ‘점프 투 파이썬’ 참고