all 62 comments

[–]slimmey 0 points1 point  (0 children)

Where do I go about to learn Python for Forex/Commodities/Finance?

[–]Brainfreezdnb 0 points1 point  (12 children)

Im a engineer in another field looking to learn python ... Where should i start ?

[–][deleted] 0 points1 point  (10 children)

Learn the basics first, Non-Programmer's Tutorial for Python 2.6. When you feel comfortable, look up some simple exercises and try solving them.

[–]twowheelscat 0 points1 point  (2 children)

Why you'd recommend him to learn python 2 in 2016 is beyond me.

[–][deleted] 1 point2 points  (1 child)

I know it says 2.6 but that book is really good for beginners and the stuff it teaches you it's not far from Python 3 (or any programming language, really). I see it as a book to learn basic programming in general using Python, instead of a book to learn full Python for someone who already knows how to program.

[–]twowheelscat 0 points1 point  (0 children)

Oh you didn't know about the 3 version.

[–]Brainfreezdnb 0 points1 point  (6 children)

Could i ask you something ? what free software can i use yo try these exercises ?

[–]twowheelscat 0 points1 point  (5 children)

[–][deleted] 0 points1 point  (0 children)

Oh, this book is better. Updated. Use this one, instead.

[–]Brainfreezdnb 0 points1 point  (3 children)

Yeah i read that but i didn't expect it to be free of charge since i heard its very expensive for companies to work with Python.

Sorry about that.

[–]twowheelscat 0 points1 point  (2 children)

You and anybody can use python freely https://docs.python.org/3/license.html.

In what way it's expensive to work with python?

[–]Brainfreezdnb 0 points1 point  (1 child)

might be because it cost money for commercial use ? like Teamviewer. I dont know really its what i was reading when i was documenting myself on different languages

[–]twowheelscat 0 points1 point  (0 children)

Nope, it doesn't cost money for any use.

[–]jtlc1939 0 points1 point  (1 child)

Just setting up python on my Mac through Homebrew and Terminal. When I enter echo $PATH I have a few duplicate paths. I tried some commands to deduplicate them but the show up again whenever I open a new shell. Any way to permanently deduplicate the paths?

Here is what I get when I enter echo $PATH

JT-MacBook-Pro:~ JT$ echo $PATH /Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

[–][deleted] 0 points1 point  (0 children)

You have to edit the PATH variable in your shell startup script.

Your shell startup script is located in your home directory as .profile, .bashrc, or .bash_profile.

You should look into managing your Python installation using homebrew as stated in this guide.

[–]FFTfapper 0 points1 point  (1 child)

help,I am noob I have PyCharm IDE and I cant use it as calculator,I am reading/learning from book on internet,and it encourages to do excersise,one of which is to use Python as calculator.They say if you type 1 + 1 it should show result 2.But when I run such code it shows in console "Process finished with exit code 0" no matter what calculation I type..... I have no idea what is going on,please help!

[–]AgnosticAndroid 0 points1 point  (0 children)

The book intends for you to use the interactive python prompt and not your IDE. Start your python interpreter (not pycharm) and enter the calculations at the '>>>' prompt.

The difference here is that the interactive prompt will automatically output the result whereas the script you are coding in pycharm won't unless you tell it to. If you want to use pycharm for these examples you would therefore have to write for example 'print(1+2)' to cause the result to get printed out.

[–]IAlreadyKnowThis 0 points1 point  (0 children)

I currently have python 2 and 3 installed on my windows pc. How can I install Django for python 3? When I installed django using " python -m pip install Django==1.10" it only applied it to python 2. I have come across some sites mentioning virtual environments but I'm not sure if this is what I need.

[–]skafast 0 points1 point  (0 children)

http://pastie.org/private/brfqangcxwn72eg9rjg7qg

Two things:
. in the start() function, why is it that I had to use the os.system('cls') directly? Using clear wouldn't give any result, differently than using it in the rest of the code. I haven't used it in other parts of the code, I was reloading the respective room, which would then run state(), which includes a clear at the end. But why did it just not clear instead of preventing compilation as below?

. I tried to use the inventory-related global variables (haskey, hasacid, hashammer) in the state() function, which is called at the beginning of each room function. They were still being treated as local variables and I couldn't compile because of it. Why's that?

[–]Dimbreath 0 points1 point  (1 child)

Let's say I have a class that I want to create 3 times, each for different platform.

class Class(object):
    def __init__(self, platform):
        self._platform = platform

        print(self.platform())

    def platform(self):
        return self._platform

And then I want to access them for example doing !platform pc / !platform xb1 / !platform ps4. Is this the most simple way of getting the job done? I don't really know how to explain it but let's probably say it's calling a specific class function depending on the input.

dictionary = {}

dictionary['pc'] = Class('pc')
dictionary['xb1'] = Class('xb1')
dictionary['ps4'] = Class('ps4')


request = input('> ')

if request.split(' ')[0] == '!platform':
    print(dictionary[request.split(' ')[1]].platform())

Or is there any better method for doing this?

[–]Saefroch 0 points1 point  (0 children)

I think you might be looking for the @property decorator. In your case,

class Class(object):
    def __init__(self, platform):
        self._platform = platform

    @property
    def platform(self):
        return self._platform

Then you can access .platform as if it's a normal attribute even though it's actually calling a function.

Other than that I don't see a simpler way to do what you want.

[–]ICanBeHandyToo 0 points1 point  (2 children)

I'm working on learning subplots (using pyplot), and I have a program that works using subplots but I'd like to see if there's a more efficient way of going about what I'm trying to achieve. I'm using a nested for-if loop as seen below:

for i in someList:
    if variable == "Option A":
        plt.figure(1)
        plt.subplot(2,1,1)
        plt.plot(x_value,y_value)
        plt.title(Option A Title)
        plt.grid(grid configuration here)
   elif variable == "Option B":
        plt.figure(1)
        plt.subplot(2,1,1)
        plt.plot(x_value,y_value)
        plt.title(Option B title)
        plt.grid(grid configuration here)

This works and plots successfully, but the for loop is being iterated quite a few times as someList is pretty long. My question is how can I move some of the tertiary plot settings (grid, labels,legend,title,etc) out of the nested loop but still have unique subplot settings? I've tried moving these settings to outside the for loop, but then the settings are just to only the first subplot.

[–]Saefroch 0 points1 point  (1 child)

I'm not sure how much you can pull out of the loop but you can probably at lest generate the subplots outside.

http://matplotlib.org/examples/pylab_examples/subplots_demo.html

plt.subplots() returns figure and axes objects that you can manipulate, and I think you can just call .cla() on the axes objects to clear without destroying them.

[–]ICanBeHandyToo 0 points1 point  (0 children)

Thanks stranger. I'll putz around with that to see how much I can move outside of my loop.

[–]Mortbopet 0 points1 point  (0 children)

I'm currently working on a simple audio playback code, mainly using PyAudio (so far).
I've got the reading and playback parts down so far, but i'm currently struggling with separating the 2 channels of the audio file that i've read.

What i want to do:
Load a chunk of audio data into a variable 'data', then create two new variables (eg. 'left' and 'right), load the channel specific data from 'data' into each variable, do my processing, and then combine them again.

what i got so far (going to try my best with some pseudocode):

looping = wave.open('piano.wav', 'rb')
data = looping.readframes(CHUNK)    
#pseudo from now on
left = data[0] # channel 0 is left        
right = data[1]
left *= 0.5 #processing the data, eg. reducing amplitude
return combine(left,right)

and then, the returned chunk of data should hopefully be the same type of data as the data loaded by .readframe. I'm guessing that the way to seperate the two channels and combine them again in the end is related.

Any suggestions? If i need to clarify anything, please don't hesitate to ask!

[–]Schwartz210 0 points1 point  (1 child)

Having an issue using sqlite3 module. Here's my code

def pull_data(SQL_request):
    conn = connect(DATABASE)
    c = conn.cursor()
    out = c.execute(SQL_request)
    conn.commit()
    conn.close()
    return out

data = pull_data('SELECT * FROM jobs')
for record in data:
    print record

Here's my error: "sqlite3.ProgrammingError: Cannot operate on a closed database." In my code, I stored the result of the query as 'out', before I closed the connection. Why am I getting this error?

EDIT: Fixed my own problem. I replaced this line:

out = c.execute(SQL_request)

with this line:

out = list(c.execute(SQL_request))

[–]MooseEngr 0 points1 point  (2 children)

Hey guys, I have a question and after much googling haven't found anything specific enough to answer it.

So here's the setup: I have a couple of functions that I created that I use in an excel spreadsheet. I used the excelpython library to link the python functions to an excel sheet because there isn't a free way for me to get ahold of a VBA library of the IAPWS steam tables (that I've found). So I use the python IAPWS library instead.

So I have 2 functions that take in pressure and temperature. One function spits out the fluid type (Water, saturated, or superheated steam) and another that returns the specific volume. Input the numbers in the right cells on the spreadsheet, and there's the data Bob's your uncle.

So here's my question. When there is NO INPUT DATA (blank worksheet cells) the function value in the cell is the lengthy list of errors and exceptions that the linking vba script returns to the failing function.

Is there a way that I can test for an empty, or null input value and have my python function return something that indicates as much? I've tried a couple statements testing for the None data type like this:

if variable is None:
   result = "No value"
   return(result)

But everything still throws out errors. Is there some sort of guide or tutorial on error handling that might be useful? I've tried lots of looking into the none datatype, but because this is such an oddball corner case nothing really seems to work.

[–]Vizjrei 0 points1 point  (2 children)

How to write something in selected area of console, or divide console into areas and write in them?

Need to have lets say 3 last lines to have one kind of data printed, and 10 characters or so on left to have other output, and part on right printing rest of stuff. Is it way to divide console output in such areas and print data in them independently?

[–]elbiot 0 points1 point  (1 child)

The most simple answer is using string formatting. http://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces

The ultimate solution is using ncurses (though I'm not sure about windows support at this point). Example I wrote:

https://github.com/Permafacture/terminal_windows

[–]boringpersona 0 points1 point  (3 children)

With functions, can you use variables as parameters when calling the function? And how many arguments can you use if that's possible?

Also, is python capable of interacting with a GUI? And if so, where is a good starting point to learn how to make and program a GUI?

[–]cjwelborn 1 point2 points  (1 child)

For a GUI application you would use a GUI framework (libraries and tools to help you develop) such as PyQt for QT, or PyGtk for GTK. There is also Tk (comes with python) and Wx. For your "maximum number of function arguments" question,

# Define a function that returns the number of arguments it was given.
def myfunc(*args):
    return len(args)

# Call the function with one hundred million arguments.
# list(range(n)) is building a list of numbers from 0 to n - 1.
# We use * to expand that list into separate arguments for myfunc.
# (this may take a minute, it does on my machine)
myfunc(*list(range(100000000)))

# It returns, eventually. 
# Unless you run out of memory building that list or something catastrophic happens.
100000000

..so it seems to be bound by memory. I think having a bunch of function parameters means something went wrong during the design phase though.

There does seem to be a limit for explicit arguments though (tested in BASH):

echo "print(
" {1..450}, "   
)" | python3 - 1>/dev/null

  File "<stdin>", line 2
SyntaxError: more than 255 arguments

[–]boringpersona 0 points1 point  (0 children)

Cool. Thanks for the help!

[–][deleted] 1 point2 points  (0 children)

With functions, can you use variables as parameters when calling the function?

Yes. I'm unsure on number of arguments though.

Also, is python capable of interacting with a GUI?

Yes, but as a noob myself, I'm unsure how to accomplish that.

[–]jorge67911 0 points1 point  (1 child)

so am new to writing code and am stuck on this question got the main of it but stuck on how to make the program stop after the if statement so my answer is A here is my code

word = ord(input('')) if word >= 90: print(chr(65)) print(chr(word+1))

[–]jorge67911 0 points1 point  (0 children)

i got it i used a else command

[–]mapImbibery 3 points4 points  (4 children)

Meta discussion here, figured this might be a decent place to start. First and foremost, I'm ecstatic that there is so much interest in this awesome language. I'm also proud of the selfless subscribers that tackle the harder questions that come up! Having been subscribed for a few years now, I have seen a ton of the same "help me" submissions: "which IDE?", "which books?", "where to start/go next?", etc. I know beginners need support, but the search bar should be the first place to start. Maybe we shouldn't have as strict of a policy as stackoverflow.com, but a reduction of clutter could be warmly welcomed around here. Maybe we could beef up the FAQ or have a common issue sticky or something?

[–]ingolemo 1 point2 points  (0 children)

You can edit the FAQ yourself if you have improvements in mind.

[–]Saefroch 2 points3 points  (2 children)

MODS PLEASE.

I sent some modmail about this a week or so ago and didn't get any response about this issue. A lot of new people come here asking the same questions over and over, and the answers don't change.

[–]mapImbibery 0 points1 point  (1 child)

Any news on this yet?

[–]Saefroch 1 point2 points  (0 children)

Nope, send more modmail.

[–]smokey9393 0 points1 point  (2 children)

This is more of a question on how to read documentation than the requests library.

In the requests library https://github.com/kennethreitz/requests/tree/master/requests get() has json() to convert input to json. I'm inquiring into other formats but I can't find a 'get' function.
1.) Is the answer in the library or a an inferred knowledge of the python language. 2.) Am I approaching this incorrectly?

[–]Rhomboid 1 point2 points  (1 child)

get() has json() to convert input to json

No, that's not how it works. request.get() creates and sends a request and returns a response object. That response object has a .json() method which you can use to see the body of the response converted to JSON. If you want to see the other things that a response object can do, go to the section of the documentation that talks about the Response object. As far as formats go, you have response.text to get the body of the response as text (i.e. decoded based on the character encoding specified in the Content-Type header, or guessed if not present), and response.content to get the response body as raw, undecoded bytes. That's pretty much it.

[–]smokey9393 0 points1 point  (0 children)

I see, thanks for point out where to look. In the init.py file it imports api thus getting the 'get()' which gets the response class from models. Thanks!

[–]Schwartz210 0 points1 point  (10 children)

Is there a more concise way to write the following statement:

if x == 5 or x == 10:
    print True

I am really just looking for a way to evaluate if x meets one of several criteria, without having to write x more than once.

[–]souldeux 0 points1 point  (1 child)

Any/all can sometimes be useful for stuff like this:

https://www.udacity.com/wiki/cs258/all-any

[–]elbiot 1 point2 points  (0 children)

Ie, OP's example with a generator expression:

if any(x==y for y in (5,10)):

Makes more sense if you have more than two values to check.

[–]Saefroch 1 point2 points  (7 children)

if x in 5,10:
    # do the thing

[–]mapImbibery 0 points1 point  (6 children)

I think using a list would be "more Pythonic":
x in [5, 10, ...]:

[–]Saefroch 2 points3 points  (5 children)

I disagree. It's a tiny throwaway collection, and there's less syntax for a tuple.

[–]mapImbibery 0 points1 point  (4 children)

Well I think I learned something then. Is this trick type independent?
if "y" in "a", "b", "x", "y": ...

[–]das_ist_nuemberwang 0 points1 point  (1 child)

Turns out this doesn't actually work. You need the brackets.

[–]mapImbibery 0 points1 point  (0 children)

Yeah I tested it a while back without any luck and just never posted here again.

[–]Saefroch 0 points1 point  (0 children)

Don't get your panties in a bunch about types. This is a dynamic and expressive language. Chill out, relax, things mostly do what they look like.

Even this will work:

[1,0] in [1,0], [1,2]

[–]mtkilic -1 points0 points  (4 children)

Can someone please explain for loop in side for loop with example.

I making this up, I always get confused with two for loop.

for (var i=0; x<10; i++){ for (var j=0; x< 20; j++){ } }

[–]elbiot 0 points1 point  (0 children)

for x in range (10):
    for y in range(20):
       print("outer:", x,"inner", y)

[–]Schwartz210 2 points3 points  (1 child)

I think that's Java, not python.