Random Homebrew: PSPacman
Pacman for your PSP!
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

[Tutorial] Learning programming with Python, part III

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 III

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

Part III
Now that your more familiar with Python's syntax lets see some statements.

if, elif, else:
The if statement is used to execute some code when a special condition is met:
Code: Select all
x = True
if x == True:
  print "Hello World"

as you can see the text "Hello World" is only printed if the variable X is equal to the boolean True, so if x is not equal to True then "Hello World" will not be printed:
Code: Select all
x = False
if x == True:
  print "Hello World"

If that's the case then we're better off using the else statement after the if statement:
Code: Select all
x = True
if x == True:
  print "Hello World"
else:
  print "Goodbye World"

It's pretty simple, if x is True then print "Hello World", otherwise print "Goodbye World". Now the else statement doesn't mean x needs to be False, it can be anything as long is it's not True:
Code: Select all
x = 2
if x == 1:
  print "Hello World"
else:
  print "Goodbye World"

This mean that if x is anything but 1 it prints "Goodbye World".
Then we have the elif statement, which is a combination of else if:
Code: Select all
x = 1
if x == 1:
  print "Hello"
elif x == 2:
  print "Goddbye"
else:
  print "Unrecognised number"

This will help you put more than one code depending on the situation of the variable x.
Another example:
Code: Select all
x = True
y = 1 if x == True else 2

This is the same as before, but more compact.

input() and raw_input() :
We have seen the print statement, but now lets see the input() and raw_input() functions.
Both functions let you ask the user for an input, whether hitting the enter button or typing something:
Code: Select all
x = raw_input("Enter your name > ")
print "Your name is " + x

while raw_input() is used to input strings only, input() can be used to input strings, integers, lists, tuples, bools, etc:
Code: Select all
>>> x = input("type a string > ")
type a string > "hello"
>>> y = input("type an integer > ")
type an integer > 1
>>> z = input("type a list > ")
type a list > ["hello", 1]
>>> print x
hello
>>> print y
1
>>> print z
['hello', 1]


while, for, in:
The while statement is used similarly to the if statement, it gets executed if a certain condition is met, but it's a loop, this means the code is going to be executed over and over again until that condition is no longer met:
Code: Select all
x = True
while x == True:
  y = raw_input("finish? y/n >")
  if y == "y":
    x = False

it can also be terminated with the break statement:
Code: Select all
x = True
while x == True:
  y = raw_input("finish? y/n >")
  if y == "y":
    break

The for statement is mainly used to execute code individually for each item in a group:
Code: Select all
x = ["hello", "world"]
for i in x:
  print i

and the results:
Code: Select all
hello
world


See you in the next lesson...

Previous: viewtopic.php?f=37&t=11843
Next: viewtopic.php?f=37&t=11904
"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: 2039
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Advertising

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest