![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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'] 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 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 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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#4 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
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) |
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#8 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#9 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|