반응형

기본 예제 - "예제중심의 파이썬" 1장

 

source

 

# -*- coding: utf-8 -*-

teamstandings = [('SK', 38,25,0),
                 ('SS',39,27,2),
                 ('KIA',38,29,0),
                 ('LG',36,30,0),
                 ('HH',29,39,1)
                 ]

for ts in teamstandings:
    team, win, lost, draw = ts
    games = win + lost + draw
    winrate = float(win) / (win + lost)
    format = '%s 팀은 총  %d 경기 중  %d 승으로 승률은   %.3f 입니다. '
    print(format % (team, games, win, winrate))
    #print( team, games, win, winrate )

# 4.1 숫자 계산
print("=" * 20 + '  4.1. 숫자계산    ' + "=" * 20)

print("1 + 2 = " , (1+2))
print("50 - 4 =" , 50-4)
print("5000 / 3 = ", 5000/3)
print("5000.0 / 3.0 = ", 5000.0/3.0)

print()
print('-' * 10 , 'calendar', '-' * 10)
import calendar
calendar.prmonth(2018, 2)

print()
print('-' * 10 , '문자열', '-' * 10)

print(' show * 3 = ', 'show' * 3)
print(' 11,200,000.replace(\',\', \'\' ) = ', '11,200,000'.replace(',', ''))
# 앞쪽 여섯글자
print('0~6 :  \'follower\'[0:6] = ', 'follower'[0:6])
# 앞의 0은 생략 가능
print('0~6 :  \'follower\'[:6] = ', 'follower'[:6])
# 4번부터 끝까지
print('4~end :  \'followerwhite\'[4:] = ', 'followerwhite'[4:])
#뒤에서 네번째부터 끝까지
print('-4 :  \'followerwhite\'[-4:] = ', 'followerwhite'[-4:])
# 단어의 첫 글자는 대문자, 나머지는 소문자
print(' \'espresso\'.title() = ', 'espresso'.title())
# 문자열의 길이(공백, 기호도 포함된다.)
print(' len(\'what are you?\') = ', len('what are you?'))
# 문장에서 특정문자열이 몇 번 나오는지 count
ans = "We're  here because you are looking for the best of the best of the best, sir!"
print(ans)
print( " ans.count(\'best\') = ",ans.count('best') )

 

print()
print('-' * 10 , ' 날짜 계산 ', '-' * 10)
from datetime import date

t   = date.today()
print( ' today : ' , t )
c   = date(2013, 9, 19)
gap = abs(c - t).days
subscrib = " %s - %s = - %s days. remain."
print( subscrib % (c, t, gap) )

print()
print('-' * 10 , ' 로또 번호 뽑기', '-' * 10)

lotto = list(range(1,46))
print(lotto)
import random
# lotto를 무작위로 섞는다.
random.shuffle(lotto)
print(lotto)
# 하나의 숫자를 꺼낸다.
print(' 1.  ', lotto.pop())
print(lotto)
# 하나의 숫자를 꺼낸다.
print(' 2.  ', lotto.pop())

for i in range(5):
    print(' ',i + 1,'. ', lotto.pop())
print(lotto)


family = ['father','mother','I','sister']
i = 0
print('family = ' , family )
for x in family:
    i += 1
    print(i ,'  - ', x)

print(range(4,8))   
for i in range(4,8): print('  - ', i)   

 

print('카이사르 암호문 만들기' , '-' * 20);
# from string import maketrans
# tt = maketrans('abcdefghijklmnopqrstuvwxyz', 'DEFGHIJKLMNOPQRSTUVWXYZABC')


print('  While 문   ' , '-' * 20);
num = 1
while num <= 10:
    print( num )
    num += 1

print('  IF 문   ' , '-' * 20);
a = 1234 * 4
b = 13456/4
if a < b:
    print( a )
else:
    print( b )
   
c = 15 * 5
d = 15 + 15 + 15 + 15 + 15

if c > d :
    print(' c > d ')
elif c == d:
    print(' c == d ')
else:
    print(' C < d ')
   
print('  구구단   ' , '-' * 20);
left = 8
rightlist = range(1,10)
for right in rightlist:
    print(' %d * %d = %d' % (left, right, left * right))

# * 로 마름모 만들기
a = 1
b = 10
c = 0
d = 10
for i in range(10):
    if a == 1:
        print(' ' * b + ('*' * a))
    else:
        print(' ' * b + ('*' * a) + ('*' * c) )
    a += 1
    c += 1
    b -= 1
print('*'*a + '*'*c)
for i in range(10):
    c -= 1
    a -= 1
    print(' ' * b, '*' * a + ('*' * c) )
    b += 1


 

 

 

result 

 SK 팀은 총  63 경기 중  38 승으로 승률은   0.603 입니다.
SS 팀은 총  68 경기 중  39 승으로 승률은   0.591 입니다.
KIA 팀은 총  67 경기 중  38 승으로 승률은   0.567 입니다.
LG 팀은 총  66 경기 중  36 승으로 승률은   0.545 입니다.
HH 팀은 총  69 경기 중  29 승으로 승률은   0.426 입니다.
====================  4.1. 숫자계산    ====================
1 + 2 =  3
50 - 4 = 46
5000 / 3 =  1666.6666666666667
5000.0 / 3.0 =  1666.6666666666667

---------- calendar ----------
   February 2018
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28
 
---------- 문자열 ----------
 show * 3 =  showshowshow
 11,200,000.replace(',', '' ) =  11200000
0~6 :  'follower'[0:6] =  follow
0~6 :  'follower'[:6] =  follow
4~end :  'followerwhite'[4:] =  owerwhite
-4 :  'followerwhite'[-4:] =  hite
 'espresso'.title() =  Espresso
 len('what are you?') =  13
We're  here because you are looking for the best of the best of the best, sir!
 ans.count('best') =  3

----------  날짜 계산  ----------
 today :  2013-02-08
 2013-09-19 - 2013-02-08 = - 223 days. remain.

----------  로또 번호 뽑기 ----------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]
[21, 40, 1, 8, 15, 9, 11, 23, 31, 20, 10, 28, 13, 41, 19, 6, 12, 39, 43, 18, 27, 16, 24, 5, 29, 14, 30, 34, 35, 38, 32, 3, 17, 45, 7, 37, 4, 2, 25, 22, 33, 42, 44, 26, 36]
 1.   36
[21, 40, 1, 8, 15, 9, 11, 23, 31, 20, 10, 28, 13, 41, 19, 6, 12, 39, 43, 18, 27, 16, 24, 5, 29, 14, 30, 34, 35, 38, 32, 3, 17, 45, 7, 37, 4, 2, 25, 22, 33, 42, 44, 26]
 2.   26
  1 .  44
  2 .  42
  3 .  33
  4 .  22
  5 .  25
[21, 40, 1, 8, 15, 9, 11, 23, 31, 20, 10, 28, 13, 41, 19, 6, 12, 39, 43, 18, 27, 16, 24, 5, 29, 14, 30, 34, 35, 38, 32, 3, 17, 45, 7, 37, 4, 2]
family =  ['father', 'mother', 'I', 'sister']
1   -  father
2   -  mother
3   -  I
4   -  sister
range(4, 8)
  -  4
  -  5
  -  6
  -  7
카이사르 암호문 만들기 --------------------
  While 문    --------------------
1
2
3
4
5
6
7
8
9
10
  IF 문    --------------------
3364.0
 c == d
  구구단    --------------------
 8 * 1 = 8
 8 * 2 = 16
 8 * 3 = 24
 8 * 4 = 32
 8 * 5 = 40
 8 * 6 = 48
 8 * 7 = 56
 8 * 8 = 64
 8 * 9 = 72
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *
done.

 

반응형

+ Recent posts