Scientific Linux Forum.org



  Reply to this topicStart new topicStart Poll

> Python 5
Jessica_Lily
 Posted: Jul 6 2012, 07:42 PM
Quote Post


SLF IRC Team
**

Group: Members
Posts: 47
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)
CODE

num = int(raw_input("Number: "))
if num < 100 and num > 50:
print "The number you entered is okay!"
else:
print "I want a number greater than 50 and less than 100"


10)
CODE

num = int(raw_input("Number: "))
if num < 0:
print "You've entered a negative number!"
elif num <= 100 and 0 <= num:
# I didn't specify if it included 100 or not so ether num <= 100 or num < 100 is correct.
print "You've entered a small number!"
else:
# You could  have done elif num > 100 however it's not needed, else works well here.
print "You've entered a huge number"


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. []
CODE

>>> my_empty_list = []
>>> type(my_empty_list)
<type 'list'>


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:
CODE

>>> fruit = ["Green Apples", "Green Grapes", "Oranges"]


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:
CODE

>>> fruit = ["Green Apples", "Green Grapes", "Oranges"]
>>> fruit_entered = raw_input("Fruit: ")
Fruit: Green Apples
>>> if fruit_entered in fruit:
...     print "YAY! You like the same fruit as me"
... else:
...     print "Awhh :("
...
YAY! You like the same fruit as me
ntered is a amongst my favorite fruit?


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.

CODE

if fruit_entered in fruit:
   print "It's in the list"
else:
   fruit.append(fruit_entered)


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:
CODE

>>> numbers = []
>>> print numbers
[]
>>> numbers.append(7)
>>> print numbers
[7]
>>> numbers.append(2)  
>>> numbers.append(-27)
>>> print numbers
[7, 2, -27]
>>>


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.
CODE

>>> numbers = [7, 2, 9, 4, -6, 8, 924734, -45, 1, 0]
>>> numbers[0]
7
>>> numbers[1]
2
>>> numbers[2]
9
>>> numbers[-1]
0
>>> numbers[-5]
8
>>> numbers[-4]
924734
>>> print numbers
[7, 2, 9, 4, -6, 8, 924734, -45, 1, 0]
>>>


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 smile.gif)
PMUsers Website
^
log69
 Posted: Jul 6 2012, 09:34 PM
Quote Post


SLF Member
***

Group: Members
Posts: 94
Member No.: 1325
Joined: 24-February 12









I don't mean to be offtopic, but have you ever tried ruby? smile.gif
PM
^
tux99
 Posted: Jul 6 2012, 10:02 PM
Quote Post


SLF Moderator
********

Group: Moderators
Posts: 1276
Member No.: 224
Joined: 28-May 11









QUOTE (log69 @ Jul 6 2012, 11:34 PM)
I don't mean to be offtopic, but have you ever tried ruby? smile.gif


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... dry.gif wink.gif

--------------------
My personal SL6 repository, specialized in audio/video software: http://pkgrepo.linuxtech.net/el6/
(can be used together with EPEL and ELRepo repositories) - repository mirror: http://linuxsoft.cern.ch/linuxtech/el6/
PM
^
log69
 Posted: Jul 7 2012, 11:59 AM
Quote Post


SLF Member
***

Group: Members
Posts: 94
Member No.: 1325
Joined: 24-February 12









QUOTE (tux99 @ Jul 6 2012, 11:02 PM)
QUOTE (log69 @ Jul 6 2012, 11:34 PM)
I don't mean to be offtopic, but have you ever tried ruby? smile.gif


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... dry.gif wink.gif


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.
PM
^
zxq9
 Posted: Jul 8 2012, 12:54 AM
Quote Post


SLF Geek
****

Group: Members
Posts: 321
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.
QUOTE (log69 @ Jul 6 2012, 11:34 PM)
I don't mean to be offtopic, but have you ever tried ruby? smile.gif

@ 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.
PMEmail PosterUsers Website
^
Jessica_Lily
 Posted: Jul 10 2012, 11:49 AM
Quote Post


SLF IRC Team
**

Group: Members
Posts: 47
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.
PMUsers Website
^
zxq9
 Posted: Jul 10 2012, 11:36 PM
Quote Post


SLF Geek
****

Group: Members
Posts: 321
Member No.: 611
Joined: 5-August 11









QUOTE (Jessica_Lily @ Jul 10 2012, 11:49 AM)
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.

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!"
PMEmail PosterUsers Website
^
wearetheborg
 Posted: Aug 10 2012, 03:28 AM
Quote Post


SLF Geek
****

Group: Members
Posts: 249
Member No.: 18
Joined: 11-April 11









QUOTE (zxq9 @ Jul 7 2012, 07:54 PM)
J I relish the idea that such a relatively small distro as SL incites such wonderful community participation even at the level of language tutorials.



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 ph34r.gif

--------------------
PM
^
zxq9
 Posted: Aug 10 2012, 05:55 AM
Quote Post


SLF Geek
****

Group: Members
Posts: 321
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)
PMEmail PosterUsers Website
^
wearetheborg
 Posted: Aug 10 2012, 06:01 AM
Quote Post


SLF Geek
****

Group: Members
Posts: 249
Member No.: 18
Joined: 11-April 11









I'm dead. I am SO dead.

--------------------
PM
^
zxq9
 Posted: Aug 10 2012, 06:04 AM
Quote Post


SLF Geek
****

Group: Members
Posts: 321
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!
PMEmail PosterUsers Website
^
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll