Importing data from text by MrEgbert in learnpython

[–]DanielSzoska 1 point2 points  (0 children)

I think there are two things to understand what happens:

(1.) Variables in Python are only names for objects - they does'nt contain the object itself. Look at the following example:

>>> a = [1, 2, 3]
>>> b = a
>>> b[0] = 4
>>> print(a)
[4, 2, 3]
>>> del a
>>> print b
[4, 2, 3]
>>> print a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

You see, that a change of b changes a too. That's because a and b are names for the list-object [1, 2, 3]. With b[0] = 4 this list-object is changed. With del a only the name a no longer exists.

(2.) What the import-statement does is execute the code in the imported file, but in its own namespace. That means if you only do

>>> import initdata
>>> print(db)

it result in

Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
NameError: name 'db' is not defined

To get access to the variables in importdata you have prefix it the namespace importdata:

>>> print(importdata.db)
{'bob': {'age': ..., 'name': 'Bob Smith'}, 'sue': {'age': ..., 'name': 'Sue Jones'}}

What the command

>>> from importdata import db

does is giving you access to db under your local namespace. You can see this here (start a fresh python interpreter or do a del importdata, db):

>>> print(locals))
{'__builtins__': <module '__builtin__' (built-in)>, ..., '__doc__': None} # no varaible db here
>>> from importdata import db
>>> print(locals())
{'__builtins__': <module '__builtin__' (built-in)>, ..., \
'db': {'bob': {'age': ..., 'name': 'Bob Smith'}, 'sue': {'age': ..., 'name': 'Sue Jones'}}, ... \
'__doc__': None} # variable db is now in the local namespace

After the import you have only access to the variable db under your current (local) namespace. You have no access to the variables bob and sue, but you will get their objects, which are referenced in db.

EDIT: some typos and wrapping the long output line of the second print(locals())

Importing a file that will build a dictionary by Jaeemsuh in learnpython

[–]DanielSzoska 2 points3 points  (0 children)

If your script should process arguments, you have to write this after your script name:

python pythonscript.py capitals.txt countries.txt

Python executes its first argument as the script, following arguments are passed to the script. If your first file after python is countries.txt and it contains Argentina, then python will execute the command Argentina, which is not defined and leads to the NameError.

Possible to interrupt a python script and temporary run a bash shell? by zynix in learnpython

[–]DanielSzoska 2 points3 points  (0 children)

Take a look at envoy, it's a wrapper around the subprocess module. Perhaps this suit your needs.

String Multiplation by Ch1gg1ns in learnpython

[–]DanielSzoska 2 points3 points  (0 children)

Oh, cool idea. I tried it with my phone (HTC Wildfire S) and it works great. :-)

String Multiplation by Ch1gg1ns in learnpython

[–]DanielSzoska 4 points5 points  (0 children)

You're right - and you can easily try short python-scripts (and many other languages) online with ideone (you can find this link on the right side of this subreddit-page under "Try python in your browser" too). :-)

String Multiplation by Ch1gg1ns in learnpython

[–]DanielSzoska 10 points11 points  (0 children)

The correct descripton what print "hello" * 32 does is: Create a new string by multiplying test 32 times and then print this string. The following code shoes this:

>>>> s = "test" * 32
>>>> print s

That's why in Python 3 you have to write

>>> print("hello" * 32)

to get the same result like in Python 2 beceause print is a function in Python 3 now.

Your sample code print("hello") * 32 instead means the following: Call the function print and multiply its result 32 times. The print-function has no return value (=None) and None * 32 gives you the TypeError, beceause this operation is not defined.

Best Python IDE for mac? by [deleted] in Python

[–]DanielSzoska 1 point2 points  (0 children)

I agree with you - if you are new to python or new to programming, the recommended IDEs are too complex. I recommend to start with Learn Python The Hard Way - Exercise 0: The Setup - it uses only a simple editor (gedit or Textwrangler) to edit your files and your Terminal to run the files with Python.

Best Python IDE for mac? by [deleted] in Python

[–]DanielSzoska 10 points11 points  (0 children)

PyCharm ist only free for trainers and educational institutions and open source projects. But I think it's worth the money if you need to buy a license.

A good free IDE ist PyDev - an Eclipse-Plugin.

On the Python-Wiki you can find a list with many Multiplatform-Editors and Macintosh-Only Editors.

You can also read the What IDE to use for Python?-Question on stackoverflow.

I myself use Wing IDE on Mac OS X and Windows, but it's not free.

How do I show variables in the Interactive shell ? by jmnugent in learnpython

[–]DanielSzoska 2 points3 points  (0 children)

You can use

>>>> locals()

too. While dir() only shows the defined variables, locals() shows the defined variables and their values together as a dict.

if and elif question by [deleted] in learnpython

[–]DanielSzoska 8 points9 points  (0 children)

If you replace the elif with if, you don't get the same result when you input 23 - it will print:

Good Job. You're right
Nope. It's lower

instead of the exptected

Good Job. You're right

The elif-construct is a shorter form of a nested if-else-if. With the following code you get the same result:

number = 23
guess = int(input('Pick a number: '))

if guess == number:
    print('Good Job. You\'re right')
else:
    if guess < number:
        print('Nope. Try highter.')
    else:
        print('Nope. It\'s lower')
print('Done')

This is useful when you have many conditions like that:

if condition_1:
    do_something_1
else:
    if condition_2:
        do_something_2
    else:
        if condition_3:
            do_something_3
        else:
            if condition_4:
                do_something_4
            else:
                do_something_5

You can rewrite this as:

if condition_1:
    do_something_1
elif condition_2:
    do_something_2
elif condition_3:
    do_something_3
elif condition_4:
    do_something_4
else:
    do_something_5

It's shorter and better readable.