Computer Science Canada [Python-tut] Python Basics |
Author: | wtd [ Wed Nov 03, 2004 11:16 pm ] | ||||||||||||||||||||||||||||||||||||
Post subject: | [Python-tut] Python Basics | ||||||||||||||||||||||||||||||||||||
What is Python? Python is an interpreted, object-oriented programming language. What do you need?
How do I use the Python interpreter? The Python interpreter is typically run from the command-line. It can either be used to run entire source files, or to interactively run code as you write it. For learning the language, I would recommend running the interpreter interactively. To do so, open a console window. Those using Linux or Mac OS X should know how to do this. Windows users should go to Start -> Run and type "cmd". At the prompt, type "python" and hit enter. Hello, world
Pretty easy, eh? It prints "Hello, world", then gives you a prompt for the next bit of code. Variables There's no need to declare variables in Python. Simply assign to them and they spring into existence.
There are literals for integers, floating point numbers, strings, lists, and dictionaries. Tuples are also there, but they don't come into the picture... yet.
If... elif... else...
Notice that instead of braces or "end", Python uses indentation. You'd probably indent it anywYou can ay, so this isn't terribly onerous. Just make sure you use indents consistently. Other logical operators are ==, !=, "is", "is not", <=, and >=, "or" and "and". To negate anything, use "not". The constants "True", and "False" are also provided. You can also use "in" and "not in" with lists, dictionaries and strings.
When using these with dictionaries, "in" and "not in" checks to see if the dictionary has a key.
Loops Two loops are provided. The most frequently seen is the for loop.
Each element in "hello" is assigned to ch in turn and the loop body is executed. This works the same way on lists as well. It's possible to loop in reverse.
Dictionaries work similarly, though we use the iteritems() method to get both the key and value.
We can also count as we move through a list or string, with the enumerate() function.
While loops are far simpler.
Defining functions Defining functions is easy. A simple example, taking zero arguments.
A function which takes an argument.
We can provide default values for arguments.
We can use the argument name when we call a function. This is great for functions with multiple arguments, when you can't remember which arguments come first.
We can "slurp" up any extra arguments provided into a list.
Oh, and we can slurp up arguments given names that we didn't specify.
|
Author: | Mazer [ Thu Nov 04, 2004 8:31 am ] |
Post subject: | |
YAY it's python! Thanks wtd. |
Author: | wtd [ Thu Nov 04, 2004 2:54 pm ] |
Post subject: | |
Coutsos wrote: YAY it's python! Thanks wtd.
You're quite welcome. |
Author: | Amailer [ Sun Nov 07, 2004 1:39 am ] | ||||
Post subject: | |||||
hey thanks. Now even though im not learing Python, I want to edit this viewCvs scritpt but I can't seem to get the string.replace function to work (i think thats the name of hte function). What I want to do is for every
replace it with
strange, I could do it in php but not rudy (I think i got the location and variabele which is 'html' correct). Thanks |
Author: | wtd [ Sun Nov 07, 2004 4:28 am ] | ||||||||
Post subject: | |||||||||
Amailer wrote: hey thanks.
Now even though im not learing Python, I want to edit this viewCvs scritpt but I can't seem to get the string.replace function to work (i think thats the name of hte function). What I want to do is for every
replace it with
strange, I could do it in php but not rudy (I think i got the location and variabele which is 'html' correct). Thanks Well, in Ruby
In Python
Unfortunately regular expressions aren't as easy in Python, so the Python example isn't as flexible. |
Author: | Shyfire [ Wed Apr 20, 2005 10:20 am ] |
Post subject: | |
wow i'v got a lot of learning to do ![]() this python stuff is a lot more difficult then turing any suggestions on were i should start ![]() cuz i am sooooo lost ![]() |
Author: | wtd [ Wed Apr 20, 2005 2:05 pm ] |
Post subject: | |
fatboi wrote: wow i'v got a lot of learning to do
![]() this python stuff is a lot more difficult then turing any suggestions on were i should start ![]() cuz i am sooooo lost ![]() Actually, I'd wager it's a lot easier to learn than Turing. ![]() You may wish to check out the tutorial at python.org. |
Author: | StealthArcher [ Sat Jan 12, 2008 1:37 pm ] |
Post subject: | Re: [Python-tut] Python Basics |
To an extent yes... but the for loop seems harder to me... |
Author: | SIXAXIS [ Mon Feb 18, 2008 1:17 pm ] | ||
Post subject: | Re: [Python-tut] Python Basics | ||
Quick question for you Python masters. When I enter this:
It always says that foo is greater than 23. What am I doing wrong. BTW, I tried this without having user input. |
Author: | McKenzie [ Mon Feb 18, 2008 1:23 pm ] |
Post subject: | Re: [Python-tut] Python Basics |
Two errors, 1. Use input() for numbers. raw_input() returns a string 2. use elif for your second condition otherwise 50 is both greater than 23 and between 3 and 23. |
Author: | wtd [ Mon Feb 18, 2008 2:25 pm ] |
Post subject: | RE:[Python-tut] Python Basics |
Alternatively, if you can get a string with raw_input, and you know how to turn a string into an integer, then you know full well how to use raw_input to get an integer. |
Author: | Epic Tissue [ Sat Jun 14, 2008 6:09 am ] |
Post subject: | RE:[Python-tut] Python Basics |
Thanks for this tut! Great starting point ![]() |
Author: | juzten [ Mon Sep 08, 2008 3:08 pm ] |
Post subject: | RE:[Python-tut] Python Basics |
Yeah thanks, I am just learning python and this explained alot of my questions |