
| This forum is proudly powered by Scientific Linux 6 | SL website Download SL Help Search Members |
| Welcome Guest ( Log In | Register ) | Resend Validation Email |
![]() ![]() ![]() |
| Jessica_Lily |
Posted: Jul 6 2012, 07:42 PM
|
|||||||||||||||||
![]() SLF IRC Team ![]() ![]() ![]() Group: Members Posts: 95 Member No.: 180 Joined: 15-May 11 |
Hello again folks!
Previous Lessons Lesson 1 - Basic Maths & Core Data Types Lesson 2 - Binary / Binary Storage & Variables Lesson 3 - Input & Output (IO) Lesson 4 - Conditions & If's Lesson 4's Answers 1) True 2) False 3) False 4) True 5) False 6) True 7) False 8) False 9)
10)
Intro This lesson We'll go over lists. I decided to only do lists as they are in virtually every single python program and a place where bugs do creep in. Being able to look at a list and know in your head exactly how to use them will help you. Lists So what are these things I speak of that I call 'lists'. They are another data type but unlike the last ones we looked at. This is made from those data types. It's really as it sounds. Like we have in everyday life shopping lists, todo lists, etc... this is just a list of "stuff". This stuff can be numbers, strings, bools, anything. They can be of any length really (including empty). They are used all over in programming for plenty of different uses (Will get to later with some examples). So how do I make a list in python. Well you use the square brackets. []
This is not very helpful so how do i make a list with values in it? Well lets say I wanted to make a list of my favoruite fruit:
Okay now that's all well and good but, how does that help us, well... maybe we want to check if a fruit someone in the list:
Before we would have done an if fruit_entered == "Green Apples", then elif/or it'd be a mess and then if my tastes changed it'd be a pain to change as they'd be hard coded into if statements. So, say you wanted to ask for their favorite fruit, if it's there tell them that, if not, add it to the list.
So what's going on with this .append business? Well it's called a method and their used a lot. You take the variable name and to <variable name>.<method>(<arguments>) An argument simply is some data you want to use with it so in respect to append you tell it what you want added/appended to the list. Usually the methods are words which are what you want to do so (Google's Definiton). So you use this with name_of_list.append(data) lets show some more examples:
Take note of the order too, their in a specific order. with append they get added to the end. This means also you can take input from a user and add it to lists. The reason I make a point of saying this information is when I said you have to be good at working out how to work with this list, sometimes the list is hypothetical and has to be imagined as doesn't really exist until your software is running. So, this is all well and good but how can i access one item in this list (an item is called an element to most programmers). Well programmers start counting at 0 so list[0] is the very first element in the list. list[1] is the second element. list[2] is the third etc... Note you can also go from the back, now this obviously isn't -0 as negative 0 is nonsensical but -1 list[-1] is the last element. list[-2] is the second to last list[-3] is the third from the back. etc... You get the idea, a live example in the interpreter then.
My suggestion now as a good task it's easy to read but is harder until you've got use to it so, make your own list in python and predict the number you need to put in the [] to get different elements. Do both thinking of a number to put in [] and then work out what element it is and also think of what element you want and then work out the number to put in []. You will need to be good at doing both ways when writing python code. Getting the wrong element can cause bugs, potential security holes. I will finish lists off in the next tutorial, this in my opinion is very important and I don't want to overload the tutorials (expect the questions posted below in the next few days ) |
|||||||||||||||||
| log69 |
Posted: Jul 6 2012, 09:34 PM
|
|
![]() SLF Member ![]() ![]() ![]() Group: Members Posts: 97 Member No.: 1325 Joined: 24-February 12 |
I don't mean to be offtopic, but have you ever tried ruby?
|
|
| tux99 |
Posted: Jul 6 2012, 10:02 PM
|
|||
|
SLF Guru ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1124 Member No.: 224 Joined: 28-May 11 |
I think your question is very much off topic and IMHO a bit rude. This is like asking an Italian master chef, who just served you a delicious Italian meal on the house, if he (actually she) can do a cheeseburger instead... -------------------- My personal SL6 repository, specialized in audio/video software: http://pkgrepo.linuxtech.net/el6/
|
|||
| redman |
Posted: Jul 7 2012, 10:35 AM
|
|||
![]() SLF Admin ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Admins Posts: 1672 Member No.: 2 Joined: 8-April 11 |
Good to have you back -------------------- What is SL? - Forum Rules - Info on 3rd Party Repos - How to post images - How to post large text / config files
Desktop: Asus P5QPL-AM, Intel Dual-Core E6500, 4GB DDR2, Asus GeForce GT 430 1GB, SL6.4 x86_64 Test box: Intel S5000PSL, 2x Intel Xeon E5310, 8GB ECC DDR2 FB-Dimm, Asus GeForce GT 220 1GB, SL6.4 x86_64 |
|||
| log69 |
Posted: Jul 7 2012, 11:59 AM
|
|||||
![]() SLF Member ![]() ![]() ![]() Group: Members Posts: 97 Member No.: 1325 Joined: 24-February 12 |
Well, no offense, I'm purely interested in everyone's opinion regarding python vs ruby. I may learn from it. Everything has its pros and cons, so I don't plan to insist and say anything about which is good and why. I know what I prefer, but I would find it interesting to know if she prefers python over ruby or she's never played with it before. @Jessica: Good article, thanks for sharing. |
|||||
| zxq9 |
Posted: Jul 8 2012, 12:54 AM
|
|||
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 369 Member No.: 611 Joined: 5-August 11 |
JL: Nice post! Folks get pretty turned around sometimes using iterables in any language. Fortunately actually iterating over iterables in Python is really easy. I'm curious to see how you tackle tuples and dicts and tie them in later on. I wonder if it wouldn't be helpful to provide an occasional C example to bounce an idea off of to highlight exactly what is so nice about dynamic languages (esp. Python) VS managing it all yourself?
Anyway, I'm a big fan of your series. Please keep it up. Every so often you'll state something in a very clear way that I can't express well to a complete newcomer and I can refer them here instead. I relish the idea that such a relatively small distro as SL incites such wonderful community participation even at the level of language tutorials.
@ log69: Man, I like you, but I've got to bite you for this. It is indeed rude to spawn Lang A vs Lang B discussions in tutorial threads about Lang A. The Lovely Lily Herself has gone way, way out of her way to post a continuation of her Python series at the expense of great effort. To attack your question directly I started this thread. I would hope that all such discussion moves there instead of here. |
|||
| Jessica_Lily |
Posted: Jul 10 2012, 11:49 AM
|
|
![]() SLF IRC Team ![]() ![]() ![]() Group: Members Posts: 95 Member No.: 180 Joined: 15-May 11 |
Thanks for your responses. I'm glad you guys are enjoying them, I regret not to have more time for them.
@log69: I have played with it a little but but not enough to feel I can make a fair comparison between two. Sorry. @zxq9 Thanks. I could include some C examples I'm not sure if that's wise or not though, it will have a lot of unfamiliar syntax and different ways of doing things. Often in C you write code based upon your understanding of how your data is physically laid out in memory. I might consider it, I do love C as a language and I use it enough to probably write some apt examples but I'm not sure. Ether way, thanks for your compliments. @everyone: Hopefully the next few days i'll have the next tutorial written. Sorry I have a lot of university work to do in preparation for next second year so finding time to write a tutorial which will actually be of some use to people is difficult. |
|
| zxq9 |
Posted: Jul 10 2012, 11:36 PM
|
|||
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 369 Member No.: 611 Joined: 5-August 11 |
I think these differences would be the real meat of showing different implementations. That is highly pertinent considering that C is the extension module language for Python anyway and the language of the canonical Python interpreter implementation. Some simple examples of C vs Python make one think "Ah, its easy either way" while other situations make one think "Ah, I'm so glad I don't have to write that all in C anymore!" |
|||
| wearetheborg |
Posted: Aug 10 2012, 03:28 AM
|
|||
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 352 Member No.: 18 Joined: 11-April 11 |
If you want to see redman get all mad and frothy at the mouth, make a request for a PL related subforum here. Dont say that I gave you the idea though -------------------- |
|||
| redman |
Posted: Aug 10 2012, 05:50 AM
|
|||||
![]() SLF Admin ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Admins Posts: 1672 Member No.: 2 Joined: 8-April 11 |
-------------------- What is SL? - Forum Rules - Info on 3rd Party Repos - How to post images - How to post large text / config files
Desktop: Asus P5QPL-AM, Intel Dual-Core E6500, 4GB DDR2, Asus GeForce GT 430 1GB, SL6.4 x86_64 Test box: Intel S5000PSL, 2x Intel Xeon E5310, 8GB ECC DDR2 FB-Dimm, Asus GeForce GT 220 1GB, SL6.4 x86_64 |
|||||
| zxq9 |
Posted: Aug 10 2012, 05:55 AM
|
|
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 369 Member No.: 611 Joined: 5-August 11 |
Oh, so we're getting a programming sub forum?
(if I say it like that, I can foster an assumption and put a huge smile on redman's face) |
|
| wearetheborg |
Posted: Aug 10 2012, 06:01 AM
|
|
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 352 Member No.: 18 Joined: 11-April 11 |
I'm dead. I am SO dead.
-------------------- |
|
| zxq9 |
Posted: Aug 10 2012, 06:04 AM
|
|
![]() SLF Advocate ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 369 Member No.: 611 Joined: 5-August 11 |
Well, not all the way dead just yet! Just soon-to-be encaphalographically challenged (that's the politically correct way to say "dead" today). So party on!
|
|
| redman |
Posted: Aug 10 2012, 06:15 AM
|
|
![]() SLF Admin ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Admins Posts: 1672 Member No.: 2 Joined: 8-April 11 |
Ok, we had our fun here.
Please stick to the subject and stop the chit chat (this isn't the lounge) -------------------- What is SL? - Forum Rules - Info on 3rd Party Repos - How to post images - How to post large text / config files
Desktop: Asus P5QPL-AM, Intel Dual-Core E6500, 4GB DDR2, Asus GeForce GT 430 1GB, SL6.4 x86_64 Test box: Intel S5000PSL, 2x Intel Xeon E5310, 8GB ECC DDR2 FB-Dimm, Asus GeForce GT 220 1GB, SL6.4 x86_64 |
|
![]() |
![]() ![]() ![]() |