Random Homebrew: 1945 Sidescroller
fly around and shoot enemies and earn points.

[Tutorial] Learning programming with Python, part 1

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device

[Tutorial] Learning programming with Python, part 1

Postby Acid_Snake » Fri May 25, 2012 4:57 pm

Advertising
I want to start this Topic to introduce people on the basics of Python programming.

Intro:
Python is a high-level object oriented interpreted programming language.

A simple app:
In every programming language is best to start creating a basic app, this app is usually a hello world and in python it can be as simple as:
Code: Select all
print "Hello World"

This will print the text Hello World and exit.

Strings and integers:
In python there are strings and there are integers, a string uses double quotes or single quote:
Code: Select all
print "Hello World"
print 'Hello World'

They both do the same.
Integers don't use any type of quotation:
Code: Select all
print 1

If an integer uses quotation it becomes a string, for example:
Code: Select all
print 1+1

will print out number 2, but this:
Code: Select all
print "1"+"1"

will print out 11 the same way this:
Code: Select all
print "hello"+"world"

will print out helloworld
It is important to know that you can't add (+) a string and an integer:
Code: Select all
print 1 + "hello"

will produce this error:
Code: Select all
TypeError: unsupported operand type(s) for +: 'int' and 'str'

To do so, the integer has to be converted into a string:
Code: Select all
print str(1) + "hello"

Now you know the basics of strings and integers in python.
Next up: variables.
Stay tuned for more on this crazy sitcom. :D
Next: http://wololo.net/talk/viewtopic.php?f=37&t=11843
Last edited by Acid_Snake on Wed May 30, 2012 3:25 pm, edited 1 time in total.
"V2h5IGFyZSB5b3UgcmVhZGluZyBteSBzaWduYXR1cmU/\n".decode("base64")
My forum:
Console Heaven
My Homebrews:
pyMenu 0.3.2, multiBootMenu V3, PSvid 3.0, PSP Tools 0.2
User avatar
Acid_Snake
Moderator
 
Posts: 2131
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Postby qwikrazor87 » Fri May 25, 2012 7:45 pm

Advertising
Is that really the outcome in Python when adding two stringed numbers? "1"+"1" outcome 11?
I'm sorry, I'm not a Python programmer. I just tried that out in Lua and it converted the stringed numbers into integers and produced the result as if they were integers in the first place. In my case I tried:
"7" + "8" and the result was 15
qwikrazor87
 
Posts: 2420
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Learning programming with Python

Postby Acid_Snake » Fri May 25, 2012 8:33 pm

qwikrazor87 wrote:Is that really the outcome in Python when adding two stringed numbers? "1"+"1" outcome 11?
I'm sorry, I'm not a Python programmer. I just tried that out in Lua and it converted the stringed numbers into integers and produced the result as if they were integers in the first place. In my case I tried:
"7" + "8" and the result was 15

In python if you use quotes then it becomes string, so "1"+"1" becomes "11"(a one next to a one) the same way "1"+"hello" becomes "1hello", in python integers dont use quotes, so you use 1+1 and it returns a 2.
I recommend you install and use the python interpreter instead of lua cause programming languages are different.
"V2h5IGFyZSB5b3UgcmVhZGluZyBteSBzaWduYXR1cmU/\n".decode("base64")
My forum:
Console Heaven
My Homebrews:
pyMenu 0.3.2, multiBootMenu V3, PSvid 3.0, PSP Tools 0.2
User avatar
Acid_Snake
Moderator
 
Posts: 2131
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Postby Sparky » Fri May 25, 2012 10:08 pm

Why are you using Python 2 and not 3 ?
http://1.337.be/ - PSVita ISO/CSO and Homebrew compatibility
Sparky
 
Posts: 8
Joined: Mon Apr 16, 2012 4:55 pm

Re: [Tutorial] Learning programming with Python

Postby Wdingdong » Sat May 26, 2012 5:55 am

qwikrazor87 wrote:Is that really the outcome in Python when adding two stringed numbers? "1"+"1" outcome 11?
I'm sorry, I'm not a Python programmer. I just tried that out in Lua and it converted the stringed numbers into integers and produced the result as if they were integers in the first place. In my case I tried:
"7" + "8" and the result was 15


Here the '+' sign is not a arithimatic operator of addition of numbers, but it is a concatenation operator which joins two strings.
// Big thanks to people who share information !!!
Wdingdong
 
Posts: 127
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Learning programming with Python

Postby Acid_Snake » Sat May 26, 2012 11:20 am

Sparky wrote:Why are you using Python 2 and not 3 ?

I prefer python 2.7, it has the new stuff from python 3 with the syntax intact. You can however make code compatible with both:
Code: Select all
print("Hello World")

this will work for both.
"V2h5IGFyZSB5b3UgcmVhZGluZyBteSBzaWduYXR1cmU/\n".decode("base64")
My forum:
Console Heaven
My Homebrews:
pyMenu 0.3.2, multiBootMenu V3, PSvid 3.0, PSP Tools 0.2
User avatar
Acid_Snake
Moderator
 
Posts: 2131
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Postby m0skit0 » Tue May 29, 2012 11:10 am

Nice, thanks for the tuto ;) I suggest you linking to next lesson in your first post, because I'm thinking about sticking it ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4800
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Learning programming with Python

Postby Acid_Snake » Wed May 30, 2012 4:38 pm

m0skit0 wrote:Nice, thanks for the tuto ;) I suggest you linking to next lesson in your first post, because I'm thinking about sticking it ;)

Done. thank you :)
"V2h5IGFyZSB5b3UgcmVhZGluZyBteSBzaWduYXR1cmU/\n".decode("base64")
My forum:
Console Heaven
My Homebrews:
pyMenu 0.3.2, multiBootMenu V3, PSvid 3.0, PSP Tools 0.2
User avatar
Acid_Snake
Moderator
 
Posts: 2131
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Postby lacrimosaangel » Sat Dec 29, 2012 4:13 pm

Ooooh thanks for this! Will be reading all of your little tutes :D
Someone did try explaining python to me before but they made it sound so confusing :shock: I'm used to programming with Java so plenty used to strings and integers etc..

Can I use Text pad for Python and then compile and run via cmd prompt or is it best to download a particular python-specific compiler?
PSV 2.02 eCFW TN
psp 3000 6.60 Pro-B10
3DS R4i Gold WoodR4
Wii 4.3E DM2.2+ GC mod
ps2 fat FMCB
iphone 3g 4.2.1jb
Current projects: psp3003 sound reactive LED mod,
User avatar
lacrimosaangel
 
Posts: 112
Joined: Tue Dec 04, 2012 2:11 pm
Location: Kakariko Village, Hyrule

Re: [Tutorial] Learning programming with Python

Postby Acid_Snake » Sun Dec 30, 2012 6:41 pm

python is an interpreted language, it doesn't need compiling (well, actually it does need compiling but that is done by the interpreter at run time so you don't have to worry about that)
"V2h5IGFyZSB5b3UgcmVhZGluZyBteSBzaWduYXR1cmU/\n".decode("base64")
My forum:
Console Heaven
My Homebrews:
pyMenu 0.3.2, multiBootMenu V3, PSvid 3.0, PSP Tools 0.2
User avatar
Acid_Snake
Moderator
 
Posts: 2131
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

Friends

Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita