print(None and 100)
None
print(None or 100)
100
print(not(''))
True
c = 3+4j
abs(c)
5.0
all([1, 2, 4])
True
all([1, 2, 0])
False
any([0, 2, 0])
True
any([0, 0, 0])
False
bin(7)
'0b111'
class Blah():
pass
b = Blah()
b.egg = 0
bool(b)
True
bool(b.egg)
False
bool(None)
False
bs = bytes('Hi there!', encoding='utf-8')
bs
b'Hi there!'
chr(97)
'a'
command_str = '''
pi = 3.141592
print('pi/2 = ', pi/2)
'''
cmd = compile(command_str, 'my_mod', 'exec')
exec(cmd)
pi/2 = 1.570796
complex()
0j
complex(5.5)
(5.5+0j)
complex(3, -2.1)
(3-2.1j)
complex(10-3.5j)
(10-3.5j)
complex(1, 5j)
(-4+0j)
complex('10.2+5j')
(10.2+5j)
complex('(10.2+5j)')
(10.2+5j)
complex('2')
(2+0j)
complex('3j')
3j
complex('1+j')
(1+1j)
class Dog:
bread = ''
my_dog = Dog()
my_dog.breed = 'St Bernard'
print(my_dog.breed)
a_str = 'breed'
print(hasattr(my_dog, a_str))
delattr(my_dog, a_str)
print(hasattr(my_dog, a_str))
delattr(my_dog, a_str)
St Bernard True False
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-21-b0d2639f97d1> in <module> 10 delattr(my_dog, a_str) 11 print(hasattr(my_dog, a_str)) ---> 12 delattr(my_dog, a_str) AttributeError: breed
dir()
['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'quit']
def demo():
i = 1
j = 2
print(dir())
demo()
['i', 'j']
print(dir(int))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
quot, rem = divmod(203, 10) # 결과는 20, 나머지는 3
print(quot, rem)
20 3
divmod(10.6, 0.5)
(21.0, 0.09999999999999964)
beat_list = ['John', 'Paul', 'George', 'Ringo']
for n, item in enumerate(beat_list, 1):
print('%s. %s' % (n, item))
1. John 2. Paul 3. George 4. Ringo
a = 100
eval('3 * a + 55')
355
eval('3 * 100 + 55', {}) # 문제 없음
355
eval('3 * a + 55', {}) # 에러; 'a'가 정의되지 않음
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-0c0784b7797f> in <module> ----> 1 eval('3 * a + 55', {}) # 에러; 'a'가 정의되지 않음 <string> in <module> NameError: name 'a' is not defined
from math import *
a = 25
dict_1 = {'tan': tan, 'radians': radians, 'a': a }
eval('1000 * tan(radians(a))', dict_1)
466.3076581549986
eval('a * a + 100', {}, locals())
725
def is_neg(x):
return x < 0
my_list = [10, -1, 20, -3, -2.5, 30]
print(list(filter(is_neg, my_list)))
[-1, -3, -2.5]
n = 1
yyy = float(n) # yyy에 1.0 대입.
amt = float('-23E01') # amt에 -23E01 대입.
print(yyy, amt)
1.0 -230.0
amt = float() # amt에 0.0 대입.
amt
0.0
format(1000000, ',')
'1,000,000'
frozenset((1, 2, 2, 3, 3, 4, 1))
frozenset({1, 2, 3, 4})
class Dog:
breed = ''
d = Dog()
d.breed = 'Bulldog'
attr_name = 'breed'
getattr(d, attr_name)
'Bulldog'
class Dog:
breed = ''
my_dog = Dog()
my_dog.breed = 'Husky'
nm = 'breed'
hasattr(my_dog, nm)
True
hex(23)
'0x17'
my_name = input('Enter your name, please: ')
my_age = int(input('Enter your age, please: '))
Enter your name, please: insuk Enter your age, please: 40
int('1000', 16)
4096
int(3.99), int(-3.99)
(3, -3)
class Floopy(int):
pass
f = Floopy()
issubclass(Floopy, int)
True
issubclass(int, Floopy)
False
issubclass(int, int)
True
issubclass(f, int) # TypeError: f 는 클래스가 아니다
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-34-04af2111b52a> in <module> ----> 1 issubclass(f, int) # TypeError: f 는 클래스가 아니다 TypeError: issubclass() arg 1 must be a class
gen = iter(5) # TypeError 발생
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-35-efc46b2e1ac3> in <module> ----> 1 gen = iter(5) # TypeError 발생 TypeError: 'int' object is not iterable
gen = iter([5])
gen
<list_iterator at 0x14f518569c8>
gen = iter([1, 2, 3])
next(gen)
1
next(gen)
2
next(gen)
3
next(gen)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-41-6e72e47198db> in <module> ----> 1 next(gen) StopIteration:
len('Hello')
5
print(list('cat'))
['c', 'a', 't']
new_list = list() # new_list를 빈 리스트로 초기화.
new_list
[]
def foobar():
a = 2
b = 3
c = 1
print(locals())
foobar()
{'a': 2, 'b': 3, 'c': 1}
def multy(a, b, c):
return a * b * c
m = map(multy, [1, 20], [1, 20], [1, 50])
print(list(m))
[1, 20000]
max(1, 3.0, -100, 5.25)
5.25
from fractions import Fraction
a_list = [1, Fraction('5/2'), 2.1]
max(a_list)
Fraction(5, 2)
min(1, 3.0, -100, 5.25)
-100
from fractions import Fraction
a_list = [1, Fraction('5/2'), 2.1]
min(a_list)
1
oct(9)
'0o11'
f = open('mydata.dat', 'wb')
f.close()
chr(ord('a'))
'a'
pow(2, 4)
16
pow(2, 4, 10)
6
pow(1.1, 100)
13780.61233982238
s1 = 'eenie'
s2 = 'meenie'
s3 = 'Moe'
print(s1, s2, s3, sep='; ')
eenie; meenie; Moe
list(range(5))
[0, 1, 2, 3, 4]
list(range(1, 7, 2))
[1, 3, 5]
list(range(5, 1, -1))
[5, 4, 3, 2]
my_str = "Hi, I'm Brian!"
print(repr(my_str))
"Hi, I'm Brian!"
my_str
"Hi, I'm Brian!"
print(list(reversed([1, 2, 3])))
[3, 2, 1]
str(reversed('Wow, Bob, wow!'))
'<reversed object at 0x000001EA04901BC8>'
'Wow, Bob, wow!'[::-1]
'!wow ,boB ,woW'
round(12.555, 1)
12.6
round(12.555, 2)
12.55
round(12.555, 0)
13.0
round(12.555)
13
round(12.555, -1)
10.0
round(-55.55)
-56
my_list = [11, 11, 3, 5, 3, 3, 3]
my_set = set(my_list)
my_set
{3, 5, 11}
class Dog:
breed = ''
d = Dog()
d.breed = 'Dane'
attr_str = 'breed'
setattr(d, attr_str, 'Dane') # d.breed = 'Dane' 설정
d.breed
'Dane'
sorted([5, 0, 10, 7])
[0, 5, 7, 10]
n = 10100140
s = str(n)
s.count('0')
4
bs = b'Hello!' # 정확하게 6 바이트를 보장한다
s = str(bs, encoding='utf-8')
print(s) # 일반 문자열 출력, 문자당 ? 바이트.
Hello!
def get_avg(a_list):
return sum(a_list)/len(a_list)
get_avg([1, 2, 3, 4, 10])
4.0
def gen_count(n):
i = 1
while i <= n:
yield i
i += 1
sum(gen_count(100))
5050
i = 5
type(i) is int
True
type(i) == int
True
a = [1, 2, 3]
b = [10, 20, 30]
c = [i[0] + i[1] for i in zip(a, b)]
c
[11, 22, 33]
a_list = list(zip(a, b))
a_list
[(1, 10), (2, 20), (3, 30)]
a = [1, 2, 3]
b = [10, 20, 30]
c = []
for i in range(min(len(a), len(b))):
c.append(a[i] + b[i])
c
[11, 22, 33]