Advertising (This ad goes away for registered users. You can Login or Register)

[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
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Learning programming with Python, part 1

Post by Acid_Snake » Fri May 25, 2012 4:57 pm

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: viewtopic.php?f=37&t=11843
Advertising
Last edited by Acid_Snake on Wed May 30, 2012 3:25 pm, edited 1 time in total.

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Learning programming with Python

Post by qwikrazor87 » Fri May 25, 2012 7:45 pm

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
Advertising
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Post by 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.

Sparky
Posts: 9
Joined: Mon Apr 16, 2012 4:55 pm

Re: [Tutorial] Learning programming with Python

Post by Sparky » Fri May 25, 2012 10:08 pm

Why are you using Python 2 and not 3 ?

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Learning programming with Python

Post by 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 !!!

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Post by 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.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Learning programming with Python

Post by 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
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Post by 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 :)

User avatar
lacrimosaangel
Posts: 131
Joined: Tue Dec 04, 2012 2:11 pm
Location: Kakariko Village, Hyrule

Re: [Tutorial] Learning programming with Python

Post by 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
Samsung Galaxy Note 2: Root w/freedom
Current projects: psp3003 sound reactive LED mod, PS3 60gb downgrade

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python

Post by 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)

Post Reply

Return to “Programming and Security”