sábado, 5 de enero de 2019

Repaso en PYTHON

CAPITULOS 1 Y 2 HEAD FIRST



movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

print(movies)

for each_item in movies:
    print(each_item)

SALIDA:
C:\code\chapter1>python page19.py
['The Holy Grail', 1975, 'Terry Jones & Terry Gilliam', 91, ['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]]
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]




NESTED ITEM


movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 

                ["Graham Chapman", ["Michael Palin", "John Cleese",

                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]
for each_item in movies:
    if isinstance(each_item, list):
        for nested_item in each_item:
            print(nested_item)
    else:
        print(each_item)


Salida

C:\code\chapter1>python page23.py
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']



movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

for each_item in movies:
if isinstance(each_item, list):
    for nested_item in each_item:
        if isinstance(nested_item, list):
            for deeper_item in nested_item:
            print(deeper_item)
            else:
                print(nested_item)
else:
    print(each_item)

C:\code\chapter1>python page25.py
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']


SACA TODOS LOS ITEMS

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]


def print_lol(a_list):
    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)
            

print_lol(movies)



C:\code\chapter1>python page30.py
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones

C:\code\chapter1>



DEFINIENDO FUNCIONES CON DEF


"""This is the "nester.py" module and it provides one function called print_lol() 
   which prints lists that may or may not include nested lists."""

def print_lol(the_list):
    """This function takes one positional argument called "the_list", which 
        is any Python list (of - possibly - nested lists). Each data item in the 
        provided list is (recursively) printed to the screen on it’s own line."""

    for each_item in the_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)


********
BUILD YOUR DISTRIBUTION

from distutils.core import setup

setup( 
        name         = 'nester', 
        version      = '1.0.0', 
        ## TODO: be sure to change these next few lines to match your details!
        py_modules   = ['nester'],
        author       = 'hfpython',
        author_email = 'hfpython@headfirstlabs.com',
        url          = 'http://www.headfirstlabs.com',
        description  = 'A simple printer of nested lists',
     )



salida
C:\code\chapter2>python page40.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating nester-1.0.0
copying files to nester-1.0.0...
copying nester.py -> nester-1.0.0
copying page40.py -> nester-1.0.0
creating dist
creating 'dist\nester-1.0.0.zip' and adding 'nester-1.0.0' to it
adding 'nester-1.0.0\nester.py'
adding 'nester-1.0.0\page40.py'
adding 'nester-1.0.0\PKG-INFO'
removing 'nester-1.0.0' (and everything under it)


CAMBIO DE DIRECTORIO DE TRABAJO

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> os.getcwd()
'C:\\Users\\robertoperez\\AppData\\Local\\Programs\\Python\\Python37-32'
>>> os.chdir('C:\code\chapter2')
>>> os.getcwd
<built-in function getcwd>
>>> os.getcwd()
'C:\\code\\chapter2'

**************

Nester.py

def print_lol(the_list):

    for each_item in the_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)


FILE-> OPEN-> Nester.py  ASI LO AGREGAS A LA MEMORIA

==================== RESTART: C:\code\chapter2\Nester.py ====================
>>> import nester
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    import nester
ModuleNotFoundError: No module named 'nester'
>>> 
>>> import Nester
>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]
>>> Nester.print_lol(movies,0)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    Nester.print_lol(movies,0)
TypeError: print_lol() takes 1 positional argument but 2 were given
>>> Nester.print_lol(movies)
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
>>> 
==================== RESTART: C:\code\chapter2\nester2.py ====================
>>> import nester2
>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]
>>> nester2.print_lol(movies,0)
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
>>> nester2.print_lol(movies,1)
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
>>>  nester2.print_lol(movies,-9)
SyntaxError: unexpected indent
>>> nester2.print_lol(movies,2)
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
>>> 


def print_lol(a_list, level=0):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""

    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item, level+1)
        else:
            for l in range(level):
                print("\t", end='')
            print(each_item)



def print_lol(a_list, indent=False, level=0):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""

    for each_item in a_list:
        if isinstance(each_item, list):
            print_lol(each_item, indent, level+1)
        else:
            if indent:
                for l in range(level):
                    print("\t", end='')
            print(each_item)


tab en la salida es el rango


Say "Hello, World!" With Python 

Here is a sample line of code that can be executed in Python:
print("Hello, World!")
You can just as easily store a string as a variable and then print it to stdout:
my_string = "Hello, World!"
print(my_string)
The above code will print Hello, World! on your screen. Try it yourself in the editor below!
Input Format
You do not need to read any input in this challenge.
Output Format
Print Hello, World! to stdout.
Sample Output
Hello, World!
PROGRAMA
if __name__ == '__main__':
    print "Hello, World!"

No hay comentarios:

Publicar un comentario

zen consultora

Blogger Widgets

Entrada destacada

Platzy y el payaso Freddy Vega, PLATZI APESTA, PLATZI NO SIRVE, PLATZI ES UNA ESTAFA

  Platzy y los payasos fredy vega y cvander parte 1,  PLATZI ES UNA ESTAFA Hola amigos, este post va a ir creciendo conforme vaya escribiend...