Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Tutorial >> 'print', output and variables

Holiverh's izHawt Python....

Tutorial >> 'print', output and variables

Postby Holiverh » Sat Aug 15, 2009 7:24 pm

For this tutorial you will need the interactive Python shell IDLE.

The 'print' statement in Python is the easiest to get your head around. Print is used to output to the screen. The print sytax, like most Python code, is extremly simple:
Code: Select all
print "This is foo."
Call the print statement, and enclose the desiered output in inverted commas.(Don't forget the whitespace between 'print' and output!)
If you wern't aware we chose to ouput a string[1] in the above example. But you can esencially display any data type through the same method:
Code: Select all
print 10
'Okay this is great but what if i want to out two different things at the same time?' Python has a very simple way of handling concantination(sticking output together). The + sign is used to attach once peice of output to another:
Code: Select all
print "Hello my name is " + "Oliver."
This would have the same affect as:
Code: Select all
print "Hello my name is Oliver."
Rather simple ay? So now you must be asking yourself; 'Strings are all well and good but what would happen if i used integers[2]?'. Now ere come de smart bit. Python knows what data type you are using, it detects it internally, so it knows when your using strings, and it knows when your using numbers:
Code: Select all
print 5 + 5
This will return the number 10 because Python detected you are using numbers, thus the + is used mathimatically.
Code: Select all
print "5" + "5"
5 points if you can predict what the output would be... "55"!!! This is because Python detected you are using strings(see footnotes for detection of strings) and used the + as a concantenation operator instead of a mathmatical operator.
All of this may rather simple but it gets a little bit harder. What if you want to mix data types?:
Code: Select all
print "My mental age is " + 2
This will return a type error; "Cannot concantenate 'str' and 'int' objects". This is because you have confused Python; You tell Python your working with strings but you're also using integers, so how is python ment to know what to do. Are you trying to do math or concantenation? Because we all know you can't add numbers to letters can you? :) However this error is easily overcome by enclosing your integer in a str() function:
Code: Select all
print "My mental age is " + str(2)
This converts the integer to a string so it can be attached to the other one. Also note that this works on other data types not just integers.

Now we've done the print satement and outptting to the screen we are going to move onto argubly the most important thing about any programming language, variables. If you've ever done algebra you'll understand variables alot quicker. Variables are place holders basically, it's a way of storing and labeling data so it can be used later on. Python allows you to name anything, anything[3]; strings, integers, lists, functions, you name, you can assign it to a variable. I'll start with a basic example:
Code: Select all
x = 50
print x
In this example we assign 50 to a variable called 'x'. Note in Python variables don't have to be predefined[4]. Then we displayed the content of 'x' by printing it to the screen. If you don't see the point of this tell me which would be easier to write:
Code: Select all
print 19865
print 19865
print 19865
print 19865
print 19865
Or
Code: Select all
a = 19865

print a
print a
print a
print a
print a
This means we can preform basic algebra using variables to hold the numbers:
Code: Select all
x = 10
y = 20
a = 2

print (y - a) * x
This is the equivelent of:
Code: Select all
print (20 - 2) * 10
'Hay wait a moment! You said variables makes things easier, but there's less code in the second example!' True but what if we wanted to access that same data later on, we'd have to write it all out again, and you wouldn't be able to do what i'm about to show ya!
You can change the contents of a variable at any time. And it's so simple to do:
Code: Select all
s = "Hello world!"
print s
s = "This is foo."
print s
That would output:
Hello world!
This is foo.
Yet we only used the one varible, and that's because we rewrote it. Because you can't assign more than one thing to a varible at one given time it is over written with the new data.

And for the grand finale we will combine everything we've learnt into one!:
Code: Select all
x = 14
y = 7

print "The sum of " + str(x) + " and " + str(y) + " is: " + str(x+y)

x = 2
y = 21

print "The sum of " + str(x) + " and " + str(y) + " is: " + str(x+y)
If there is something you don't fully understand or would like clarification, feel free to post a reply to this thread, or alternately contact me through PM or even Email if your incapable of using the PM system! :P :lol:

Footnotes:
[1]: A string is a data type that is identifyed by being enclosed in inverted commas or speach marks; ""
[2]: A integer is a data type that is a whole number.
[3]: There are rules to what a variable can be called. Some generic words are reserved like 'print' for obvious reasons. Also a variable name must start with a letter or underscore and may only contain letters, underscores and numbers. Variables are also cases sensitive; foo is different to FOO.
[4]: In other, normally compiled languages all varibles must be decleared so the compiler knows what data it will be dealing with.


-------------------------------------------------------------------------------------------------------------
Image

Python tutorial: 'print' and variables by Oliver Haddock is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 UK: England & Wales License. Permissions beyond the scope of this license may be available at ottajay@googlemail.com.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Tutorial >> 'print', output and variables

Postby Dink » Sat Aug 15, 2009 9:26 pm

Great tutorial Holiverh!!! :D

With
Code: Select all
print y - a *x
and
Code: Select all
print 20 - 2 * 10


you need to include a set of brackets so that the order of operations works correctly (BODMAS/BEDMAS).

I.e.
Code: Select all
print (y - a) * x
and
Code: Select all
print (20 - 2) * 10
:ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tutorial >> 'print', output and variables

Postby Holiverh » Sat Aug 15, 2009 9:34 pm

Dink wrote:Great tutorial Holiverh!!! :D

With
Code: Select all
print y - a *x
and
Code: Select all
print 20 - 2 * 10


you need to include a set of brackets so that the order of operations works correctly (BODMAS/BEDMAS).

I.e.
Code: Select all
print (y - a) * x
and
Code: Select all
print (20 - 2) * 10
:ugeek:
Thank you, i hope it was easy to understand and follow.
However i'm 99.9% sure there's no need for brackets, Python will happily execute the calculation as it is.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tutorial >> 'print', output and variables

Postby Dink » Sat Aug 15, 2009 9:38 pm

The answer is 0 when I run it, which isn't what you want is it?
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tutorial >> 'print', output and variables

Postby Holiverh » Sat Aug 15, 2009 9:49 pm

Dink wrote:The answer is 0 when I run it, which isn't what you want is it?
Hmm, you seem to be right... Odd, i'll ammend them sections of code.(Before anyone else notices :P)
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tutorial >> 'print', output and variables

Postby Unknown98 » Sun Aug 16, 2009 5:49 am

I noticed! :p. you need to update that in the tut on the main site too.

Keep them coming Holiverh, I am starting to understand Python a bit more now...

What kind of programs is it possible to make with python? And can you get data from an outside source, such as a currency converter like what Replacement did with the currency converter he made for Informatist?

What plugins are there and what do they do/add? What exactly is a library?

I can make good UI's with save, open buttons like on MS word, and I can make menus like file, edit, correct?
http://www.salesmangame.com
We have an IRC channel! #Salesman on coldfront
User avatar
Unknown98
 
Posts: 97
Joined: Sun Jul 12, 2009 11:30 am
Location: Houston

Re: Tutorial >> 'print', output and variables

Postby Dink » Sun Aug 16, 2009 5:54 am

Unknown98 wrote:I noticed! :p. you need to update that in the tut on the main site too.


It already is, no?

Unknown98 wrote:What plugins are there and what do they do/add? What exactly is a library?

I can make good UI's with save, open buttons like on MS word, and I can make menus like file, edit, correct?


From what I understand everything is module based, so there is a networking module, irc module, Tk module to easily make GUI's etc... :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tutorial >> 'print', output and variables

Postby Unknown98 » Sun Aug 16, 2009 6:10 am

Hmm... ok. I'll wait for Holly bush to come on :p
http://www.salesmangame.com
We have an IRC channel! #Salesman on coldfront
User avatar
Unknown98
 
Posts: 97
Joined: Sun Jul 12, 2009 11:30 am
Location: Houston

Re: Tutorial >> 'print', output and variables

Postby Holiverh » Sun Aug 16, 2009 6:15 am

Unknown98 wrote:I noticed! :p. you need to update that in the tut on the main site too.

Keep them coming Holiverh, I am starting to understand Python a bit more now...

What kind of programs is it possible to make with python? And can you get data from an outside source, such as a currency converter like what Replacement did with the currency converter he made for Informatist?

What plugins are there and what do they do/add? What exactly is a library?

I can make good UI's with save, open buttons like on MS word, and I can make menus like file, edit, correct?

Firstly...DRAT! :lol:

Python is a very diverse language it can handle just about anything. It's original purpose was to script the web like Php, perl and ruby does, but it's also widly used to do al sorts. The great thing about Python is, it's a widely used language, support for it is higly accesable. Many APIs have Python wrappers(http requests and decodes the returned data).

I don't really get what you mean by a plug-in. Python has 1000s of libraries wating for you to use but in the Pyuniverse they're known as Modules. Libraries are static files that store functions which you can call at any time. I'm currently writing my first module, it's a general shortcut library because i'm lazy.[lol] To see a list of all avalible Python modules vist the 'Python package index' or PyPi for short.

As i said above, python offers tons of libraries! And lots of UI ones, funnily i was looking at the list the other day, forgot the link though. I hear wxPython is a very good cross platform GUI module, i only use Tk, the standard library because i tend to stick to CLIs.

EDIT: For quick pick up and play GUIs, easyGUI is good, it removes the event driven element from it.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tutorial >> 'print', output and variables

Postby Unknown98 » Sun Aug 16, 2009 7:58 am

Ok... firstly, is it possible to make a game world with Python? Apply textures etc? I heard of something called PyGame?? Could I create a cround, place buildings, cars, make rain, etc.? Import models from Gmax or similar program?

And secondly, I understand the varibles now. But they are useless to me unless they are dynamic. Example:

How do I make an input box where you type your name. Then assign that value to a varible called "name" so that I can use "name" later on?
http://www.salesmangame.com
We have an IRC channel! #Salesman on coldfront
User avatar
Unknown98
 
Posts: 97
Joined: Sun Jul 12, 2009 11:30 am
Location: Houston

Next

Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred