1.파이썬은 c나 java와는 다르게 변수의 자료형을 지정하지 않는다.

 type() 함수로 데이터타입을 확인할 수 있다.

 동적데이터타입변환  #같은 변수에 데이터 여러번 입력가능

 

2.데이터타입변환 숫자, 문자열

x="2019"

y="2020"    #문자열데이터타입이므로 x-y 불가.

 

>>>x=int("2019")

>>>y=int("2020")

>>>x-y

-1

>>>type(x)

<class 'int'>

 

#데이터타입변환없이 숫자계산

>>>x="2019"

>>>y="2020"

>>>int(x)-int(y)

-1

>>>type(x)

<class 'str'>

 

3.정수와 실수의 정확도

import sys

sys.getsizeof()   #byte크기를 나타냄.

 

#정수는 자동적으로 크기저장. 정확

#실수는 소수점아래 16자리까지만 표현. 

 

4. ord(), bin(), 아스키코드

x="a"     #변수를 str로 지정. 따옴표이용

ord(x)

>>>97

bin(ord(x))    #bin()함수는 반드시 ord()를 이용해 숫자로 변환한 뒤 입력해야함 

>>>0b1100001

 

예제) KIM이라는 문자열이 메모리에 2진수로 어떻게 표현되는지 확인해보자. 각 문자를 변수로 지정한 후 ord 및 bin 함수를 사용하여 출력해보자.

5.input()   #기본적으로 문자열(str)로 인식한다. 숫자로 받아서 계산하려면 int(input()) float(input())으로 변환해야함.

name=input("이름이 무엇입니까?") 가능

 

예제) 자동챗봇만들기

#파이썬은 c나 java와 달리 세미콜론(;)이용해서 같은줄에 함수를 여러개 쓸 수 있다.

 

예제)번역프로그램 -구글 번역기와 연동

#웹브라우저 주소를 제대로 쓰고 +text라고 입력해야 자동번역됨.

 

5-1)정수입력.   

 변수=int(input(""))

 

1_  year=input("몇년생인가?")

age=2021-int(year)+1    #int()를 쓰지 않으면 str 과 int의 type error 발생

print("올해",age,"살이군요.")

 

#실수는 float(input())

 

 

예제) 태어난 달의 달력 출력하기

 

import calendar

 

year=int(input("당신이 태어난 년도는?"))

month=int(input("당신이 태어난 달은?"))

print("당신이 태어난 달의 달력입니다.")

calendar.prmonth(year,month)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6. 거북이 모듈로 그림그리기

 

import turtle

turtle.shape("turtle")

turtle.shapesize(5)

turtle.forward(200)

turtle.right(90)   #각도만큼 회전

turtle.backward(200)

turtle.home()

turtle.clear()

turtle.shape("arrow")     #square, circle, triangle, classic(shape 함수 실행안할때 기본형)

turtle.color("red")

 

1_) turtle 객체 여러개 사용할때

import turtle

t1=turtle.Turtle(); t1.shape("turtle")

t2=turtle.Turtle(); t2.shape("turtle")

t1.color("yellow"); t1.forward(200)

t2.color("purple"); t2.backward(100)

 

2_)간결하게 t로 생성

import turtle as t   #혹시 .py이름이 turtle 이면 실행 안됨

t.forward(200)

t.circle(50)   #힘겹게 원을 그리는데 귀엽다.

 

for i in range (4):     #같은작업은 4번 반복한다.

      t.forward(300)

      t.right(90)

 

 

3_)선분의 길이 textinpu()으로 입력받기

 

temp=t.textinput("사각형", "한변의 길이:")     #그냥 t.textinput()이라고 입력하면 에러가 발생

size=int(temp)   #따로 입력창이 뜬다.

 

4_)다각형 그리기

 

 

5_)좌표를 이용하여 그리기 goto(x,y) 함수 이용, write(text)

6_) fillcolor()이용

 

t.fillcolor()만 설정하거나, begin_fill() 있고 end_fill() 없으면 처음에 arrow만 노랗게 칠해짐

 

7_)circle()함수를 이용해서 간단하게 다각형 그리기

import turtle as t
n=int(t.textinput("다각형","몇각형을 원하니?"))
t.circle(150,360,n)

 

 

7.penup() pendown() 함수 -좌표축 생성가능, write()함수 좌표표시

 

7_1. y=x 그래프그리기

7_2.원 그리는 거북이. speed()함수

스피드는 0~10까지의 값. 10이상의 값은 10과 동일한 속도

 

 

turtle 함수 정리>>

shape()

shapesize()

register_shape() : 이미지파일로 개체모양 설정가능

home()

clear()

right/left()

forward()

backward()

goto(x,y)

circle()

dot.() : 크기가 ()인 점을 그린다

width() : 펜의 두께

color()

pencolor()

bgcolor() background color

bgcolor(r,g,b)

screensize(x,y)

penup()

pendown()

Screen() 배경화면을 가져온다

bgpic()

xcor() x위치 리턴

ycor()

setx(a) x축의 a위치로 이동 y위치 유지

sety(b)

distance(x,y) 현재 거북이의 위치와 (x,y)좌표거리를 계산하여 리턴

 

 

 

 

 

 

+ Recent posts