1 2 3 4 5 6 | 8 >> 1 8 << 1 2 & 4 #and 2 | 4 #or 8 ^ 2 #ex or ~ 0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | score = 1 while score ! = 0 : score = int ( input ( "점수입력 : " )) if score> = 90 : print ( "A" ) elif score> = 80 : print ( "B" ) elif score> = 70 : print ( "C" ) elif score> = 60 : print ( "D" ) else : print ( "F" ) print ( "학점입니다.^^\n" ) print ( "프로그램을 종료합니다" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | score = 1 while True : score = int ( input ( "점수입력 : " )) if score = = 0 : break if score> = 90 : print ( "A" ) elif score> = 80 : print ( "B" ) elif score> = 70 : print ( "C" ) elif score> = 60 : print ( "D" ) else : print ( "F" ) print ( "학점입니다.^^\n" ) print ( "프로그램을 종료합니다" ) |
1 2 | for i in range ( 0 , 3 , 1 ): #0으로 시작해서 3사이에 1씩 print ( "Hello" ) |
1 2 | for i in [ 0 , 3 , 1 ]: #0으로 시작해서 3사이에 1씩 print ( "Hello" ) |
1 2 | for i in range ( 0 , 6 , 1 ): #0으로 시작해서 6사이에 1씩 print ( "%d" % i) #"십진수의 형태로" i 값을 불러온다 |
1 2 | for i in [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]: #배열지정 print ( "%d" % i)nbsp; #"십진수의 형태로" i 값을 불러온다 |
1 2 3 4 | i,hap = 0 , 0 for i in range ( 1 , 11 , 1 ): hap = hap + i print ( "1에서 %d 까지의 합 = %d" % (i,hap)) |
1 2 3 4 5 6 7 8 9 10 | num = 1 while True : num = int ( input ( "num : " )) if num = = 0 : break if num = = 1 : print ( "다시 입력" ) continue print (num) print ( "0이면 종료" ) |
1 2 3 4 5 6 7 8 9 | import random my_tuple = ( "one" , "two" , "three" , "four" ) while True : i = random.randint( 0 , 3 ) print (my_tuple[i]) key = int ( input ( "key : " )) if key = = 0 : break print ( "프로그램 종료" ) |
대괄호로 묶으면 리스트 중괄호로 묶으면 사전 소괄호로 묶으면 튜플
1 2 3 4 5 6 7 8 9 10 11 | import random i = 0 key = 1 my_list = [ "one" , "two" , "three" , "four" ] while True : i = random.randint( 0 , 3 ) print (my_list[i]) key = int ( input ( "key : " )) if key = = 0 : break print ( "프로그램 종료" ) |
대괄호로 묶으면 리스트 중괄호로 묶으면 사전 소괄호로 묶으면 튜플
list값 변경
a_list = [10,20,30,40]a_list [2] = 300
list값 삭제
a_list = [10,20,30,40]
del(a_list[1])
dictionary
dic_1 = {1:'a',2:'b',3:'c'}
student = {'학번':1000,'이름':'홍길동','학과':'모바일'}
dictionary값 추가/변경
dictionary값 삭제
데이터 읽기
student['학번']
list(student.keys())
student.values()
student.items()
1 2 3 4 5 6 7 8 9 | #for문 활용 dictionary값 출력 mobile = {} #tuple->().list->[] mobile[ '이름' ] = '김수환' mobile[ '학번' ] = '130100682' mobile[ '주소' ] = '서울 용산구 보광동' mobile[ '학점' ] = 'A+' mobile[ '전화번호' ] = '010-1234-5678' for mo in mobile.keys(): print ( '%s ---> %s' % (mo,mobile[mo])) |
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 | #함수정의 def calc(num1,num2,op): result = 0 if op = = '+' : result = num1 + num2 if op = = '-' : result = num1 - num2 if op = = '*' : result = num1 * num2 if op = = '/' : result = num1 / num2 return result ##변수선언 res = 0 var1, var2, oper = 0 , 0 , "" ###메인코드 #res = calc(10,20,"+") print (res) print () oper = input ( "연산자 입력(+,-,*,/) : " ) var1 = int ( input ( "num1 값 입력 : " )) var2 = int ( input ( "num2 값 입력 : " )) res = calc(var1,var2,oper) print ( "계산기 : %d %s %d = %f " % (var1,oper,var2,res)) |
1 2 3 4 5 6 | def para_func(val_1 = 0 ,val_2 = 0 ,val_3 = 0 ,val_4 = 0 ,val_5 = 0 ): #매개변수 result = 0 result = val_1 + val_2 + val_3 + val_4 + val_5 return result value = para_func( 10 , 20 , 30 , 40 , 50 ) print (value) |
para_func(10,20,30)
60
1 2 3 4 5 6 7 8 | #함수의 가변 매개변수 def para_func( * para): result = 0 for num in para: result = result + num return result hap = para_func( 10 , 20 , 30 ) print (hap) |
'먼지 낀 책장사이 > Python' 카테고리의 다른 글
쓰레드 (0) | 2016.10.11 |
---|---|
상속, 오버라이딩, 툴킷 (0) | 2016.09.27 |
Raw파일 읽기, 클래스, 생성자 (0) | 2016.09.20 |
문자열, 구구단, 그림그리기 (0) | 2016.09.13 |
가변 매개변수, 모듈 (0) | 2016.09.06 |