Be a Supporter!
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Python 2012-12-29 16:16:41 Reply

My friends,

While I've had experience with multiple programming languages and scripting languages, I haven't spent a lot of time with Python or Ruby. I'm interesting in learning both of these languages. One, because Python is used quite extensively at the place of my employment. Quite a few of the existing websites were created using Zope, which I think is a terrible CMS, but I felt that learning Python would be beneficial.

I have quite a few resources to help with my Ruby pursuits. However, I'm not sure the best approach to learning Python. I read the PEP8 for styling Python code. I also read the Pep for docstrings for writing proper comments and documentation. I'm reading the Python 3.X Tutorial (I've made it to chapter 4).

What Python resources do you recommend I use for learning Python?


BBS Signature
deckheadtottie
deckheadtottie
  • Member since: Oct. 21, 2003
  • Offline.
Forum Stats
Supporter
Level 59
Programmer
Response to Python 2012-12-29 16:30:24 Reply

Diki made a great thread which highlights some features of Python. There's even a link or two to some teaching resources.


#coys

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2012-12-29 16:33:01 Reply

At 12/29/12 04:30 PM, deckheadtottie wrote: Diki made a great thread which highlights some features of Python. There's even a link or two to some teaching resources.

Diki continues moving up on my totem pole of respect. You rock dude. I'll check that out, thanks!


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2012-12-29 21:48:06 Reply

At 12/29/12 04:33 PM, FallingTears wrote: I'll check that out, thanks!

No worries, buddy.
Keep in mind that all the code I wrote in that thread was intended for Python 3.x, though it shouldn't really matter. If you're going to learn Python from scratch I'd say you should learn Python 2.x first because you'll find more support for it right now, and it's easier to move from 2.x to 3.x than vice-versa (kind of like how it's easier to learn C before you learn C++ than it is to do the opposite).

If you run into any problems with Python feel free to PM me or post in this thread; I know Python pretty well, and love talking about it, so I should be able to lend a helping hand. :)

P.S.
Just for clarity the book I linked to in my thread is Learn Python the Hard Way, and there is also the other free book Think Python which is an excellent book as well.

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2012-12-29 21:56:55 Reply

At 12/29/12 09:48 PM, Diki wrote:
No worries, buddy.
Keep in mind that all the code I wrote in that thread was intended for Python 3.x, though it shouldn't really matter. If you're going to learn Python from scratch I'd say you should learn Python 2.x first because you'll find more support for it right now, and it's easier to move from 2.x to 3.x than vice-versa (kind of like how it's easier to learn C before you learn C++ than it is to do the opposite).

Before you posted the above, I already decided to start with Python 2.x first. When it comes to C/C++, I actually learned C++ first through college. We discussed C, but didn't look at code or work with it. So, I have two semesters of C++. I know very little about C.

I read through the entire thread that you posted about loving Python. I do want to say that because of the programming classes that I've taken in college (learning how to write those algorithms), I can honestly enjoy the power given to me through other higher level programming languages. I don't think someone should be less efficient and spend time reinventing the wheel. The wheel is already 360 degrees, you're not going to refine it any better than that. Use those wheels and assemble a car!

If you run into any problems with Python feel free to PM me or post in this thread; I know Python pretty well, and love talking about it, so I should be able to lend a helping hand. :)

Awesome. I'm quite certain I'll have a lot of questions. May I add you to my friends list? By the way, I enjoyed the bit you wrote about making a simple HTTP server. That sounds exciting honestly.

P.S.
Just for clarity the book I linked to in my thread is Learn Python the Hard Way, and there is also the other free book Think Python which is an excellent book as well.

I not only bookmarked the book, but I purchased the course to get the PDF and download the videos associated with the book. I'm really interested in this language!


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2012-12-29 22:10:26 Reply

At 12/29/12 09:56 PM, FallingTears wrote: Before you posted the above, I already decided to start with Python 2.x first. When it comes to C/C++, I actually learned C++ first through college. We discussed C, but didn't look at code or work with it. So, I have two semesters of C++. I know very little about C.

Not to derail, but C is basically a simplified version of C++ (or, more accurately, C++ is a more complex version of C). One of the biggest differences, and one that will definitely slip up C++ programmers, is that C doesn't have classes or anything resembling them. The closest it has is structures, which cannot contain functions or assignments.

But enough about C. That's best left for a different thread.

I read through the entire thread that you posted about loving Python. I do want to say that because of the programming classes that I've taken in college (learning how to write those algorithms), I can honestly enjoy the power given to me through other higher level programming languages. I don't think someone should be less efficient and spend time reinventing the wheel. The wheel is already 360 degrees, you're not going to refine it any better than that. Use those wheels and assemble a car!

I think that's why some people, such as sharpnova in my thread, don't like Python: they've become accustomed to languages making certain things difficult and requiring verbose code, so when they see how to do it in Python they find it "too easy" and "not real programming". That's purely speculation though.
Personally I love doing as much as possible with as little code as possible, while still writing readable code, and Python is great for that.

Awesome. I'm quite certain I'll have a lot of questions. May I add you to my friends list?

Of course. In fact I beat you to it and added you to my friends list.

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2012-12-31 22:54:04 Reply

I will be writing Python as the calendar changes to the New Year!!!


BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2012-12-31 23:50:24 Reply

So, I ran into my first Python problem!!! YES!

Just playing around with the quadratic formula and equation.

# ax^2 + bx + c = 0
# x = (-b +/- sqrt(-b^2 - 4ac)) / 2a
# Solve for x
# 4x^2 + 8x + 16 = 0

from math import sqrt

def sqr(num):
	return num * num

print -8 + (sqrt(sqr(-8) - 4 * 4 * 16) / (2 * 4))
print -8 - (sqrt(sqr(-8) - 4 * 4 * 16) / (2 * 4))

Error

C:\Users\Samuel\Desktop\Python\Practice>python extra_math.py
Traceback (most recent call last):
  File "extra_math.py", line 11, in <module>
    print -8 + (sqrt(sqr(-8) - 4 * 4 * 16) / (2 * 4))
ValueError: math domain error

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2012-12-31 23:58:18 Reply

Ah, trying to get the square of a negative number!


BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 01:02:32 Reply

For my first actual Python exercise, I decided to write a simple algorithm that will determine whether a number is prime. This algorithm doesn't consider some of the more complicated ways primes can be determined. My algorithm follows the following suit:

All prime numbers and non-prime numbers are divisible by 1 and itself. So, I automatically drop one and the selected number. So you have a range of numbers starting with 2 and ending with "selected number minus 1." Then, I test whether "selected number" can have the range of numbers evenly divided into it. If any of the numbers can be divided into the selected number, then I increment "not prime" counter. If the number doesn't divide into the selected number, then the "prime" counter is incremented. If the "not prime" counter is equal to 0 in the end, then the number is prime.

I'm reducing this code by dropping the "prime" counter as it's not needed. Then, I will test at the beginning of the code whether the "not prime" counter has been incremented by 1. If it has, the code will automatically stop as "not prime."

Version 1:

def isPrime(num):
	primeCount = 0
	notPrimeCount = 0
	prime = 0
	for i in range(2,num - 1):
		if num % i == 0:
			notPrimeCount += 1
		if num % i != 0:
			primeCount += 1

	if notPrimeCount == 0:
		prime = "Prime"
	else:
		prime = "Not Prime"

	return prime

print isPrime(4)

Version 2 -- More efficient, I think?

def isPrime(num):
	notPrimeCount = 0
	prime = 0
	for i in range(2,num - 1):
		if notPrimeCount == 1:
			break
		if num % i == 0:
			notPrimeCount += 1

	if notPrimeCount == 0:
		prime = "Prime"
	else:
		prime = "Not Prime"

	return prime

print isPrime(2)

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 01:16:41 Reply

Once again, I think I made it more efficient:

def isPrime(num):
	notPrimeCount = 0
	prime = 0
	for i in range(2,num - 1):
		if notPrimeCount == 1:
			break
		if num % i == 0:
			prime = "Not Prime"
			notPrimeCount += 1
			break

	if notPrimeCount == 0:
		prime = "Prime"

	return prime

print isPrime(11)

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 01:17:58 Reply

Yes, one more optimization:

def isPrime(num):
	notPrimeCount = 0
	prime = 0
	for i in range(2,num - 1):
		if num % i == 0:
			prime = "Not Prime"
			notPrimeCount += 1
			break

	if notPrimeCount == 0:
		prime = "Prime"

	return prime

print isPrime(10)

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-01-01 12:09:57 Reply

If you want to make it even more efficient you can perform less checks by only checking up to the square root of the number plus one, as that is sufficient for determining if a given number is prime.
I'd also say that it would make more sense to return a boolean instead of a string (plus that is more Pythonic), so this is how I would write it:

def isPrime(num):
    for i in range(2, int(num**0.5)+1):
        if num%i == 0:
            return False
    return True
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-01-01 12:22:05 Reply

And speaking of writing Pythonic code it's always a good idea for newcomers to Python to read The Zen of Python. Following that will help you write more Pythonic code (which, generally speaking, you should try to do if you're writing Python).

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-01-01 14:00:59 Reply

And upon thinking about it using xrange would be more efficient in that code, since it doesn't load an entire list into memory at once, though the difference would be pretty negligible if you're checking primes under like 1000.

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 15:57:12 Reply

Thanks for the response. Love the Zen!!! Anyway, could you explain to me a little more how the math in your code works. I have done square roots, but not as manually as that.

Thanks

I'm going to try out xrange


BBS Signature
kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Python 2013-01-01 16:59:01 Reply

At 1/1/13 03:57 PM, FallingTears wrote: Anyway, could you explain to me a little more how the math in your code works.

If the square root of something is x then for it to be dividable by x+1 for instance, it would also have to be dividable by a number below x as well, which you will find on your way there, so there's no point in going past that point.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-01-01 22:39:12 Reply

At 1/1/13 03:57 PM, FallingTears wrote: Thanks for the response. Love the Zen!!! Anyway, could you explain to me a little more how the math in your code works. I have done square roots, but not as manually as that.

When I'm doing num**0.5 that's the exact same as sqrt(num). Raising a number to the 0.5th power will give you the number's square root (just to be clear: the ** operator in Python is the exponent operator).

The reason I am adding one to the result is essentially just error padding. If you did not do that and checked, for example, 8 for prime you would get the square root of 8, which is 2.82, which would be rounded down to 2 when converted to an integer, and then considered prime. 8 is of course not prime, so padding the result with a +1 will help prevent that error from occurring. Keep in mind that this only works this way because of how the for operator works in Python (because range(8) will iterate through 0 to 7, not including 8).
In short: it prevents false positives without producing false negatives.

kiwi-kiwi already explained why you only need to look up to the square root of a number to determine if it's prime, but I'll try explaining in my own words: basically if you take any number that is not prime, which I will call n, then any number between 1 and sqrt(n) will be divisible by something that's not 1 or n and anything that the square root of n can be divided by the number n itself can also be divided by.
Take the number 64, for example, which has a square root of 8. The number 8 is divisible by 1, 2, 4, and 8. The number 64 is also divisible by 1, 2, 4 and 8, which therefore means 64 is not a prime number.
But if you take the number 107, which is prime, and you produce its prime which is 10.34, and round that down and then increment by one, and you get 11. The number 11 is prime, therefore 107 is also prime.

That's the best I can explain it. :)

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 23:28:58 Reply

Wow, that actually makes sense. In school, even though I have had the highest marks in Math I never dealt a lot with square roots without a calculator. I knew, at that time, all the squares of 1 through 30 by memory. So, when a perfect square happened to be in that range, I automatically knew its squareroot. (Almost ALL perfect squares in HS and college were within that range).

Looks like I need to revisit math as I haven't done math in about 10 years (I don't consider basic arithmetic math).


BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 23:38:00 Reply

I would then assume a cubed root would be something like 8**0.3333333333333333


BBS Signature
Thegluestickman
Thegluestickman
  • Member since: Mar. 16, 2007
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to Python 2013-01-01 23:44:58 Reply

At 1/1/13 11:38 PM, FallingTears wrote: I would then assume a cubed root would be something like 8**0.3333333333333333

Yeah, anything to the power of a fraction is similar to taking the nth root of the number, where n is your denominator. If that makes sense.

27 ^ 1/3 = 3root(27)


To protect the world from devastation.
kidd25 is the best Newgrounder ever.

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-01 23:54:02 Reply

At 1/1/13 11:44 PM, Thegluestickman wrote:
At 1/1/13 11:38 PM, FallingTears wrote: I would then assume a cubed root would be something like 8**0.3333333333333333
Yeah, anything to the power of a fraction is similar to taking the nth root of the number, where n is your denominator. If that makes sense.

27 ^ 1/3 = 3root(27)

Perfect sense!!! When I was in school, I never thought about this. I'm sure they taught us, but I didn't take the effort to learn. I'm thinking about taking some math courses all over again.


BBS Signature
Thegluestickman
Thegluestickman
  • Member since: Mar. 16, 2007
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to Python 2013-01-02 13:27:05 Reply

At 1/1/13 11:54 PM, FallingTears wrote: Perfect sense!!! When I was in school, I never thought about this. I'm sure they taught us, but I didn't take the effort to learn. I'm thinking about taking some math courses all over again.

It's just a little tricky to explain math in words. If you're looking to review some math:

khanacademy.org

has some really helpful math lessons, and math practices.


To protect the world from devastation.
kidd25 is the best Newgrounder ever.

BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-04 11:25:35 Reply

I've tried to come up with a list of "exercises" that I could do to help learn Python. If any of you would like to throw out some exercises to help with this ... that would be great.


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-01-04 14:18:57 Reply

There's Python Challenge, which is a collection of increasingly difficult challenges intended to be solved with Python. I'm not a big fan of it, because the challenges are more lateral thinking puzzles that are solved with programming than actual programming challenges, but lots of people seem to like it.

Other things you could do, which I have done in the past for learning, are (all of these are command line applications):
- Blackjack against a computer (with basic ascii-graphics)
- Double Linked List with push/pop/shift/unshift/insert/remove functionality
- Binary Search Tree with insert/remove functionality
- Command line parsers that let you write commands which will do things like:
................... - Read and manipulate text files
................... - Open a connection to server via HTTP and have a specified request sent (and receive the response)

After you get more comfortable with Python you could move on to using a library to make something practical:
- web2py to make a website
- PyGame to make games
- Tkinter to make GUI applications

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-01-04 14:22:09 Reply

Thanks! I'll definitely look into the above and share progress I make. I'm sure it will also be a great learning experience when others teach you better ways of doing things.


BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-02-05 22:48:25 Reply

At 1/1/13 10:39 PM, Diki wrote:
At 1/1/13 03:57 PM, FallingTears wrote: Thanks for the response. Love the Zen!!! Anyway, could you explain to me a little more how the math in your code works. I have done square roots, but not as manually as that.
When I'm doing num**0.5 that's the exact same as sqrt(num). Raising a number to the 0.5th power will give you the number's square root (just to be clear: the ** operator in Python is the exponent operator).

This algorithm doesn't work for all prime numbers. Depending on how large the number gets, the algorithm may have to change to accommodate for a margin that I don't even understand. Take this prime number in consideration Largest Prime Number Discovered

Using the mathematical algorithm you provided fails in that the square root of this prime + 1 is not a prime.

I'm sure for people who program as hobbyists, they'll never need to find a prime number in the "margin of error" range provided in the above algorithm.

Quite truthfully, not one single algorithm could be used for discovering all primes.


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Python 2013-02-05 23:35:39 Reply

At 2/5/13 10:48 PM, FallingTears wrote: Take this prime number in consideration Largest Prime Number Discovered

They didn't say what the largest prime number was in that article (merely only the power to which raise 2 to in order to achieve it, which would take a very long time to calculate on a single processor).
If you actually calculated that number and then used it in that algorithm in Python then chances are that there was an error produced by the way computers represent numbers. If you were to calculate that algorithm by hand you'd find that the prime number described in the article is determined as prime by that algorithm (so far as I know, at least, I'm not going to confirm that by calculating it).

The only way to even possibly determine if that prime can be determined to be prime with a computer using the algorithm I described is to use something like Mathematica, and even then I don't know how well it would handle a number with 17 million digits.

FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-02-06 08:22:53 Reply

17 million digits.


BBS Signature
FallingTears
FallingTears
  • Member since: Nov. 28, 2012
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Python 2013-09-26 16:50:28 Reply

Are there any other free online course-based websites?


BBS Signature