Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 19th, 2005, 2:22 AM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Python - A Programmers Introduction

Python - A Programmers Introduction

This is an introduction to Python. Instead of being for people new to
programming, it is for programmers who want to learn the language as
painlessly as possible.

Python is a Scripting Language. It's homepage is http://www.python.org
From there you can download the lastest release of Python.


Running Code
A great thing about Python is the Interpretor. You can type a bit of
code in and it executes it straight away. It is very useful for testing
quick bits of code.
You can use the interpretor, or you can make a file and save it as .py
you can run .py files using the Interpretor. Or on Linux, type the command

python filename.py


Comments
Comments in Python are shown by the hash symbol (#)
 #I am a comment i do not affect the code


Variables
When you declare a variables in Python, it takes an educated guess at
what type it is.

String
my_string = "Hello World!"

Integer
my_int = 12

Float
my_float = 2.3

Long
my_long = 10L

Mulitple assignment
[code]var_1,var_2,var_3=1,2,3


Output
The most simple form of output is the print function.
It displays content to the screen.

It's syntax is: print content

Example:
print my_int #prints 12
print mystring, my_float #prints Hello World! 2.3
print "Hello" #prints hello
print 5 #prints 5
print "I like\nNewlines" # \n makes a new line


Lists
my_list = ['hello',1,2,3,'coldDeath']
That makes a new list, lists can contain any variable types, even other lists.
Note that the start with 0.
print my_list #prints ['hello',1,2,3,'coldDeath']
print my_list[4] #prints 'coldDeath'


Operations
You can perform many operations to variable.

Examples:
my_int * 10 #prints 120
my_float + 11.2 #prints 13.5
10 / 5 #prints 2
100 - 54 #prints 46
my_string + "!!!!!" #prints Hello World!!!!!!


If statements
Python uses whitespace, it makes code clearer and requires less typing.
For those coming from C++ or the like, Python doesn't have a switch statement
so you must use if statements, which is just as easy.

Example:
if my_int < 15: #True because my_int is 12
	print"my_int was less than 15"
elif my_int >= 16: #Else if statement
	print"my_int was greater than or equal to 16"
else: #Else statement
	print"my_int must be 15"


While loops
While loops are the easiest type of loop in Python.

Example:
count = 0 #integer
while count < 10:
	print count #print the variable
	count += 1	#Count = Count + 1
That would print out the numbers 1 - 10.


For loops
For loops in Python are different to most languages, the best way to see how they
work is to see an example!

Example:
for i in range(1,11): #range (1,11) means 1,2,3,4,5,6,7,8,9,10,11
	print i

Easy!


Functions
Functions in Python are easy, we use the keyword def to show we are defining
a function, then we have the function name then a bracket, the arguments
and another bracket.

def checkforprime(n):
	if (n < 2):
		return False
	for i in range(int(sqrt(n)) + 1)[2:]:
		if n % i == 0:
			return False
	return True
That takes a number as its argument and returns true if it is prime, and false if it isn't.
We call a function like this:
checkforprime(99)


Useful Stuff
range(1,10) #make a list from 1 to 10
pass #does nothing at all. Used when a statement requires no action
len(variable) #prints the length of the variable in the argument


Further Reading
If you want to go further with Python, you should read this tutorial:
http://docs.python.org/tut/tut.html
Remember that this tutorial doesn't cover much of the Python language,
but it should help you on your way.

Have fun!, sorry if i left anything out or made any mistakes.
coldDeath is offline   Reply With Quote
Old Aug 19th, 2005, 7:21 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Looks pretty good for someone coming from another programming language. You may wish to PM Big K and ask him to move this to the tutorials section.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 19th, 2005, 7:59 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Correct me if I am wrong... I haven't tinkered with Python as much as most of you.

If I wanted a block of code within my if statement to be executed... the indentations MUST be there. Correct? Would it be wrong to enclose the block in { }?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 19th, 2005, 8:02 AM   #4
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Yeah the only way that you tell blocks is by the white space. Python actually just looks for the whitespace so it would by like this
if true:
     do this
     do this
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Aug 19th, 2005, 8:18 AM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
If I wanted a block of code within my if statement to be executed... the indentations MUST be there. Correct? Would it be wrong to enclose the block in { }?
Yeah, precisely as Big K said. The colons are only part of the syntax because the creators of Python noticed that it really does add to readability, and gives writers of auto-indenters an easier time.
The amount of whitespace you use is up to you, the interpreter is smart enough to latch on to the fact you're using two spaces for your blocks, or a tab character, etc. The Python style guide, however, recommends you use four spaces (and also that you put spaces after each argument to a function, except the last argument :p)
Cerulean is offline   Reply With Quote
Old Aug 19th, 2005, 8:24 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Man, that kinda sucks... I like my { }. Personally, using the { } instead of a single : followed by indentation makes it more readable to me. I remember the days when I had to write code on grid paper to make sure the columns matched up correctly, because the language based its compile procedures depending on which column you were in, I forgot what language that was though... it was around the earlier 90s though.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 19th, 2005, 8:53 AM   #7
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
I think Python is easy to read and write. HOWEVER, if you ever have to run code that has lost it's formatting, it is a pain in the ass to get back the correct indentation. At least with braces, the end of a block is obvious.
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Aug 19th, 2005, 9:28 AM   #8
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
I should have said about whitespace, but i didn't think twice about it.

Also i forgot the [/code] in the Multiple assignment bit, so the output part got stuck in the code tags.
coldDeath is offline   Reply With Quote
Old Aug 19th, 2005, 9:31 AM   #9
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
IR, RPG you have to have things in the right columns so the compiler knows what everything does.

http://www.99-bottles-of-beer.net/la...g-400-497.html

there might be another tho
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Aug 19th, 2005, 10:07 AM   #10
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
The spaces versus {} trips up a lot of programmers when they first start using python. But it is amazing how quickly you adjust and then actually start to like it.
Moldz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC