Last active
May 24, 2018 02:40
-
-
Save seven0525/93aa1f8a8e6da1b2ffaefcf129b9bd28 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」7日目(1週間の感想:31本〜40本目) ref: https://qiita.com/ahpjop/items/e852688b0e91eee50e00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#問題のコード | |
short_list = [1,2,3] | |
while True: | |
value = input("Position [q to qui]? ") | |
if value == "q": | |
break | |
positon = int(value) | |
print(short_list[position]) | |
#エラー処理を加えたもの | |
short_list = [1,2,3] | |
while True: | |
value = input("Position [q to qui]? ") | |
if value == "q": | |
breakt | |
try: | |
positon = int(value) | |
print(short_list[position]) | |
except IndexError as err: | |
print("Bad index:", position) | |
except Exception as other: | |
print("something else broke:", other) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#クラス | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.name = name | |
self.symbol = symbol | |
self.number = number | |
#辞書 | |
el_dict = {"name": "Hydrogem", "symbol": "H", "number": 1 } | |
``` | |
#辞書から初期化 | |
hydrogem = Elements(**el_dict) # 辞書を引数に渡すときは「**」 をつける | |
hydrogem.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
odd = {number for number in range(10) if number % 2 != 0} | |
odd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for thing in ("Got %s" % number for number in range(10)): | |
print(thing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_odds(): | |
for number in range(1, 10, 2): | |
yield number | |
for count, number in enumerate(get_odds(), 1): | |
if count == 3: | |
print(number) | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test(func): | |
def new_func(*args, **kwargs): | |
print("start") | |
result = func(*args, **kwargs) | |
print("end") | |
return result | |
return new_func | |
@test | |
def greeting(): | |
print("Hello") | |
greeting() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#問題のコード | |
short_list = [1,2,3] | |
while True: | |
value = input("Position [q to qui]? ") | |
if value == "q": | |
break | |
positon = int(value) | |
print(short_list[position]) | |
#エラー処理を加えたもの | |
short_list = [1,2,3] | |
while True: | |
value = input("Position [q to qui]? ") | |
if value == "q": | |
breakt | |
try: | |
positon = int(value) | |
print(short_list[position]) | |
except IndexError as err: | |
print("Bad index:", position) | |
except Exception as other: | |
print("something else broke:", other) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
titles = ["Creature of Habit", "Crewel Fate"] | |
plots = ["A nun turns into a monster", "A haunted yarn shop"] | |
movies = dict(zip(titles, plots)) | |
movies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#クラス | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.name = name | |
self.symbol = symbol | |
self.number = number | |
#辞書 | |
el_dict = {"name": "Hydrogem", "symbol": "H", "number": 1 } | |
#解答 | |
#辞書から初期化 | |
hydrogem = Elements(**el_dict) # 辞書を引数に渡すときは「**」 をつける | |
hydrogem.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#問題のクラス | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.name = name | |
self.symbol = symbol | |
self.number = number | |
#解答 | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.name = name | |
self.symbol = symbol | |
self.number = number | |
def __str__(self): | |
return ("name: %s, symbol: %s, number: %s" % (self.name, self.symbol, self.number)) | |
el_dict = {"name": "Hydrogem", "symbol": "H", "number": 1 } | |
hydrogem = Elements(**el_dict) | |
print(hydrogem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#問題 | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.__name = name | |
self.__symbol = symbol | |
self.__number = number | |
#解答 | |
class Elements: | |
def __init__(self, name, symbol, number): | |
self.__name = name | |
self.__symbol = symbol | |
self.__number = number | |
@property#属性を非公開にする | |
def name(self): | |
return self.__name | |
def symbol(self): | |
return self.__symbol | |
def number(self): | |
return self.__number | |
el_dict = {"name": "Hydrogem", "symbol": "H", "number": 1 } | |
hydrogem = Elements(**el_dict) | |
hydrogem.name | |
hydrogem.__name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bear: | |
def eats(self): | |
return "berries" | |
class Rabbit: | |
def eats(self): | |
return "clover" | |
class Octothorpe: | |
def eats(self): | |
return "campers" | |
b = Bear() | |
r = Rabbit() | |
o = Octothorpe() | |
print(b.eats()) | |
print(r.eats()) | |
print(o.eats()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment