Bitcoin Forum

Other => Off-topic => Topic started by: gangplank on September 27, 2013, 12:19:30 AM



Title: Programming in Python
Post by: gangplank on September 27, 2013, 12:19:30 AM
Hey,

I've been wanting to learn to program for a while but I never really bothered as I had no idea of what to build and so my motivation was zero. Because of Bitcoin, I have some simple ideas that I'd like to build so now is as good a time as any to learn how to program. To that end, I thought I would share with others a course on python is starting on October 9th titled "An Introduction to Interactive Programming in Python".

https://www.coursera.org/course/interactivepython

I don't know if this is the ideal language to learn if I want to develop my Bitcoin ideas, perhaps someone can offer some advice in that regard but anyway, I thought I would share the link here and see if anyone else was interested in doing this as a group, perhaps we can collaborate and help each other to learn via the forum or on irc in the #interactivepython channel.

Anyone else interested?







Title: Re: Programming in Python
Post by: 01BTC10 on September 27, 2013, 12:22:59 AM
I'm in! I think python is a good multi-purpose language.


Title: Re: Programming in Python
Post by: jackjack on September 27, 2013, 01:21:24 AM
I don't know if this is the ideal language to learn if I want to develop my Bitcoin ideas, perhaps someone can offer some advice in that regard
I learnt Python and Bitcoin by looking/forking this software: https://github.com/joric/pywallet/
I think I'm now pretty good at both
I can help you if you need some help


Title: Re: Programming in Python
Post by: gangplank on September 27, 2013, 02:01:27 AM
Thanks for the offer of assistance.

I'll post this over at /r/bitcoin and see if we can get a small group for the 9th next month.


Title: Re: Programming in Python
Post by: 01BTC10 on September 28, 2013, 12:46:32 AM
I'm now also registered to an initiation course to C++ but it's in french: https://www.coursera.org/course/intro-cpp-fr

I'm already semi-fluent in C++ but only learnt via source code reading and working on small projects. Maybe it will help me to start from the beginning, then there is a part 2 course for more advanced students.  


Title: Re: Programming in Python
Post by: Wipeout2097 on September 28, 2013, 09:59:53 PM
Instead of learning python, learn the OOP then you can easily learn any language. I personally think python is a descent web language. I personally like php, and JavaEE for web, and Objective-c for desktop/ios apps.
This is nitpicking, but python is not a "web language". It's multipurpose and one can code desktop programs with it (and wxPython). I did so, some time ago.


Title: Re: Programming in Python
Post by: moni3z on September 29, 2013, 12:32:18 AM
Do SICP http://mitpress.mit.edu/sicp/

Afterwards you can code in any language, you just need to learn it's syntax which takes a weekend.


Title: Re: Programming in Python
Post by: gangplank on October 10, 2013, 05:17:35 AM
Just a reminder that this course has started (week 0). It's pretty straight forward but we can use this thread to assist one another if needed.


Title: Re: Programming in Python
Post by: termhn on October 10, 2013, 05:40:11 AM
Yeah, if you're looking to do web stuff python probably isn't the most "ideal," BUT python IS a great multi-purpose language. It can be used for so many things, much like C/C++.


Title: Re: Programming in Python
Post by: vite on October 10, 2013, 01:37:53 PM
I took learning a programming language as a hobby and chose python. It has been a great learning experience and really keeps your mind busy.

JackJack has been a great "guidance counselor"

So enjoy your ride...

Still got a lot of learning to do but its "educational entertainment"


Title: Re: Programming in Python
Post by: Peter Lambert on October 10, 2013, 01:44:04 PM
I learned some python a while ago. I found it useful to solve the problems at projecteuler.net (http://projecteuler.net) using python to experiment with different programming and mathematical techniques.


Title: Re: Programming in Python
Post by: dragonkid on October 10, 2013, 01:47:37 PM
Python is a great language, it is a lot quicker to code then any other language out there. It is not too slow compare to C either.


Title: Re: Programming in Python
Post by: FirstAscent on October 10, 2013, 08:29:17 PM
Do SICP http://mitpress.mit.edu/sicp/

Afterwards you can code in any language, you just need to learn it's syntax which takes a weekend.

Incorrect. Oh sure, you're going to learn Lisp, C++, Haskell and Prolog in a weekend just by learning its syntax.


Title: Re: Programming in Python
Post by: FirstAscent on October 10, 2013, 08:40:41 PM
Python is a great language, it is a lot quicker to code then any other language out there. It is not too slow compare to C either.

It's very slow compared to C. Sometimes 50 or more times slower. On the other hand, if your whole Python program just does one function call which does some serious number crunching, and that Python function was written in C, then sure, it's not too slow compared to C.

Python speed: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=nbody&lang=python3&id=1&data=u32

C speed: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=nbody&lang=gcc&id=1&data=u32

Results show C is 50 times faster and uses one tenth the amount of memory.


Title: Re: Programming in Python
Post by: Welsh on October 10, 2013, 08:49:28 PM
Python being a Object orientated language is a great start. If you ever do get good enough that you know a lot, then you should also pick up other languages easier too.


Title: Re: Programming in Python
Post by: gangplank on October 12, 2013, 09:09:57 AM
New lessons are up.


Title: Re: Programming in Python
Post by: gangplank on October 13, 2013, 02:04:36 AM
Working on the functions lesson at the moment, I didn't really understand it at first. I think I've got it now as I tried to write some of my own and it outputs the correct answer. Maybe someone can check that my thinking is right on these functions I wrote.

Quote

# converts cm to inches

def cm_to_inches(cm):
    inch = 1 / 2.54 * cm
    return inch

a1 = cm_to_inches(25.4)
print a1



# convert inches to cm

def inch_to_cm(inch):
    cm = 2.54 / 1 * inch
    return cm

a2 = inch_to_cm(10)
print a2



# convert 100 to 1 (100 = 1, 500 = 5, 1000 = 10)

def onehundred_to_one(hundred):
    one = hundred / 100
    return one

a3 = onehundred_to_one(800)
print a3


# convert 1 to 100 (1 = 100, 5 = 500, 10 = 1000)

def one_to_onehundred(one):
    hundred = 100 * one
    return hundred

a4 = one_to_onehundred(5)
print a4


Title: Re: Programming in Python
Post by: jackjack on October 13, 2013, 02:12:57 AM
Yep you got it
Just two remarks: do you know you can "print cm_to_inches(1.0)" directly? And why writing ones in the formulas? Why not just "cm=2.54*inch"?


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 02:15:51 AM
You can share your code with CodeSkulpor for easier review: http://www.codeskulptor.org/#user19_HWWqVRQDa8_0.py
I don't know the exact formula but the code looks good to me.

I just finished everything for this week.  :D


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 02:18:04 AM
do you know you can "print cm_to_inches(1.0)" directly?
You are right but he did it the way it is taught in the video. I guess it is easier for beginner to understand.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 02:22:32 AM
By the way there is a computer networks course starting in 3 months: https://www.coursera.org/course/comnetworks

Quote
Course Syllabus:
Introduction, Protocols and Layering
Physical and Link layers
Retransmissions, Multiple access, Switching
Network layer, Internetworking
Intra- and Inter-domain Routing
Transport layer, Reliability
Congestion Control
DNS, Web/HTTP, Content Distribution
Quality of Service and Real-time Apps
Network Security


Title: Re: Programming in Python
Post by: gangplank on October 13, 2013, 02:23:32 AM
Yep you got it
Just two remarks: do you know you can "print cm_to_inches(1.0)" directly? And why writing ones in the formulas? Why not just "cm=2.54*inch"?

The example used in the course is fahrenheit to celsius but I couldn't seem to grasp it. I tried thinking of a few different ways I might use a function and those are just the things that came to mind. Obviously they are not useful as you've provided better options but for the purposes of understanding how a function operates, it seems to have worked for me :)


Title: Re: Programming in Python
Post by: gangplank on October 13, 2013, 03:39:04 AM
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 03:47:26 AM
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py
You can call a function anywhere you want in your code, even inside another function.


Title: Re: Programming in Python
Post by: jambola2 on October 13, 2013, 10:53:49 AM
I recommend trying a guide called learn python the hard way.
It is really helpful and easy.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 03:27:12 PM
Working on the functions lesson at the moment, I didn't really understand it at first. I think I've got it now as I tried to write some of my own and it outputs the correct answer. Maybe someone can check that my thinking is right on these functions I wrote.

Quote
# converts cm to inches

def cm_to_inches(cm):
    inch = 1 / 2.54 * cm
    return inch

a1 = cm_to_inches(25.4)
print a1

Less lines are better when it doesn't reduce clarity. The reason is it allows more lines visible on the screen, which allows greater comprehension while examining the code. But don't sacrifice white space to achieve that.

Quote
def cm_to_inches(cm):
    return cm / 2.54

And there's no point in dividing by 1 or multiplying by 1 or zero when they are constants:

Quote
# convert inches to cm

def inch_to_cm(inch):
    cm = 2.54 / 1 * inch
    return cm

Try this:

Quote
def inch_to_cm(inch):
    return inch * 2.54


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 03:33:37 PM
Nice  :)

Code:
def miles_to_km(miles):
    return miles * 1.60934

def km_to_miles(km):
    return km / 1.60934

print miles_to_km(10)
print km_to_miles(16.0934)

Edit:
Improved version that assumes that the user only input numbers:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

def km_to_miles(km):
    return float(km) / 1.60934

miles_input = input('Please input miles to convert in km:')
print miles_input + " miles = " + str(miles_to_km(miles_input)) + " km"

km_input = input('Please input km to convert in miles:')
print km_input + " km = " + str(km_to_miles(km_input)) + " miles"



Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:22:19 PM
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 04:24:15 PM
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

It prints "11"  ???

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:27:11 PM
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  ???

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.

This program prints 1.60934.

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)

Show me your entire program here.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 04:30:42 PM
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  ???

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.

This program prints 1.60934.

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)

Show me your entire program here.

http://www.codeskulptor.org/#user19_9Zu5nZ2ctm_3.py

I think it's because that I convert a string to a float. It is not integer --> float.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:41:40 PM
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:46:37 PM
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

Code:
def miles_to_km_no_float(miles):
    return miles * 1.60934

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 04:46:48 PM
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:48:25 PM
And that's the problem. Your toy Python interpreter did not catch the problem.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 04:49:08 PM
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

Code:
def miles_to_km_no_float(miles):
    return miles * 1.60934

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"

It works fine using python on my computer but still get the same old result with codeskulptor.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:50:35 PM
The problem is not with the functions. This is pointless:

Code:
def miles_to_km_with_float(miles):
    return float(miles) * 1.60934



Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 04:51:26 PM
And that's the problem. Your toy Python interpreter did not catch the problem.
I know it's meant to make it easy for student to share the link for their homework but the python implementation lack some feature.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 04:58:29 PM
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.

Also, I can't get a program to print the Python version to work in the online version.

For example, this does not work:

import platform
print(platform.python_version())


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 05:07:27 PM
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.

Also, I can't get a program to print the Python version to work in the online version.

For example, this does not work:

import platform
print(platform.python_version())

I think it's an incomplete version 2.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 05:09:10 PM
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 05:12:13 PM
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I will; however, the online interpreter is the only way to submit our work for the course.   


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 05:20:54 PM
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I will; however, the online interpreter is the only way to submit our work for the course.   

Even so, the function was not the problem. Run this online:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 05:22:09 PM
Even further proof: here we're guaranteeing that we're passing an integer into the function:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(int(1))


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 05:23:20 PM
Even further proof: here we're guaranteeing that we're passing an integer into the function:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(int(1))

Yes, I know this is how I did it the first time (https://bitcointalk.org/index.php?topic=303207.msg3330690#msg3330690) but it doesn't works using input() in the online version.


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 05:26:35 PM
Code:
def miles_to_km(miles):
    return miles * 1.60934

miles_input = input('Please input miles to convert in km: ')
print miles_to_km(float(miles_input))

What's likely happening here is that when querying for input, you're getting a string. What you want is a number. You don't know if the string, when converted to a number, has a decimal place or not, so you take the safe route and convert it to a float. But this is all about preparing the arguments for the function.

The function should expect a number (int or float) and receive that.


Title: Re: Programming in Python
Post by: Peter Lambert on October 13, 2013, 07:57:06 PM
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

for the print command, intead of adding a bunch of things together, it might be better to use placeholders?

Code:
print "%f miles = %f km." % (miles_input, miles_to_km_no_float(miles_input))


Title: Re: Programming in Python
Post by: FirstAscent on October 13, 2013, 08:09:44 PM
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

for the print command, intead of adding a bunch of things together, it might be better to use placeholders?

Code:
print "%f miles = %f km." % (miles_input, miles_to_km_no_float(miles_input))

Yes. Such power using C style printf printing. Read up on formatting for printf.


Title: Re: Programming in Python
Post by: 01BTC10 on October 13, 2013, 08:25:17 PM
Nice one.

http://en.wikipedia.org/wiki/Printf_format_string#1990s:_PHP.2C_Python


Title: Re: Programming in Python
Post by: MakeBelieve on October 13, 2013, 09:37:38 PM
I need to learn python


Title: Re: Programming in Python
Post by: gangplank on October 13, 2013, 10:20:39 PM
I need to learn python

Jump aboard with this course! I'm pretty much a beginner so we can assist one another via this thread :) I'm dedicating 1-2 hours a day to this, just need to make it habit for it to stick in my mind otherwise I'll forget.


Title: Re: Programming in Python
Post by: gangplank on October 13, 2013, 10:26:39 PM
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I don't know if this is related but it only uses the libraries we need to complete the course.


Title: Re: Programming in Python
Post by: MakeBelieve on October 13, 2013, 10:40:12 PM
I need to learn python

Jump aboard with this course! I'm pretty much a beginner so we can assist one another via this thread :) I'm dedicating 1-2 hours a day to this, just need to make it habit for it to stick in my mind otherwise I'll forget.

Will do. Coursera seems to have other awesome courses too.


Title: Re: Programming in Python
Post by: Baitty on October 13, 2013, 10:42:54 PM
There was a thread on this forum with different things people created with python.


Title: Re: Programming in Python
Post by: jackjack on October 14, 2013, 07:29:11 AM
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int
str

Fix
Quote
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"


Title: Re: Programming in Python
Post by: MakeBelieve on October 14, 2013, 08:41:14 AM
I've started the course once again.


Title: Re: Programming in Python
Post by: FirstAscent on October 14, 2013, 04:02:36 PM
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int
str

Fix
Quote
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

It's already been done.


Title: Re: Programming in Python
Post by: 01BTC10 on October 14, 2013, 04:26:28 PM
I found another course but this one use a real interpreter :D : https://www.edx.org/course/mit/6-00-1x/introduction-computer-science/1122