scope=[1,2,3]
for x in scope
print(x)
break
else:
print("Perfect")
Output:
1
break를 지우면,
Output:
1
2
3
Perfect
while condition:
repeating_code
continue # return to while condition
...
break # exit while condition
Saturday, September 9, 2017
Saturday, August 26, 2017
Python: tips for variables
키워드를 변수로 사용 : SyntaxError
키워드 확인
>>> import keyword
>>> keyword.kwlist
module 이름을 변수로 사용: 모듈사용못함, 하면 TypeError
>>> abs=1 # now abs type is 'int'
# How to use abs() function?
>>> abs = 1
>>> type(abs)
<type 'int'> # abs is int variable
>>> abs(-2) # TypeError
Traceback (most recent call last):
File "<pyshell#137>", line 1, in <module>
abs(-2)
TypeError: 'int' object is not callable
>>> del abs # delete int variable; remain internal function abs()
>>> type(abs)
<type 'builtin_function_or_method'>
>>> abs(-2)
2
>>>
키워드 확인
>>> import keyword
>>> keyword.kwlist
module 이름을 변수로 사용: 모듈사용못함, 하면 TypeError
>>> abs=1 # now abs type is 'int'
# How to use abs() function?
>>> abs = 1
>>> type(abs)
<type 'int'> # abs is int variable
>>> abs(-2) # TypeError
Traceback (most recent call last):
File "<pyshell#137>", line 1, in <module>
abs(-2)
TypeError: 'int' object is not callable
>>> del abs # delete int variable; remain internal function abs()
>>> type(abs)
<type 'builtin_function_or_method'>
>>> abs(-2)
2
>>>
Python: calling a class/method with parenthesis vs. no-parenthesis? whats the diff?
calling a class/method with parenthesis vs. no-parenthesis? whats the diff?
>>> class Service:
secret = "He is a alian."
def setname(self, name):
self.name=name
def sum(self,a,b):
print "{}, {}+{} = {}".format(self.name,a,b,a+b)
code source: https://wikidocs.net/28
aman = Service()
awoman = Service
>>> type(aman)
<type 'instance'>
>>> type(awoman)
<type 'classobj'>
>>>
classobj can assign a instance.
>>> kim=awoman()
>>> type(kim)
<type 'instance'>
>>> aman.setname("Strong")
>>> awoman.setname("Beauty")
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
awoman.setname("Beauty")
TypeError: unbound method setname() must be called with Service instance as first argument (got str instance instead)
>>>
Subscribe to:
Posts (Atom)