Skip to content

Instantly share code, notes, and snippets.

@mrbkdad
Created January 27, 2019 10:39
Show Gist options
  • Save mrbkdad/cfaa591c22a061c3055c67bfdbd177f4 to your computer and use it in GitHub Desktop.
Save mrbkdad/cfaa591c22a061c3055c67bfdbd177f4 to your computer and use it in GitHub Desktop.
Python basic course 1
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 자료형\n",
"1. Int, float, long, string, boolean, byte ...\n",
"1. https://jythonbook-ko.readthedocs.io/en/latest/DataTypes.html\n",
"1. 형변환 : int(1.0), str(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 1\n",
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 1.0\n",
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3]\n",
"l[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, dict)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m = {\"a\":1,'b':2}\n",
"m['a'],type(m)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({1, 2, 3}, set)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss = {1,2,3,3}\n",
"ss,type(ss)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'s'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1 = 'test'\n",
"s1[2]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.0'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(1.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 문자열\n",
"1. 정의, 사용\n",
"2. format"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abcdef\n"
]
}
],
"source": [
"s1 = 'abcdef'\n",
"print(s1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'cd'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1[2:4]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'fedcba'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'bdf'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1[1::2]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(s1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ABCDEF'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1.upper()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getnewargs__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mod__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__rmod__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'capitalize',\n",
" 'casefold',\n",
" 'center',\n",
" 'count',\n",
" 'encode',\n",
" 'endswith',\n",
" 'expandtabs',\n",
" 'find',\n",
" 'format',\n",
" 'format_map',\n",
" 'index',\n",
" 'isalnum',\n",
" 'isalpha',\n",
" 'isdecimal',\n",
" 'isdigit',\n",
" 'isidentifier',\n",
" 'islower',\n",
" 'isnumeric',\n",
" 'isprintable',\n",
" 'isspace',\n",
" 'istitle',\n",
" 'isupper',\n",
" 'join',\n",
" 'ljust',\n",
" 'lower',\n",
" 'lstrip',\n",
" 'maketrans',\n",
" 'partition',\n",
" 'replace',\n",
" 'rfind',\n",
" 'rindex',\n",
" 'rjust',\n",
" 'rpartition',\n",
" 'rsplit',\n",
" 'rstrip',\n",
" 'split',\n",
" 'splitlines',\n",
" 'startswith',\n",
" 'strip',\n",
" 'swapcase',\n",
" 'title',\n",
" 'translate',\n",
" 'upper',\n",
" 'zfill']"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(s1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class str in module builtins:\n",
"\n",
"class str(object)\n",
" | str(object='') -> str\n",
" | str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
" | \n",
" | Create a new string object from the given object. If encoding or\n",
" | errors is specified, then the object must expose a data buffer\n",
" | that will be decoded using the given encoding and error handler.\n",
" | Otherwise, returns the result of object.__str__() (if defined)\n",
" | or repr(object).\n",
" | encoding defaults to sys.getdefaultencoding().\n",
" | errors defaults to 'strict'.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __add__(self, value, /)\n",
" | Return self+value.\n",
" | \n",
" | __contains__(self, key, /)\n",
" | Return key in self.\n",
" | \n",
" | __eq__(self, value, /)\n",
" | Return self==value.\n",
" | \n",
" | __format__(...)\n",
" | S.__format__(format_spec) -> str\n",
" | \n",
" | Return a formatted version of S as described by format_spec.\n",
" | \n",
" | __ge__(self, value, /)\n",
" | Return self>=value.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __getitem__(self, key, /)\n",
" | Return self[key].\n",
" | \n",
" | __getnewargs__(...)\n",
" | \n",
" | __gt__(self, value, /)\n",
" | Return self>value.\n",
" | \n",
" | __hash__(self, /)\n",
" | Return hash(self).\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __le__(self, value, /)\n",
" | Return self<=value.\n",
" | \n",
" | __len__(self, /)\n",
" | Return len(self).\n",
" | \n",
" | __lt__(self, value, /)\n",
" | Return self<value.\n",
" | \n",
" | __mod__(self, value, /)\n",
" | Return self%value.\n",
" | \n",
" | __mul__(self, value, /)\n",
" | Return self*value.n\n",
" | \n",
" | __ne__(self, value, /)\n",
" | Return self!=value.\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | __rmod__(self, value, /)\n",
" | Return value%self.\n",
" | \n",
" | __rmul__(self, value, /)\n",
" | Return self*value.\n",
" | \n",
" | __sizeof__(...)\n",
" | S.__sizeof__() -> size of S in memory, in bytes\n",
" | \n",
" | __str__(self, /)\n",
" | Return str(self).\n",
" | \n",
" | capitalize(...)\n",
" | S.capitalize() -> str\n",
" | \n",
" | Return a capitalized version of S, i.e. make the first character\n",
" | have upper case and the rest lower case.\n",
" | \n",
" | casefold(...)\n",
" | S.casefold() -> str\n",
" | \n",
" | Return a version of S suitable for caseless comparisons.\n",
" | \n",
" | center(...)\n",
" | S.center(width[, fillchar]) -> str\n",
" | \n",
" | Return S centered in a string of length width. Padding is\n",
" | done using the specified fill character (default is a space)\n",
" | \n",
" | count(...)\n",
" | S.count(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the number of non-overlapping occurrences of substring sub in\n",
" | string S[start:end]. Optional arguments start and end are\n",
" | interpreted as in slice notation.\n",
" | \n",
" | encode(...)\n",
" | S.encode(encoding='utf-8', errors='strict') -> bytes\n",
" | \n",
" | Encode S using the codec registered for encoding. Default encoding\n",
" | is 'utf-8'. errors may be given to set a different error\n",
" | handling scheme. Default is 'strict' meaning that encoding errors raise\n",
" | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n",
" | 'xmlcharrefreplace' as well as any other name registered with\n",
" | codecs.register_error that can handle UnicodeEncodeErrors.\n",
" | \n",
" | endswith(...)\n",
" | S.endswith(suffix[, start[, end]]) -> bool\n",
" | \n",
" | Return True if S ends with the specified suffix, False otherwise.\n",
" | With optional start, test S beginning at that position.\n",
" | With optional end, stop comparing S at that position.\n",
" | suffix can also be a tuple of strings to try.\n",
" | \n",
" | expandtabs(...)\n",
" | S.expandtabs(tabsize=8) -> str\n",
" | \n",
" | Return a copy of S where all tab characters are expanded using spaces.\n",
" | If tabsize is not given, a tab size of 8 characters is assumed.\n",
" | \n",
" | find(...)\n",
" | S.find(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the lowest index in S where substring sub is found,\n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Return -1 on failure.\n",
" | \n",
" | format(...)\n",
" | S.format(*args, **kwargs) -> str\n",
" | \n",
" | Return a formatted version of S, using substitutions from args and kwargs.\n",
" | The substitutions are identified by braces ('{' and '}').\n",
" | \n",
" | format_map(...)\n",
" | S.format_map(mapping) -> str\n",
" | \n",
" | Return a formatted version of S, using substitutions from mapping.\n",
" | The substitutions are identified by braces ('{' and '}').\n",
" | \n",
" | index(...)\n",
" | S.index(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the lowest index in S where substring sub is found, \n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Raises ValueError when the substring is not found.\n",
" | \n",
" | isalnum(...)\n",
" | S.isalnum() -> bool\n",
" | \n",
" | Return True if all characters in S are alphanumeric\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isalpha(...)\n",
" | S.isalpha() -> bool\n",
" | \n",
" | Return True if all characters in S are alphabetic\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isdecimal(...)\n",
" | S.isdecimal() -> bool\n",
" | \n",
" | Return True if there are only decimal characters in S,\n",
" | False otherwise.\n",
" | \n",
" | isdigit(...)\n",
" | S.isdigit() -> bool\n",
" | \n",
" | Return True if all characters in S are digits\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isidentifier(...)\n",
" | S.isidentifier() -> bool\n",
" | \n",
" | Return True if S is a valid identifier according\n",
" | to the language definition.\n",
" | \n",
" | Use keyword.iskeyword() to test for reserved identifiers\n",
" | such as \"def\" and \"class\".\n",
" | \n",
" | islower(...)\n",
" | S.islower() -> bool\n",
" | \n",
" | Return True if all cased characters in S are lowercase and there is\n",
" | at least one cased character in S, False otherwise.\n",
" | \n",
" | isnumeric(...)\n",
" | S.isnumeric() -> bool\n",
" | \n",
" | Return True if there are only numeric characters in S,\n",
" | False otherwise.\n",
" | \n",
" | isprintable(...)\n",
" | S.isprintable() -> bool\n",
" | \n",
" | Return True if all characters in S are considered\n",
" | printable in repr() or S is empty, False otherwise.\n",
" | \n",
" | isspace(...)\n",
" | S.isspace() -> bool\n",
" | \n",
" | Return True if all characters in S are whitespace\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | istitle(...)\n",
" | S.istitle() -> bool\n",
" | \n",
" | Return True if S is a titlecased string and there is at least one\n",
" | character in S, i.e. upper- and titlecase characters may only\n",
" | follow uncased characters and lowercase characters only cased ones.\n",
" | Return False otherwise.\n",
" | \n",
" | isupper(...)\n",
" | S.isupper() -> bool\n",
" | \n",
" | Return True if all cased characters in S are uppercase and there is\n",
" | at least one cased character in S, False otherwise.\n",
" | \n",
" | join(...)\n",
" | S.join(iterable) -> str\n",
" | \n",
" | Return a string which is the concatenation of the strings in the\n",
" | iterable. The separator between elements is S.\n",
" | \n",
" | ljust(...)\n",
" | S.ljust(width[, fillchar]) -> str\n",
" | \n",
" | Return S left-justified in a Unicode string of length width. Padding is\n",
" | done using the specified fill character (default is a space).\n",
" | \n",
" | lower(...)\n",
" | S.lower() -> str\n",
" | \n",
" | Return a copy of the string S converted to lowercase.\n",
" | \n",
" | lstrip(...)\n",
" | S.lstrip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with leading whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | partition(...)\n",
" | S.partition(sep) -> (head, sep, tail)\n",
" | \n",
" | Search for the separator sep in S, and return the part before it,\n",
" | the separator itself, and the part after it. If the separator is not\n",
" | found, return S and two empty strings.\n",
" | \n",
" | replace(...)\n",
" | S.replace(old, new[, count]) -> str\n",
" | \n",
" | Return a copy of S with all occurrences of substring\n",
" | old replaced by new. If the optional argument count is\n",
" | given, only the first count occurrences are replaced.\n",
" | \n",
" | rfind(...)\n",
" | S.rfind(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the highest index in S where substring sub is found,\n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Return -1 on failure.\n",
" | \n",
" | rindex(...)\n",
" | S.rindex(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the highest index in S where substring sub is found,\n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Raises ValueError when the substring is not found.\n",
" | \n",
" | rjust(...)\n",
" | S.rjust(width[, fillchar]) -> str\n",
" | \n",
" | Return S right-justified in a string of length width. Padding is\n",
" | done using the specified fill character (default is a space).\n",
" | \n",
" | rpartition(...)\n",
" | S.rpartition(sep) -> (head, sep, tail)\n",
" | \n",
" | Search for the separator sep in S, starting at the end of S, and return\n",
" | the part before it, the separator itself, and the part after it. If the\n",
" | separator is not found, return two empty strings and S.\n",
" | \n",
" | rsplit(...)\n",
" | S.rsplit(sep=None, maxsplit=-1) -> list of strings\n",
" | \n",
" | Return a list of the words in S, using sep as the\n",
" | delimiter string, starting at the end of the string and\n",
" | working to the front. If maxsplit is given, at most maxsplit\n",
" | splits are done. If sep is not specified, any whitespace string\n",
" | is a separator.\n",
" | \n",
" | rstrip(...)\n",
" | S.rstrip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with trailing whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | split(...)\n",
" | S.split(sep=None, maxsplit=-1) -> list of strings\n",
" | \n",
" | Return a list of the words in S, using sep as the\n",
" | delimiter string. If maxsplit is given, at most maxsplit\n",
" | splits are done. If sep is not specified or is None, any\n",
" | whitespace string is a separator and empty strings are\n",
" | removed from the result.\n",
" | \n",
" | splitlines(...)\n",
" | S.splitlines([keepends]) -> list of strings\n",
" | \n",
" | Return a list of the lines in S, breaking at line boundaries.\n",
" | Line breaks are not included in the resulting list unless keepends\n",
" | is given and true.\n",
" | \n",
" | startswith(...)\n",
" | S.startswith(prefix[, start[, end]]) -> bool\n",
" | \n",
" | Return True if S starts with the specified prefix, False otherwise.\n",
" | With optional start, test S beginning at that position.\n",
" | With optional end, stop comparing S at that position.\n",
" | prefix can also be a tuple of strings to try.\n",
" | \n",
" | strip(...)\n",
" | S.strip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with leading and trailing\n",
" | whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | swapcase(...)\n",
" | S.swapcase() -> str\n",
" | \n",
" | Return a copy of S with uppercase characters converted to lowercase\n",
" | and vice versa.\n",
" | \n",
" | title(...)\n",
" | S.title() -> str\n",
" | \n",
" | Return a titlecased version of S, i.e. words start with title case\n",
" | characters, all remaining cased characters have lower case.\n",
" | \n",
" | translate(...)\n",
" | S.translate(table) -> str\n",
" | \n",
" | Return a copy of the string S in which each character has been mapped\n",
" | through the given translation table. The table must implement\n",
" | lookup/indexing via __getitem__, for instance a dictionary or list,\n",
" | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n",
" | this operation raises LookupError, the character is left untouched.\n",
" | Characters mapped to None are deleted.\n",
" | \n",
" | upper(...)\n",
" | S.upper() -> str\n",
" | \n",
" | Return a copy of S converted to uppercase.\n",
" | \n",
" | zfill(...)\n",
" | S.zfill(width) -> str\n",
" | \n",
" | Pad a numeric string S with zeros on the left, to fill a field\n",
" | of the specified width. The string S is never truncated.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods defined here:\n",
" | \n",
" | maketrans(x, y=None, z=None, /)\n",
" | Return a translation table usable for str.translate().\n",
" | \n",
" | If there is only one argument, it must be a dictionary mapping Unicode\n",
" | ordinals (integers) or characters to Unicode ordinals, strings or None.\n",
" | Character keys will be then converted to ordinals.\n",
" | If there are two arguments, they must be strings of equal length, and\n",
" | in the resulting dictionary, each character in x will be mapped to the\n",
" | character at the same position in y. If there is a third argument, it\n",
" | must be a string, whose characters will be mapped to None in the result.\n",
"\n"
]
}
],
"source": [
"help(str) ## dir(s1)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcdef : 6'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'{} : {}'.format(s1,len(s1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## if\n",
"<pre>\n",
"if condition:\n",
" code\n",
"[elif:\n",
" code]\n",
"else:\n",
" code\n",
"</pre>"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n"
]
}
],
"source": [
"a = True\n",
"if a:\n",
" print('a')\n",
"else:\n",
" print('b')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n"
]
}
],
"source": [
"c = 'a' if a else 'b'\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## for\n",
"<pre>\n",
"for i in range(count):\n",
" code\n",
"</pre>"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 10)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(1, 10, 2)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(1,10,2)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Count : 0\n",
"Count : 1\n",
"Count : 2\n",
"Count : 3\n",
"Count : 4\n",
"Count : 5\n",
"Count : 6\n",
"Count : 7\n",
"Count : 8\n",
"Count : 9\n"
]
}
],
"source": [
"for i in range(10):\n",
" print('Count : {}'.format(i))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Count : 1\n",
"Count : 3\n",
"Count : 5\n",
"Count : 7\n",
"Count : 9\n"
]
}
],
"source": [
"for i in range(1,10,2):\n",
" print('Count : {}'.format(i))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[range(0, 10)]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[range(10)]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i for i in range(10)]# if i % 2 == 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 연습문제\n",
"<pre>\n",
" * \n",
" ***\n",
" *****\n",
" *******\n",
" *********\n",
"***********\n",
" *********\n",
" *******\n",
" *****\n",
" ***\n",
" *\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## function/method\n",
"<pre>\n",
"def name(args...):\n",
" code\n",
" return ...\n",
"</pre>\n",
"- default argument\n",
"- lambda : map, filter, reduce"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"def print_pretty(s):\n",
" print(\"{} {} {}\".format('*'*10,s,'*'*10))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"********** a **********\n"
]
}
],
"source": [
"print_pretty('a')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def many_args(a1,a2,a3=1):\n",
" print(a1,a2,a3)\n",
" return a1+a2+a3"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 1\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"many_args(1,2)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "many_args() missing 1 required positional argument: 'a2'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-33-772c4fd13f8a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmany_args\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: many_args() missing 1 required positional argument: 'a2'"
]
}
],
"source": [
"many_args(1)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3\n"
]
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"many_args(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "non-default argument follows default argument (<ipython-input-35-9ae96094a8cc>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-35-9ae96094a8cc>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m def many_args(a1,a2=1,a3):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
]
}
],
"source": [
"def many_args(a1,a2=1,a3):\n",
" print(a1,a2,a3)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"def fun1(x):\n",
" if x%2 == 0:\n",
" return x\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 0, 4, 0, 6, 0, 8, 0, 10]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"list(map(lambda x: x if x%2==0 else 0,ss))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(lambda x: x%2==0,ss))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 0, 4, 0, 6, 0, 8, 0, 10]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i if i % 2 == 0 else 0 for i in ss]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i for i in ss if i%2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"55"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"reduce(lambda x,y: x+y,ss)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3628800"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reduce(lambda x,y: x*y,ss)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"def use_def(a,f):\n",
" return f(a)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"use_def(1,lambda x:x*100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## global\n",
"- 전역변수"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"def local_fn():\n",
" x = 3\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"x = 10\n",
"local_fn()"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"def global_fn():\n",
" global x\n",
" x = 20\n",
" local_fn()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"global_fn()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list, dict\n",
"- list : 배열\n",
"- dict : dictionary"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class list in module builtins:\n",
"\n",
"class list(object)\n",
" | list() -> new empty list\n",
" | list(iterable) -> new list initialized from iterable's items\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __add__(self, value, /)\n",
" | Return self+value.\n",
" | \n",
" | __contains__(self, key, /)\n",
" | Return key in self.\n",
" | \n",
" | __delitem__(self, key, /)\n",
" | Delete self[key].\n",
" | \n",
" | __eq__(self, value, /)\n",
" | Return self==value.\n",
" | \n",
" | __ge__(self, value, /)\n",
" | Return self>=value.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __getitem__(...)\n",
" | x.__getitem__(y) <==> x[y]\n",
" | \n",
" | __gt__(self, value, /)\n",
" | Return self>value.\n",
" | \n",
" | __iadd__(self, value, /)\n",
" | Implement self+=value.\n",
" | \n",
" | __imul__(self, value, /)\n",
" | Implement self*=value.\n",
" | \n",
" | __init__(self, /, *args, **kwargs)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __le__(self, value, /)\n",
" | Return self<=value.\n",
" | \n",
" | __len__(self, /)\n",
" | Return len(self).\n",
" | \n",
" | __lt__(self, value, /)\n",
" | Return self<value.\n",
" | \n",
" | __mul__(self, value, /)\n",
" | Return self*value.n\n",
" | \n",
" | __ne__(self, value, /)\n",
" | Return self!=value.\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | __reversed__(...)\n",
" | L.__reversed__() -- return a reverse iterator over the list\n",
" | \n",
" | __rmul__(self, value, /)\n",
" | Return self*value.\n",
" | \n",
" | __setitem__(self, key, value, /)\n",
" | Set self[key] to value.\n",
" | \n",
" | __sizeof__(...)\n",
" | L.__sizeof__() -- size of L in memory, in bytes\n",
" | \n",
" | append(...)\n",
" | L.append(object) -> None -- append object to end\n",
" | \n",
" | clear(...)\n",
" | L.clear() -> None -- remove all items from L\n",
" | \n",
" | copy(...)\n",
" | L.copy() -> list -- a shallow copy of L\n",
" | \n",
" | count(...)\n",
" | L.count(value) -> integer -- return number of occurrences of value\n",
" | \n",
" | extend(...)\n",
" | L.extend(iterable) -> None -- extend list by appending elements from the iterable\n",
" | \n",
" | index(...)\n",
" | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n",
" | Raises ValueError if the value is not present.\n",
" | \n",
" | insert(...)\n",
" | L.insert(index, object) -- insert object before index\n",
" | \n",
" | pop(...)\n",
" | L.pop([index]) -> item -- remove and return item at index (default last).\n",
" | Raises IndexError if list is empty or index is out of range.\n",
" | \n",
" | remove(...)\n",
" | L.remove(value) -> None -- remove first occurrence of value.\n",
" | Raises ValueError if the value is not present.\n",
" | \n",
" | reverse(...)\n",
" | L.reverse() -- reverse *IN PLACE*\n",
" | \n",
" | sort(...)\n",
" | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data and other attributes defined here:\n",
" | \n",
" | __hash__ = None\n",
"\n"
]
}
],
"source": [
"help(list)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll = [1,2,3,4,5]\n",
"len(ll)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll[3]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-54-e8b54f3ce9ae>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mll\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"ll[5]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 7]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll.append(7)\n",
"ll"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 7, 0, 1, 2]"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll.extend(range(3))\n",
"ll"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, [1, 2, 3, 4, 5, 7, 0, 1])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = ll.pop()\n",
"a, ll"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 4]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll[2:4]"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 0, 7, 5, 4, 3, 2, 1]"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 1, 2, 3, 4, 5, 7]"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(ll)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 7, 0, 1]"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 1, 2, 3, 4, 5, 7]"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll.sort()\n",
"ll"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7, 5, 4, 3, 2, 1, 1, 0]"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ll[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 1, 3, 4, 5, 7]"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"del(ll[3])\n",
"ll"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(10))"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2], [3, 4]]"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"double_ll = [[1,2],[3,4]]\n",
"double_ll"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"double_ll[0][1]"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0\n",
"1 1\n",
"2 1\n",
"3 3\n",
"4 4\n",
"5 5\n",
"6 7\n"
]
}
],
"source": [
"for c,i in enumerate(ll):\n",
" print(c,i)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"a1 = [90, 85, 95, 80, 90, 100, 85, 75, 85, 80]\n",
"a2 = [95, 90, 90, 90, 95, 100, 90, 80, 95, 90]"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 (90, 95)\n",
"1 (85, 90)\n",
"2 (95, 90)\n",
"3 (80, 90)\n",
"4 (90, 95)\n",
"5 (100, 100)\n",
"6 (85, 90)\n",
"7 (75, 80)\n",
"8 (85, 95)\n",
"9 (80, 90)\n"
]
}
],
"source": [
"for i,aa in enumerate(zip(a1,a2)):\n",
" print(i,aa)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<zip at 0x153fe870f08>"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zip(a1,a2)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<enumerate at 0x153fe86f798>"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"enumerate(zip(a1,a2))"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 0),\n",
" (1, 1),\n",
" (2, 2),\n",
" (3, 3),\n",
" (4, 4),\n",
" (5, 5),\n",
" (6, 6),\n",
" (7, 7),\n",
" (8, 8),\n",
" (9, 9)]"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(enumerate(range(10)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 연습문제\n",
"학생 5명의 성적 리스트로 전체 평균 구하기\n",
"<pre>\n",
"X = [[85, 90, 20, 50, 60, 25, 30, 75, 40, 55],\n",
" [70, 100, 70, 70, 55, 75, 55, 60, 40, 45],\n",
" [25, 65, 15, 25, 20, 5, 60, 70, 35, 10],\n",
" [80, 45, 80, 40, 75, 35, 80, 55, 70, 90],\n",
" [35, 50, 75, 25, 35, 70, 65, 50, 70, 10]]\n",
"</pre>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"help(dict)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'red': 1, 'blue': 2, 'green': 3}"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd = {'red':1,'blue':2,'green':3}\n",
"dd"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd['blue']"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 'red', 2: 'blue', 3: 'green'}"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd = {1:'red',2:'blue',3:'green'}\n",
"dd"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'green'"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd[3]"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "4",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-80-221c602e6019>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdd\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m: 4"
]
}
],
"source": [
"dd[4]"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 'red', 2: 'blue', 3: 'green', 4: 'black'}"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd[4] = 'black'\n",
"dd"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 in dd"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys([1, 2, 3, 4])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd.keys()"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"for k in dd:\n",
" print(k)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([(1, 'red'), (2, 'blue'), (3, 'green'), (4, 'black')])"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd.items()"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 red\n",
"2 blue\n",
"3 green\n",
"4 black\n"
]
}
],
"source": [
"for k,v in dd.items():\n",
" print(k,v)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['red', 'blue', 'green', 'black'])"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd.values()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment