Personal computing discussed

Moderators: SecretSquirrel, just brew it!

 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Python practice programs

Thu Jul 16, 2015 11:28 am

I'm in school for computer science and I'm trying to learn Python. The problem is I've kinda passed the point of "Hello world" and random number guessing games. So i need to know where to go from here. What should my next program be!? And i would prefer one that would be applicable in a real world setting. Not a scrabble guessing game.

Thanks in Advance!!!
 
notfred
Maximum Gerbil
Posts: 4505
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Python practice programs

Thu Jul 16, 2015 11:36 am

Something that you are interested in and is useful to you. What are your hobbies? Any sports or similar where you could write a stats program? Music keep track of pieces and when performed?
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Thu Jul 16, 2015 11:47 am

If you are interested in pursuing web development, try creating some web applications using Python CGI scripts, or a full-blown Python-based web framework like Django. If you're more interested in stand-alone applications, learn one of the Python GUI frameworks like PyQt/PySide or wxPython.

More generally, pick something that interests you. One of my early Python learning exercises was a Mandelbrot fractal exploration and rendering program; I've been fascinated with fractals since the 1980s when Scientific American published an article about the Mandelbrot Set. I mentioned it here and here.
Nostalgia isn't what it used to be.
 
Flatland_Spider
Graphmaster Gerbil
Posts: 1324
Joined: Mon Sep 13, 2004 8:33 pm

Re: Python practice programs

Thu Jul 16, 2015 4:22 pm

Creating something database driven is something that happens in the real world. Writing a service using Twisted would be another way to go.

just brew it! wrote:
If you are interested in pursuing web development, try creating some web applications using Python CGI scripts, or a full-blown Python-based web framework like Django.


Flask is another good Python web-framework, and I think it's a little bit easier then Django.

Python CGI is fun. :) It's easy to write a script and throw it into a directory. It can actually be very useful too.
 
BobbinThreadbare
Graphmaster Gerbil
Posts: 1195
Joined: Sun Mar 20, 2005 2:47 am
Location: Champaign, IL
Contact:

Re: Python practice programs

Thu Jul 16, 2015 4:39 pm

Basically the opposite of real world application, but I always found the project euler problems fun, https://projecteuler.net/

I think the site has changed since the last time I looked at it though. I thought they used to list the lowest time for each language so you could see how efficient your solution was.
Last edited by BobbinThreadbare on Thu Jul 16, 2015 9:06 pm, edited 1 time in total.
 
MarkG509
Gerbil Elite
Posts: 743
Joined: Thu Feb 21, 2013 6:51 pm

Re: Python practice programs

Thu Jul 16, 2015 7:10 pm

You may find something interesting here: Ask HN: Good Python codebases to read?
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Mon Jul 20, 2015 12:05 pm

Hey thanks for the suggestions everyone!!!. I will definitely check all of those out. But with the database I've noticed that sql seems to be the most widely used. Is there a way to get exposure to SQL without spending much money??

Also, I have made a simple Text based game. Its like pokemon in that you battle a person and each monster has a set stats and depending on your actions it manipulates those stats in various way. Lowering HP. Raising Attack, etc. So is their a framework I could use to make a simple game where i could create a battle with two monster? Or is that aiming a little too high at this point??
 
BobbinThreadbare
Graphmaster Gerbil
Posts: 1195
Joined: Sun Mar 20, 2005 2:47 am
Location: Champaign, IL
Contact:

Re: Python practice programs

Mon Jul 20, 2015 12:11 pm

Cody_Pac_68 wrote:
Hey thanks for the suggestions everyone!!!. I will definitely check all of those out. But with the database I've noticed that sql seems to be the most widely used. Is there a way to get exposure to SQL without spending much money??

mysql is 100% free (as in beer and speech).
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Mon Jul 20, 2015 12:21 pm

As an aside, PostgreSQL is arguably a better database system, and is also free. But for learning purposes you're probably better off with MySQL since it is the most widely used, so you'll find plenty of tutorials and tools online that are geared towards it.
Nostalgia isn't what it used to be.
 
cheesyking
Minister of Gerbil Affairs
Posts: 2691
Joined: Sun Jan 25, 2004 7:52 am
Location: That London (or so I'm told)
Contact:

Re: Python practice programs

Mon Jul 20, 2015 1:19 pm

Cody_Pac_68 wrote:
Hey thanks for the suggestions everyone!!!. I will definitely check all of those out. But with the database I've noticed that sql seems to be the most widely used. Is there a way to get exposure to SQL without spending much money??


SQL is extremely popular and it's worth learning at least some of the basics but it's often a classic sledge hammer to crack a nut scenario. You can solve any problem by throwing SQL at it but there are often better solutions out there. A classic case of SQL overuse that I can remember is a music manager application that used to be installed by default on Sony laptops. This used MSSQL to store meta data for your music collection which might be fine if you had literally millions of songs but for most people who just have a few hundred or thousand songs it's massive overkill.

BTW, just in case you're confused over this (I'm not sure but I think you might be judging by you're post). SQL is a language for talking to databases in (Structured Query Language) and it's supported by lots of different database servers (MySQL, PostgreSQL, Oracle, MSSQL). So in theory you can write your application once and run it on any database server (though obviously it doesn't really work out that way). There are also ways to use SQL without an actual DB server like SQLite which just stores the data in a file with your python code.

The current hotness in the web 2.0 world are NoSQL databases, which (as you can probably guess) don't use SQL and have various advantages and disadvantages compared to traditional SQL databases.

Also, I have made a simple Text based game. Its like pokemon in that you battle a person and each monster has a set stats and depending on your actions it manipulates those stats in various way. Lowering HP. Raising Attack, etc. So is their a framework I could use to make a simple game where i could create a battle with two monster? Or is that aiming a little too high at this point??


Are you talking about making a gui or something for the existing text game or rewriting the text game completely using a framework of some sort?

I'd imagine there are frameworks for building games in python though I've never used any. I'd also think that
1) this is a bit too simple to bother with a framework
2) you might be better off concentrating on learning and practising the basics of the language rather than going straight to a framework

Now if you're just talking about a gui or web front end then obviously you're going to need a framework of some sort to do that. I'm not a gui programmer but at a guess I'd say you'd want to use QT for a desktop gui application. For a web front end I'll second Flask. It's a very minimal framework that just does the minimum you need to build a web application in python so you spend more time writing your application rather than learning the framework.

The problem is I've kinda passed the point of "Hello world" and random number guessing games. So i need to know where to go from here.

What stuff have you covered so far: loops, conditions, functions, recursion, error handling, OOP? Have you done any programming before?
Fernando!
Your mother ate my dog!
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Mon Jul 20, 2015 2:11 pm

honestly I've not done alot of programming. I have wrote as mentioned a text based game. It uses loops and conditions as well as error handling. It uses loops to continue the battle until a sentinel value of HP=0 is reached. It uses conditions by letting choose an attack or to use an item. If you use an attack it lowers the opponents HP and if you type item it lets you select an item which alters one of your attributes. If you type something other than attack name or the word 'item' it tells you it's invalid input and returns to the top of the loop.

I'm not 100% sure what recursion is exactly, or rather how it's applied in programming.

Also not sure about OOP, I know it's object oriented programming but not exactly sure what that means when I'm actually writing code if that makes sense

When i made the statement of past the "hello world' level i mainly meant i wanted to write code that did more than take a few numbers or word, altered them in some simple way, and then spit them back out.
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Mon Jul 20, 2015 2:15 pm

Also thanks for the help with the SQL. So if i understand write I should be able to run mySQL on windows?? and SQL itself I can download from Oracle's website? and I will be writing code in SQL to manipulate the data stored in mySQL??
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Mon Jul 20, 2015 2:30 pm

Cody_Pac_68 wrote:
Also thanks for the help with the SQL. So if i understand write I should be able to run mySQL on windows?? and SQL itself I can download from Oracle's website?

Yup, there are versions for Windows, Mac, Linux, and FreeBSD, and the "Community Edition" is free.

Cody_Pac_68 wrote:
and I will be writing code in SQL to manipulate the data stored in mySQL??

Typically you would issue SQL commands to (and receive query results from) the database using whatever programming language you are using for the rest of your application. SQL databases typically do have a sort of programming language built-in (so-called "stored procedures"), but this is not a general-purpose language suitable for building an entire application; you still need something like Python to implement the high-level application logic, user interface, network communication, etc...
Nostalgia isn't what it used to be.
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Mon Jul 20, 2015 2:39 pm

So the SQL commands would be similar to using Tkinter in python for building GUIs?
 
BobbinThreadbare
Graphmaster Gerbil
Posts: 1195
Joined: Sun Mar 20, 2005 2:47 am
Location: Champaign, IL
Contact:

Re: Python practice programs

Mon Jul 20, 2015 2:40 pm

Cody_Pac_68 wrote:
Also thanks for the help with the SQL. So if i understand write I should be able to run mySQL on windows?? and SQL itself I can download from Oracle's website? and I will be writing code in SQL to manipulate the data stored in mySQL??

MySQL runs as a server (and yes you can run it on Windows). Then you write code that connects to the server and runs what are called queries. That's selecting, inserting, updating, or deleting data. The tutorial built into the MySQL docs is actually pretty decent and should let you understand the basics.

The syntax of the queries themselves is pretty standard across various versions (MySQL, MSSQL, PostgresSQL), but they do have minor variations and some times different capabilities.

Writing a program that interfaces with the sql server does add a layer complexity, as python should provide it's own way of writing queries that again will be very similar, but just slightly different from other sql implementations.
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Mon Jul 20, 2015 2:45 pm

Cody_Pac_68 wrote:
So the SQL commands would be similar to using Tkinter in python for building GUIs?

Yeah, more or less.
Nostalgia isn't what it used to be.
 
cheesyking
Minister of Gerbil Affairs
Posts: 2691
Joined: Sun Jan 25, 2004 7:52 am
Location: That London (or so I'm told)
Contact:

Re: Python practice programs

Mon Jul 20, 2015 5:37 pm

Cody_Pac_68 wrote:
honestly I've not done alot of programming.

You're doing well, OOP and recursion are techniques that will come in handy. You probably don't have to worry about them right now but then on the other hand it doesn't hurt to use these sorts of things from day one so you don't develop bad habits.

Recursion is where something calls itself to solve a problem in small steps. It's basically another way to do loops but it's neater and more elegant.

OOP groups code relating to specific things together (in a object which is defined by a class) so all the variables and functions relating to that thing are all in the same place. This does a couple of things: it makes the code easier to read and modify; it encourages you to break your code down into smaller chunks and it makes reusing code easier.

The really clever bit with OOP is inheritance. This is where a class is built on top of another class. Say you create a class to represent individual people (lets call it Person). This class would have variables like age, name, height etc. You might then create a Woman class that inherits from the Person class. Because it inherits from Person the Woman class has all the variables like age, name etc but you can also add variables and functions that only apply to women like... and this is where I wish I'd used a different example as I can't think of anything that might not be thought of as sexist :oops:

Anyway better explanations exist and I encourage you to find them but don't get too hung up on them. I'd do a couple of quick exercises then get stuck into a decent project. Hopefully you'll start to see places where they will be useful.

When i made the statement of past the "hello world' level i mainly meant i wanted to write code that did more than take a few numbers or word, altered them in some simple way, and then spit them back out.

If you're into games then how about revisiting some of the old arcade classics like breakout, pacman or worms. I remember writing a very basic 2d version of Zarch (AKA Virus) when I started programming which was a fun little game. (BTW I'm not a proper programmer so don't take everything I say as gospel :wink: ) I'd look for a game that doesn't need a lot of artwork (unless you also happen to be an artist).

I'm afraid I can't really help you on this though as all the python I do is just sucking in numbers and words, altering them and then spitting them back out :lol: , no seriously I either just do basic scripts to automate jobs like resizing images etc or as the backend for some web app.

Don't be afraid to aim high in terms of a project, just be prepared to get there in a lot of small steps. The only thing I'd suggest you avoid for now is thinking about doing anything 3D.
Fernando!

Your mother ate my dog!
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Tue Jul 21, 2015 10:25 am

thanks for all the help you guys. I think I've decided to revisit my text based game. I'm going to ATTEMPT to set a class 'Monster' with an instance called, let's say 'Pidgey' and try to set my battle module up by using the original traits from 'Monster' and adding a variable of some sort to alter those slightly. For example, 'Monster' will have an ATK, DEF, and HP. "Pidgey' will then inherit those and and then i would multiply each stat by a random number between (1-1.4). So does this sound like I'm on the right track with using OOP??
 
cheesyking
Minister of Gerbil Affairs
Posts: 2691
Joined: Sun Jan 25, 2004 7:52 am
Location: That London (or so I'm told)
Contact:

Re: Python practice programs

Tue Jul 21, 2015 12:10 pm

Cody_Pac_68 wrote:
thanks for all the help you guys. I think I've decided to revisit my text based game. I'm going to ATTEMPT to set a class 'Monster' with an instance called, let's say 'Pidgey' and try to set my battle module up by using the original traits from 'Monster' and adding a variable of some sort to alter those slightly. For example, 'Monster' will have an ATK, DEF, and HP. "Pidgey' will then inherit those and and then i would multiply each stat by a random number between (1-1.4). So does this sound like I'm on the right track with using OOP??


Yeah that's a start. I don't know anything about how Pokemon battles work so it's a little hard for me to comment on whether it's sensible to make the monsters different classes or not but it's certainly a good exercise.

For a practical example here's the last thing I did with OOP. I was writing a web app that stored things like tasks and notes in a database. To start off with I created a class called something like DB_item. This class had all the code it needed to load and save itself to the DB but didn't do anything else. I then created Task and Note classes which inherit from DB_item and add anything additional they need. This way all the code for loading and saving items to the database is all in one place (inside DB_item), which is sort of neat.

BTW
As the programs you write get longer you'll want to start breaking them up into separate files so you don't have to scroll through 1000s of lines of code to find the bit you want to change. It's pretty common to put each class or group of related classes in their own file. Now the problem here is how do you test this code without running the whole program. If you just put some test code at then end of the class definitions like this:
class Test_class:
    def __init__(x):
        self.x = x

#test code here
t = Test_class(3)
print t.x

Then when you try to import it into the full program bad things can happen

You could just remember to comment out the test code when you weren't using it but you know that sooner or later you'll forget so a nice trick is to put your test code in an if statement like this:
class Test_class:
    def __init__(x):
        self.x = x

#test code here
if __name__=='__main__':
    t = Test_class(3)
    print t.x


What this does is stop the test code from running unless you directly execute the file (eg python testfile.py) so you can leave the test code in place.
Fernando!

Your mother ate my dog!
 
BobbinThreadbare
Graphmaster Gerbil
Posts: 1195
Joined: Sun Mar 20, 2005 2:47 am
Location: Champaign, IL
Contact:

Re: Python practice programs

Tue Jul 21, 2015 12:48 pm

Cody_Pac_68 wrote:
thanks for all the help you guys. I think I've decided to revisit my text based game. I'm going to ATTEMPT to set a class 'Monster' with an instance called, let's say 'Pidgey' and try to set my battle module up by using the original traits from 'Monster' and adding a variable of some sort to alter those slightly. For example, 'Monster' will have an ATK, DEF, and HP. "Pidgey' will then inherit those and and then i would multiply each stat by a random number between (1-1.4). So does this sound like I'm on the right track with using OOP??

That sound pretty reasonable.

Keep in mind though, you don't generally inherit the actual values. In your example, typically Monster would have a constructor that sets ATK, DEF, and HP, and each subclass would have a value it sets*. The monster class should just say that ATK, DEF, and HP exist though, not set values for them. Unless the subclasses will generally or always have the same values or there is a reason to have some kind of default value. I think it would be better to have the pidgey class randomize a value between X and Y and then set the value instead of applying a random multiplier to the super classes default values in this case.

Another thing to keep in mind is that you probably want max and current attributes, or maybe not "max" but "unmodified" since pokemon has so many abilities that change attributes for single battles.

*There are a few ways to do this in Python. You can define the variables in the super class, override the constructor completely, or override the constructor then call the super constructor.
 
Flatland_Spider
Graphmaster Gerbil
Posts: 1324
Joined: Mon Sep 13, 2004 8:33 pm

Re: Python practice programs

Tue Jul 21, 2015 1:10 pm

cheesyking wrote:
You could just remember to comment out the test code when you weren't using it but you know that sooner or later you'll forget so a nice trick is to put your test code in an if statement like this:
class Test_class:
    def __init__(x):
        self.x = x

#test code here
if __name__=='__main__':
    t = Test_class(3)
    print t.x


What this does is stop the test code from running unless you directly execute the file (eg python testfile.py) so you can leave the test code in place.


The better way to do this is to use a unit test framework, like Nose (http://pythontesting.net/framework/nose ... roduction/), or to write tests in a separate test file. This keeps the project files free of test code.

cheesyking wrote:
Recursion is where something calls itself to solve a problem in small steps. It's basically another way to do loops but it's neater and more elegant.


The Python devs don't like recursion, so Python's support for recursion isn't the greatest. C/C++ or a functional programming language would be better for learning about recursion.

Recursion being neater and more elegant is subjective, and kind of depends on the problem at hand.

It's kind of interesting that there are recursive people and iterative people. It's just a natural preference for one or the other.

Cody_Pac_68 wrote:
Is there a way to get exposure to SQL without spending much money??


SQLite should come bundled with Python. SQLite is a little embedded database engine, so it doesn't require setting up a server.
 
Cody_Pac_68
Gerbil In Training
Topic Author
Posts: 9
Joined: Thu Jul 16, 2015 10:57 am

Re: Python practice programs

Tue Jul 21, 2015 1:22 pm

Ok I think i understand most of that lol. So the class Monster would just says that the variable ATK, DEF, and HP will be prresent in each instance of the class. So if Pidgey was an instance of the Monster class it would inherit the variable names ATK, DEF, and HP. and they would have to be set in the Pidgey module? Then i could use something like randint to set a random value to each variable?

So my next question is pertaining to the MAX stats... Would their be a way to set the levels where it goes up by one and adds a random number to each stat without having an extra if statement in the program??
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Tue Jul 21, 2015 1:29 pm

Flatland_Spider wrote:
Recursion being neater and more elegant is subjective, and kind of depends on the problem at hand.

Recursion can result in very elegant solutions to problems which are amenable to a recursive approach. In other cases, it can cause hard-to-debug system misbehaviors, e.g. stack depth that grows linearly with data set size for certain pathological inputs (this can be particularly deadly on embedded systems with limited RAM and/or no MMU).
Nostalgia isn't what it used to be.
 
BobbinThreadbare
Graphmaster Gerbil
Posts: 1195
Joined: Sun Mar 20, 2005 2:47 am
Location: Champaign, IL
Contact:

Re: Python practice programs

Tue Jul 21, 2015 2:06 pm

Cody_Pac_68 wrote:
Ok I think i understand most of that lol. So the class Monster would just says that the variable ATK, DEF, and HP will be prresent in each instance of the class. So if Pidgey was an instance of the Monster class it would inherit the variable names ATK, DEF, and HP. and they would have to be set in the Pidgey module? Then i could use something like randint to set a random value to each variable?

That's how I would do it. This will actually help you catch problems like forgetting to set an attribute. Instead of having a default and possibly never catching the wrong number being used, you'll get an error.

So my next question is pertaining to the MAX stats... Would their be a way to set the levels where it goes up by one and adds a random number to each stat without having an extra if statement in the program??

Sure, just write a level up function that changes the max values.
 
notfred
Maximum Gerbil
Posts: 4505
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Python practice programs

Tue Jul 21, 2015 2:13 pm

just brew it! wrote:
Flatland_Spider wrote:
Recursion being neater and more elegant is subjective, and kind of depends on the problem at hand.

Recursion can result in very elegant solutions to problems which are amenable to a recursive approach. In other cases, it can cause hard-to-debug system misbehaviors, e.g. stack depth that grows linearly with data set size for certain pathological inputs (this can be particularly deadly on embedded systems with limited RAM and/or no MMU).

About 10 years ago I was logged in to a core router and watched a line card with a 10G link between 2 major cities crash and reboot. There was a particular packet of death that caused the recursion to not stop. :evil:
 
Redocbew
Gold subscriber
Gerbil Jedi
Posts: 1709
Joined: Sat Mar 15, 2014 11:44 am

Re: Python practice programs

Tue Jul 21, 2015 9:17 pm

I'm going to toss out the idea that you take a peek at NoSQL databases also, like MongoDB or Cassandra. Maybe not the most useful right out of the gate, but pretty widely used these days by anyone with a lot of data that needs wrangling.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.
 
just brew it!
Gold subscriber
Administrator
Posts: 51649
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Python practice programs

Tue Jul 21, 2015 9:30 pm

What's old is new again... the first large database application I worked on (over 20 years ago) used something called db_Vista (now known as Raima Data Manager), which qualifies as a NoSQL database engine I guess.
Nostalgia isn't what it used to be.
 
Flatland_Spider
Graphmaster Gerbil
Posts: 1324
Joined: Mon Sep 13, 2004 8:33 pm

Re: Python practice programs

Wed Jul 22, 2015 11:48 am

just brew it! wrote:
What's old is new again... the first large database application I worked on (over 20 years ago) used something called db_Vista (now known as Raima Data Manager), which qualifies as a NoSQL database engine I guess.


Was it a key/value store?

Document-oriented databases is what NoSQL databases really are (https://en.wikipedia.org/wiki/Document- ... d_database), and they have been around for a while. They just became popular more recently, because they allow looser data storage due to an absence of a rigidly enforced schema.

I'm sure there is also a marketing aspect to it. NoSQL looks better on a bumper sticker then Document-oriented database.

They have their place, and they are nice for dealing with unstructured data.
 
MarkG509
Gerbil Elite
Posts: 743
Joined: Thu Feb 21, 2013 6:51 pm

Re: Python practice programs

Fri Jul 24, 2015 4:12 pm

Seek and ye shall find: O'Reilly's Functional Programming in Python, found via Hacker News.
 
morphine
Gold subscriber
TR Staff
Posts: 11477
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Python practice programs

Wed Jul 29, 2015 2:29 pm

Jeez guys, OP just got into programming and we're already talking databases, NoSQL, recursion, etc? :lol:
There is a fixed amount of intelligence on the planet, and the population keeps growing :(

Who is online

Users browsing this forum: No registered users and 1 guest