Self-Improvement

Head First Python 2장 본문

프로그래밍/Python

Head First Python 2장

JoGeun 2018. 10. 21. 13:01

*vowels.py예제



-------------------------------------
#! /usr/bin/python3

vowels = ['a', 'e', 'i', 'o', 'u']
word = input("word : ")  
  #키보드로 받는다 
found =[]   
 #빈 리스트를 생성

for letter in word:    
#word변수의 내용 한글자씩 letter에게 할당이되어 반복한다
    if letter in vowels:    
#word한테 할당받은 letter이 vowels에 존재하는지
        if letter not in found:   
 #found에 존재하지 않으면 리스트에 추가
            found.append(letter)    

for vowel in found:   
 #found의 내용을 하나씩 할당하여 출력
    print(vowel)
--------------------------------------




*panic.py예제



----------------------------------------
#! /usr/bin/python3

phrase = "Don't panic!"
plist = list(phrase)   
 #plist에 리스트형식으로 한글자씩 추가
print(phrase)
print(plist)

for i in range(4):  
  #4번 반복을 한다.
    plist.pop()  
#plist 리스트의 끝에서부터 하나씩 삭제
plist.pop(0)  
#0번째을 제거
plist.remove("'")
plist.extend([plist.pop(), plist.pop()])  
#뒤의 두개를 빼서 붙힌다.
plist.insert(2, plist.pop(3))   
#세번째를 pop 하고 2번째자리에 넣는다
new_phrase = ''.join(plist)  
  #리스트를 문자열로 만들어줌
print(plist)
print(new_phrase)
-----------------------------------------


*panic2.py 예제

-------------------------------
#! /usr/bin/python3

phrase = "Don't panic!"
plist = list(phrase)  
#리스트형식으로 변경
print(phrase)
print(plist)

new_phrase = ''.join(plist[1:3])  
#1번방에서 3번방까지를 문자열로
new_phrase = new_phrase + ''.join([plist[5], plist[4], plist[7], plist[6]])   
#각 항목들을 문자열로 하여 연결

print(plist)
print(new_phrase)
-------------------------------




*marvic.py 예제



-----------------------------
#! /usr/bin/python3
paranoid_android = "Marvin, the Paranoid Android"
letters = list(paranoid_android)  
 #문자열을 리스트형식으로 변경

for char in letters[:6]:  
 #처음부터 6번째까지 반복
    print('\t', char)
print()
for char in letters[-7:]:   
#뒤에서 7번째부터 끝까지
    print('\t'*2, char)
print()
for char in letters[12:20]:    
#12번부터 20번까지
    print('\t'*3, char)
--------------------------------




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

python request 모듈  (0) 2018.10.21
Head First Python 5-1장  (0) 2018.10.21
Head First Python 4장  (0) 2018.10.21
Head First Python 3장  (0) 2018.10.21
Head First Python 1장  (0) 2018.10.21