본문 바로가기

프로그래밍언어/Python

Python 스터디 1일차

Python Study 1일차

2014 01 08
Lan2
도대국

*설치

네이버 검색창 python 검색 홈페이지로 이동 – 3.3다운 환경변수 설정
@
환경변수 설정하기
내 컴퓨터 속성 고급 환경변수 시스템 변수 새로 만들기 변수이름:PYTHON_HOME, 변수 값:C:\Python33\ - Path변수 수정 아래 변수 값에서 가장 오른쪽으로 이동 후 ;%PYTHON_HOME% 입력

*python

- 컴퓨터와 대화하기 위한 언어.
- 인터프리터방식을 사용해서 컴파일 없이 한줄한줄 바로 결과확인가능
.
- 주석은 #으로 처리
.
- 2
버전과 3버전의 차이가 있으므로 주의.(long int로 통일, print형식 변환, int/int자동 float 처리
)
- 가독성이 뛰어나다.(들여쓰기
)
- 접착성이 좋다.(다른 언어와 합쳐서 사용가능 ex : jython)

*pyscripter

Python으로 프로그래밍을 하기 위한 Tool.
pyscripter
다운하기 네이버 검색창 pyscripter 검색 두 번째 클릭 다운

*숫자맞추기게임

-다음 코드를 해석하시오.

print("Welcome!")
g=input("Guess the number: ")
guess=int(g)
if guess == 5:
    print("You win")
else:
    print("You lose!")
print("Game over!")

※주의점

@입력받은 숫자는 문자형태이다.

g1=input("Guess the number: ")
g2=input("Guess the number: ")
print(g1+g2)

@변수의 조건

꼭 영문으로 시작
특수문자 사용x
예약어(class, import…)사용 불가


-정답이 높은지 낮은지 알려주게 해보자.

print("Welcome!")
g=input("Guess the number: ")
guess=int(g)
if guess == 5:
    print("You win")
else:
   
if guess > 5:
        print("Too high")
    else:
        print("Too low")

print("Game over!")


-반복해서 게임을 할 수 있게 해보자.

반복 -> 루프(loop) -> C++ while, for?? -> 정답!!

Python while형태
             while jogundaesang == jogun:
                           print(jogundaesang)

 

print("Welcome!")
guess = 0
while guess != 5:

    g=input("Guess the number: ")
    guess=int(g)
    if guess == 5:
        print("You win")
    else:
        if guess > 5:
            print("Too high")
        else:
            print("Too low")
print("Game over!")


-정답을 랜덤하게 바꾸어보자.

아래의 코드를 사용하여보자.
from random import randint
secret = randint(1,10)

 

from random import randint
secret = randint(1,10)
print("Welcome!")
guess = 0
while guess !=
secret
:
    g=input("Guess the number: ")
    guess=int(g)
    if guess ==
secret
:
        print("You win")
    else:
        if guess >
secret
:
            print("Too high")
        else:
            print("Too low")
print("Game over!")

 

*텍스트 데이터

-다음 코드를 해석하시오.

import urllib.request
page= urllib.request.urlopen("http://www.naver.com/")
text = page.read().decode("utf8")
print(text)

-텍스트 찾아보기

import urllib.request
page= urllib.request.urlopen("http://ddka.tistory.com/")
text = page.read().decode("utf8")
price=text[330:353]
print(price)

주의점

@1부터 시작이 아니라 0부터 시작이다.
@
스트링의 위치가 바뀌면, 출력결과도 바뀐다.

-텍스트 데이터의 다양한 메소드

import urllib.request
page= urllib.request.urlopen("http://ddka.tistory.com/")
text = page.read().decode("utf8")
price=text[330:353]
print(price)

print(price.endswith(".jpg"))
print(price.endswith(".com"))

print(price.upper())
print(price.lower())

print(price.replace("ddka","woni"))

print(price.find("h"))
print(price.find("t"))

print(price.startswith("h"))
print(price.startswith("t"))


txt = " asdfg "
print(txt)
print(txt.strip())

 

- 시간과 관련된 다양한 메소드

import time

t1=
time.clock()
print(t1)

t2=
time.gmtime()
print(t2)

t3=
time.localtime()
print(t3)

time.sleep(3)
print("After 3secs")

 

'프로그래밍언어 > Python' 카테고리의 다른 글

Python 스터디 3일차  (0) 2014.01.24
Python 스터디 2일차  (0) 2014.01.15
파이썬에서 GUI이용하기_tkinter  (0) 2012.08.02
조건문 if else  (0) 2012.08.02
파이썬 Dictionary  (0) 2012.08.02