This file contains hidden or 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
| while True: | |
| filename = input('Enter the name of a file to open: ') | |
| try: | |
| # open for reading in binary mode, so any file will do | |
| fin = open(filename, 'rb') | |
| except OSError as exc: | |
| print('Something untoward happened [%r]' % exc) | |
| else: | |
| break |
This file contains hidden or 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
| while True: | |
| response = input('Enter a number: ') | |
| try: | |
| n = float(response) | |
| except ValueError: | |
| print('%r is not a number' % response) | |
| else: | |
| break | |
| print('Nice number. Thanks!') |
This file contains hidden or 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 backtracking_sort(input_list, output_list, level): | |
| print(level * ' ', input_list, output_list); | |
| # Reject - this path doesn't lead to any solution | |
| if len(output_list) > 1 and output_list[-2] > output_list[-1]: | |
| return False | |
| # Accept - one solution |
This file contains hidden or 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
| """ | |
| ========================================================= | |
| Classe ObjetoJS: imitação simples de um objeto JavaScript | |
| ========================================================= | |
| Uma instância é construída passando argumentos nomeados: | |
| >>> o = ObjetoJS(z=33, x=11, y=22) | |
| A representação textual de uma instância parece a chamada do |
This file contains hidden or 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
| """ | |
| romanos.py - arabicos para romanos | |
| >>> ara2rom(1) | |
| 'I' | |
| >>> ara2rom(2) | |
| 'II' | |
| >>> ara2rom(3) | |
| 'III' |
This file contains hidden or 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
| import pingo | |
| from time import sleep | |
| board = pingo.detect.MyBoard() | |
| pin = board.pins[13] | |
| pin.mode = pingo.OUT | |
| led = pingo.parts.Led(pin) | |
| led.blink(10, .5, .1) # new thread |
This file contains hidden or 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
| import pingo | |
| from time import sleep | |
| board = pingo.detect.MyBoard() | |
| led = board.pins[13] | |
| led.mode = pingo.OUT | |
| while True: | |
| led.toggle() | |
| sleep(1) |
This file contains hidden or 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
| # spinner_asyncio.py | |
| # credits: Example by Luciano Ramalho inspired by | |
| # Michele Simionato's multiprocessing example | |
| # source: | |
| # http://python-3-patterns-idioms-test.readthedocs.org/en/latest/CoroutinesAndConcurrency.html | |
| import sys | |
| import asyncio |
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # Copyright 2014 Brett Slatkin, Pearson Education Inc. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # |
This file contains hidden or 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
| # Code below is the expansion of the statement: | |
| # | |
| # RESULT = yield from EXPR | |
| # | |
| # Copied verbatim from the Formal Semantics section of | |
| # PEP 380 -- Syntax for Delegating to a Subgenerator | |
| # | |
| # https://www.python.org/dev/peps/pep-0380/#formal-semantics | |
| _i = iter(EXPR) |