https://wikidocs.net/71
type : 변수에 자료형을 확인 할때 ( print type(range(3)) )
자료형 : 수치형( int, long, float), 순서형(string, lst, typle, 사용자 정의 클래스), 매핑형( dictionary, 클래스 인스턴스)
문자열을 만드는 방법 4가지
https://wikidocs.net/13
튜플 Tuple
2개의 변수를 선언하고, 값을 변경하려고 할때
type : 변수에 자료형을 확인 할때 ( print type(range(3)) )
자료형 : 수치형( int, long, float), 순서형(string, lst, typle, 사용자 정의 클래스), 매핑형( dictionary, 클래스 인스턴스)
문자열을 만드는 방법 4가지
https://wikidocs.net/13
튜플 Tuple
2개의 변수를 선언하고, 값을 변경하려고 할때
c,d,e = d,e,c
이렇게
원소가 없는 튜플을 선언할때는 소괄호를 () :
empty = () , empty = ('a','b','c')
원소를 하나만 가진 튜플을 만들때는 원소 뒤에 콤마를 :
one = 5,
리스트를 튜플로 변환 : tuple( tmpList )
사전 Dictionary
dictionaries : key-value
사전 Dictionary
dictionaries : key-value
형태
[lhr@tpl-master python]# cat dataStructure.py
#!/usr/bin/python
print '===== 1. Type(val)'
## type
print type('A')
print type(100)
print type(2.0)
print type(2L)
print type(range(3))
## string
## slicing
print '===== 2. String'
def testString():
x='lhr0916'
print 'length : ',len(x)
print x[0]
print x[3:5]
print x[:3]
print x[3:]
y='id:'+x[3:]
print y
testString()
#!/usr/bin/python
print '===== 1. Type(val)'
## type
print type('A')
print type(100)
print type(2.0)
print type(2L)
print type(range(3))
## string
## slicing
print '===== 2. String'
def testString():
x='lhr0916'
print 'length : ',len(x)
print x[0]
print x[3:5]
print x[:3]
print x[3:]
y='id:'+x[3:]
print y
testString()
## list
# append(val), sort(), remove(val), del list[index]
print '===== 3. list'
def testList():
prime=[2,3,7,7]
prime.
print '===== 3. list'
def testList():
prime=[2,3,7,7]
prime.
append
(5)
print prime
prime.
print prime
prime.
sort()
print prime
prime.
prime.
remove(2)
print prime
del prime[0]
print prime
prime[0] = 1
print prime
testList()
def testDList():
orders = ['potato', ['pizza','cake','salad'], 'hamburger']
print orders[1]
print orders[1][0]
print orders[len(orders)-1]
testDList()
def strToList():
str = raw_input('str :')
tmpCh = []
for ch in str:
tmpCh.append(ch)
print 'to list : ',tmpCh
print 'reverse str : ', reduce( lambda x,y: y+x, str)
print 'reverse list : ', reduce( lambda x,y: y+x, tmpCh)
strToList()
def jumsu():
hr = [90,80,70]
js = [80,60,95]
tr = [100,70,95]
stu = [hr, js, tr]
for p in stu:
print 'avg by student : ', avgByStudent(p)
##
def avgByStudent(jumsu):
sum = reduce(lambda x,y: x+y, jumsu)
return jumsu, sum, float(sum/len(jumsu))
jumsu()
prime[0] = 1
print prime
testList()
def testDList():
orders = ['potato', ['pizza','cake','salad'], 'hamburger']
print orders[1]
print orders[1][0]
print orders[len(orders)-1]
testDList()
def strToList():
str = raw_input('str :')
tmpCh = []
for ch in str:
tmpCh.append(ch)
print 'to list : ',tmpCh
print 'reverse str : ', reduce( lambda x,y: y+x, str)
print 'reverse list : ', reduce( lambda x,y: y+x, tmpCh)
strToList()
def jumsu():
hr = [90,80,70]
js = [80,60,95]
tr = [100,70,95]
stu = [hr, js, tr]
for p in stu:
print 'avg by student : ', avgByStudent(p)
##
def avgByStudent(jumsu):
sum = reduce(lambda x,y: x+y, jumsu)
return jumsu, sum, float(sum/len(jumsu))
jumsu()
## Tuples
print '===== 4. Tuples'
def testTuples():
c = 100
d = 200
e = 300
def testTuples():
c = 100
d = 200
e = 300
c,d,e = d,e,c
print c,d,e
empty = ()
print empty
empty = ()
print empty
one = 5,
print type(one), one
tmpT = ('a', 'b','c')
print tmpT, tmpT[ len(tmpT)-1 ]
testTuples()
def magu_print(x, y, *rest):
print x,y,rest
magu_print(1,2,'a','b','c',3,4,5,['aa','bb'])
## from list to tuple
def toList():
tmpT= (1,2,3,4)
print tmpT, list(tmpT)
## from tuple to list
def toTuple():
tmpL= [1,2,3,4,5]
print tmpL, tuple(tmpL)
toList(), toTuple()
testTuples()
def magu_print(x, y, *rest):
print x,y,rest
magu_print(1,2,'a','b','c',3,4,5,['aa','bb'])
## from list to tuple
def toList():
tmpT= (1,2,3,4)
print tmpT, list(tmpT)
## from tuple to list
def toTuple():
tmpL= [1,2,3,4,5]
print tmpL, tuple(tmpL)
toList(), toTuple()
## dictionaries : key-value
print '===== 5. Dictionaries'
def testDc():
dic = {}
dic['apple'] = 'fruit with red or yellow or green skin and sweet to tart crisp whitish flesh'
dic['lemon'] = 'yellow oval fruit with juicy acidic flesh'
def testDc():
dic = {}
dic['apple'] = 'fruit with red or yellow or green skin and sweet to tart crisp whitish flesh'
dic['lemon'] = 'yellow oval fruit with juicy acidic flesh'
dic['banana'] = 'yellow oval fruit'
dic['banana'] = 'two yellow oval fruit'
print dic, '\n' , dic.keys(), '\n' , dic.values() , '\n' ,dic['apple']
del dic['apple']
# in 을 이용해서, dic 이라고 선언한 사전에 apple 이라는 키가 있는지 확인
# 버전 3부터는 has_keys(key)를 사용
del dic['apple']
# in 을 이용해서, dic 이라고 선언한 사전에 apple 이라는 키가 있는지 확인
# 버전 3부터는 has_keys(key)를 사용
if 'apple' in dic:
print dic['apple']
else:
print 'Has no apple'
testDc()
else:
print 'Has no apple'
testDc()
'파이썬' 카테고리의 다른 글
드디어 클래스! (0) | 2015.11.10 |
---|---|
파이썬 공부하기_파일 다루기 (0) | 2015.11.10 |
파이썬 공부중_여러가지 import os, os.path, glob, sys, math, calendar, string, (0) | 2015.11.10 |
왕초보를 위한 파이썬 연습하기 예제 공부하기_함수편(return, lambda, reduce, map, filter) (0) | 2015.11.10 |
왕초보를 위한 파이썬 연습하기 예제 공부하기 (0) | 2015.11.10 |