Re: [Tutor] Understanding Classes

2014-01-21 Thread spir

On 01/21/2014 05:20 AM, Christian Alexander wrote:

Alan,

The concept and purpose of classes is starting to sink in a little bit, but
I still haven't had my "Ah-ha" moment yet.  I just can't seem to visualize
the execution of classes, nor am I able to explain to myself how it
actually works.   For example:


 class Person:
 def __init__ (self, name, age):# is self just a placeholder
for  an arbitrary object?  How does __init__ actually work?
 self.name = name # why assign name to
self.name variable?
 self.age  = age   # same as previous
 def salute (self): # again with the
self
 print ("Hello, my name is " + self.name +
 " and I am " + str(self.age) + " years old.")


[PLease avoid top-posting, and quote only the parts of the post you actually 
reply to.]


Yes, self is just a placeholder, as I said in the post you reply to. (I used the 
same word.) It is a placeholder for *whatever object it operates on, now*. If 
you call a Person methon on 'x', then x is assigned to self.


We assign name & age tp self, meaning to x, because a person is defined as a 
composite piece of data {name, age}. Read again the very start of my previous 
post, where I wrote the definition of p1, directly, in an imaginary language:


   p1 = {name="Maria", age=33} # no good python code

This is a programming of a person, as we need. This means, for this application, 
we need a person to be defined by these 2 relevant properties, both of them, and 
nothing more.


__init__ works as if we had programmed that way:

===
class Person:
def init (self, name, age):
self.name = name
self.age  = age
def salute (self):
print ("Hello, my name is " + self.name +
" and I am " + str(self.age) + " years old.")

p1 = Person()
p1.init("Maria", 33)
p1.salute()


except that we don't need to call it. Do you understand the principle of (1) 
defining a function with potential input variables (2) executing it on actual 
input variables? In python the "potential input variables" are called 
"parameters", while "actual input variables" are called "arguments".


There is a similar relation between a class and objects of that class. A class 
defined potential objects, with potential attributes (here name & age); 
"instances" are actual objects of that class with actual attributes.


For the matter, I don't think your difficulties with these notions are related 
to you beeing a visual thinkers: I am, too, and strongly so (and after all, the 
notion of type is firstly visual: think at naive species).


Denis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to print certain elements

2014-01-21 Thread Adriansanchez
Hello everyone,
I am newbie to Python and programming in general. My question is, given a list:
X=['washington','adams','jefferson','madison','monroe']
And a string:
Y='washington,adams,jefferson,madison,monroe'

How would I print washington and monroe using   [:]?
How would I print every element but those two names?
Thanks community!
-A-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Understanding Classes

2014-01-21 Thread Christian Alexander
Alan,

The concept and purpose of classes is starting to sink in a little bit, but
I still haven't had my "Ah-ha" moment yet.  I just can't seem to visualize
the execution of classes, nor am I able to explain to myself how it
actually works.   For example:


class Person:
def __init__ (self, name, age):# is self just a placeholder
for  an arbitrary object?  How does __init__ actually work?
self.name = name # why assign name to
self.name variable?
self.age  = age   # same as previous
def salute (self): # again with the
self
print ("Hello, my name is " + self.name +
" and I am " + str(self.age) " years old.")


On Mon, Jan 20, 2014 at 4:20 PM, spir  wrote:

> On 01/19/2014 10:59 PM, Christian Alexander wrote:
>
>> Hello Tutorians,
>>
>> Looked all over the net for class tutorials
>> Unable to understand the "self" argument
>> Attempting to visual classes
>>
>> I have searched high and low, for easy to follow tutorials regarding
>> classes.  Although I grok the general concept of classes,  I am unable to
>> visually understand what exactly "self" does, or why it is even necessary.
>>   It seems very "magic" to me.  Also I am having the most difficult with
>> the
>> "__init__()" method in classes, and why that is also required.  Keep in
>> mind that I am a visual person (maybe I should have been a graphic
>> designer), therefore most programming concepts flow irritatingly slow for
>> me.
>>
>
> Imagine that for an app you had to define 2 persons p1 & p2 (maybe game
> characters for instance). In an imaginary programming language, a
> definition of p1 could look like this:
>
> p1 = {name="Maria", age=33} # no good python code
>
> This would be a composite piece of data, made of 2 fields (attributes,
> properties...). In python there is no such generic type Object or Composite
> to which such data as p1 could belong. You must define a custom type
> (class) for them, eg:
>
> class Person: pass
>
> Now, you can have p1 of type Person, which is written as if you would call
> the type Person, like a func, to make a new person (this is close to what
> happens):
>
> p1 = Person()
>
> Then, one can define fields on it:
>
> p1.name = "Maria"
> p1.age = 33
> print(p1.name, p1.age)
>
> We could do the same thing for p2:
>
> p2 = Person()
> p2.name = "paulo"
> p2.age = 22
> print(p2.name, p2.age)
>
> Now, say persons are supposed to do things, and all can do the same
> things. To define something all persons can do, you would define it on
> their class (this is the second purpose of a class), eg:
>
> class Person:
> def salute (self):
> print ("Hello, my name is " + self.name +
> " and I am " + str(self.age) " years old.")
>
> As you can see, this method uses the attributes 'name' & 'age' we manually
> defined on both p1 & p2. Then, how does the method, which is defined on the
> type, not on individual objects, know where to find these attributes? You
> are right to say there is some magic at play here. Let us use the method
> first, before explaining:
>
> p1.salute()
> p2.salute()
>
> [Copy-paste & run all this code.] On the first call, we ask the method
> 'salute' to operate on p1, and it writes p1's name & age. Same for p2.
> Inside the method, the attributes are searched on the weird param called
> 'self'. This is just what happens: when calling a method, the object on
> which it operates is assigned to the parameter self. 'self' is just a name
> for the-object-on-which-this-method-operates-now. When it operates on p1,
> self is p1, thus attributes are searched on p1; same for p2. We need some
> placeholder because the method is defined on the type and works for any
> object of this type (any "instance"). [We can define a method on p1 which
> works on p1 only. Maybe try it.]
>
> Finally, if we define a whole range of persons that way, it is annoying to
> set all attributes manually. We could define a method, say 'def_attrs', to
> be called at startup. But python has a specially dedicated method for that,
> which we don't even need to call explicitely, named '__init__'. We will use
> it to set person attributes:
>
> class Person:
> def __init__ (self, name, age):
> self.name = name
> self.age  = age
> def salute (self):
> print ("Hello, my name is " + self.name +
> " and I am " + str(self.age) " years old.")
>
> (yes, init methods typically are as stupid as this; too bad python does
> not do the mechanical job automagically) And this is used like this:
>
> p1 = Person("maria", 33)# no need to call __init__ explicitely
> p1.salute()
> p2 = Person("paulo", 22)# ditto
> p2.salute()
>
> denis
>
> ___
> Tutor maillist  -  Tutor@python.o

Re: [Tutor] Understanding Classes

2014-01-21 Thread Alan Gauld

On 21/01/14 04:20, Christian Alexander wrote:


 class Person:
 def __init__ (self, name, age):# is self just a
placeholder for  an arbitrary object?


Yes. If you create say 4 persons, John, Paul, Ringo and George.
When you call any method on those people objects the same code
in the Person class will get executed. So that method code needs some 
way to know which object is being manipulated. That's all that self is, 
a reference to the object currently being worked on.



How does __init__ actually work?


__init__() is actually just an ordinary metjod like any other.
There is no magic in the method itself. It is a function that takes in 
arhguments (self, name, age in this case) and processes them (stores 
them in the self object).


The magic happens when the Class is instantiated.
So if we create a person object like this:

paul = Person("Paul", 71)

Python first creates a new empty object in memory then passes that 
object to Person.__init() with the arguments (paul, "Paul", 71).
The end result is an instance called paul pre-populated by the arguments 
that we passed to Person.


We can do that manually by not defining __init__()

class Person2:
   def setData(self, name, age):
   self.name = name
   self.age = age

paul = Person2()   # get a new empty Person2 object
paul.setData("Paul", 71)   # do the equivalent of __init__

print (paul.Name, paul.age)



self.name  = name # why
assign name to self.name  variable?


Remember that name is the input parameter to the method.
Just like any other function the input parameters are just
temporary variables that get thrown away at the end of
the function. We want to store those values in our new
instance. So we assign them to a data attribute of the
new object(self) for storage so that we can access them
in the future. For example the salute() method accesses
the self.name attribute. It can only use that if it has
been stored previously.

We can add attributes to an object at any time in Python.
For instance, using our paul example above we can do this:

paul.instrument = "bass"

and later print paul.instrument.

The equivalent of doing that inside a method is to use self.
So we could have another method in Person like this:

class Person:
   def __init__(self, name, age):
   def salute(self):
   def joinBand(self, instrument):
   self.instrument = instrument

and we can call that method as before

paul = Person("Paul", 71)
paul.joinBand("bass")

So setting attributes can be done anywhere and from
any method it's not special to init. But because init
gets called automatically when we first create the
object it's convenient to be able to set the initial
values at the same time as we create the object.

Is that helping?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print certain elements

2014-01-21 Thread Alan Gauld

On 21/01/14 06:18, Adriansanchez wrote:

Hello everyone,
I am newbie to Python and programming in general. My question is, given a list:
X=['washington','adams','jefferson','madison','monroe']
And a string:
Y='washington,adams,jefferson,madison,monroe'

How would I print washington and monroe using   [:]?
How would I print every element but those two names?


The [:] syntax is used for selecting a range of values
from a starting point to a finish.  Its not appropriate
for selecting arbitrary items out of the list.

If you know which items you want you can use a simple
index to access them (remember the first item is index 0)

So to print the first item and the fourth item:

print(X[0],X[3])

In your case it's the first and last so we can do
a similar thing:

print(X[0], X[4])

But for the last element we can alternatively use
a shortcut to save counting the indexes; that's use
an index of -1:

print(X[0],X[-1])

Printing every element except those two is harder.
The simplest approach is to use a loop to process
the list and test each value:

for name in X:
if name not in (X[0], X[-1]):
   print name

For the special case of excluding the first and
last names you could use the [:] notation like
this:

print X[1:-1]

But that only works where you want *all* the
names in a sequence between two end points.

Finally there is a more advanced way of filtering
out items from a list called a list comprehension:

print ( [name for name in X if name not in (X[0],X[-1])] )

Which is pretty much our 'for' loop above, written in
a shorthand single line form.

hth

Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print certain elements

2014-01-21 Thread spir

On 01/21/2014 07:18 AM, Adriansanchez wrote:

Hello everyone,
I am newbie to Python and programming in general. My question is, given a list:
X=['washington','adams','jefferson','madison','monroe']
And a string:
Y='washington,adams,jefferson,madison,monroe'


Side note: you can generate X automatically from Y:

X = Y.split(",")
print(X)

split (as name says) splits a string into a list of substrings. The parameter is 
the separator separating the substrings. In standard (default value), meaning if 
you don't indicate a separator, python uses any whitespace (space, tab, newline) 
possibly repeted.


Denis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Understanding Classes

2014-01-21 Thread Steven D'Aprano
On Mon, Jan 20, 2014 at 09:33:29AM +, Alan Gauld wrote:

> >However, I understand that classes are
> >parallel to that of a blueprint,
> 
> Yes, they define what the objects will look like, what data and what 
> methods they contain. But to use those objects you have to instantiate 
> them and its there that the usage varies slightly from the built in types:
> 
> For built in string type objects you do this
> 
> mystring = 'a new string object'
> 
> But if String were a user defined class you'd need to do this:
> 
> mystring = String('a user defined string object')

Just to be clear here, the difference is a simple matter of syntax. 
Because strings are so common in programming, Python gives you special 
syntax for creating strings. When you have code like this:

'a new string object'

Python still has to create a string object, exactly the same as with a 
custom class. The only difference is that creating strings is handled 
automatically, by the Python interpreter, rather than by you having to 
create your own String class. (Of course you can create a string class 
if you so wish.)


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print certain elements

2014-01-21 Thread eryksun
On Tue, Jan 21, 2014 at 5:18 AM, Alan Gauld  wrote:
>
> for name in X:
> if name not in (X[0], X[-1]):
>print name
>
> For the special case of excluding the first and
> last names you could use the [:] notation like
> this:
>
> print X[1:-1]
>
> But that only works where you want *all* the
> names in a sequence between two end points.
>
> Finally there is a more advanced way of filtering
> out items from a list called a list comprehension:
>
> print ( [name for name in X if name not in (X[0],X[-1])] )

If you're using the `print` function, you can use the * operator to
unpack the items of the list. Add the option sep='\n' to print each
item on a separate line:

items = [name for name in X if name not in (X[0],X[-1])]
print(*items, sep='\n')

To get the `print` function in 2.x, add the following to the top of your module:

from __future__ import print_function
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Understanding Classes

2014-01-21 Thread Steven D'Aprano
On Sun, Jan 19, 2014 at 04:59:58PM -0500, Christian Alexander wrote:
> Hello Tutorians,
> 
> Looked all over the net for class tutorials
> Unable to understand the "self" argument
> Attempting to visual classes

Let me explain classes by analogy. Classes are best as representing 
things, that is, nouns, whether abstract nouns like numbers, or concrete 
nouns like cars. So let's work through an example.

Suppose I have a class Car. You might think of the class as a factory 
which creates cars. We call that factory a class, and the individual 
cars built by the factory "instances". (This analogy doesn't go far 
enough: there is a lot more to the relationship between instances and 
their class, but for now we'll start with this slightly inaccurate 
picture and then improve it later on.)

So the first thing that we do is we need to decide what sort of car the 
factory will produce. I don't mean the brand name and model, I mean, 
what does it do? What parts does it need? Two doors or four? So let's 
make a basic car that does just two things: it starts the engine, and 
stops the engine:

class Car:
def start_engine(self):
if self.gear not in ('Park', 'Neutral'):
raise RuntimeError('cannot start engine')
self.engine.start()

def stop_engine(self):
self.engine.stop()


Not a very interesting car, but all things must start somewhere. Now, 
lets look at those methods, `start_engine` and `stop_engine`. They take 
a single argument, `self`. What does self represent? It represents the 
individual car you wish to operate. You might end up with fifty cars:

car1 = Car()  # create a new car instance
car2 = Car()
car3 = Car()
...

Every one of those cars shares access to the same `start_engine` method. 
So when you call the method:

car23.start_engine()

how does the method know to start car23's engine, instead of car42 or 
car18? It knows because Python takes the bit before the dot (`car23`), 
and automatically passes it to the method as the `self` argument.

Confused? Let's make a brief side-track, and talk about functions. 
Here's a function:

def greet(name, greeting):
print greeting, name

If I call that function:

greet("Fred", "Greetings and salutations")
=> prints "Greetings and salutations Fred"

Python takes the first value, "Fred", and assigns it to the argument 
`name`, the second value "Greetings and salutations" and assigns it to 
the argument `greeting`. Then the function can access them as local 
variables inside the function.

The same applies to methods, with just one additional bit of magic: when 
you call a method like this:

 instance.method(a, b, c)  # say

Python turns it into a function call:

 method(instance, a, b, c)

[For advanced readers: to be precise, Python uses what is technically 
known as an *unbound method*, which it gets by looking inside the class 
of `instance`. To be even more precise, in Python 3, it no longer uses 
unbound methods but just regular functions. But the basic concept is 
the same.]

So, back to our car: we've got a car that has two methods, which 
controls the *behaviour* of the car, but they won't do anything except 
raise an exception, because the car doesn't have any *state*. It has no 
engine to turn on or off, and it has no memory of what gear it is in. So 
let's fix that.

Every individual car needs its own engine, and each car needs to 
remember it's own gear. The obvious time to add the engine is when we 
create the car. Python creates the car when you call the class name as 
if it were a function: 

my_car = Car()  # Python creates the car instance

Now of course, Python doesn't know what cars are supposed to include, so 
it just does the bare minimum, which basically is just to allocate a 
small amount of memory for the instance and a few other bits of 
behind-the-scenes book-keeping. The rest is up to you, and that's what 
the __init__ method is for. So we give the car an engine when we create 
the car, and set the gear at the same time:


class Car:
def __init__(self):
self.engine = Engine()
self.gear = "Neutral"

def start_engine(self):
if self.gear not in ('Park', 'Neutral'):
raise RuntimeError('cannot start engine')
self.engine.start()

def stop_engine(self):
self.engine.stop()



Of course, somebody has to write the Engine class too, but I can't be 
expected to do everything. Let's just pretend it already exists. Now you 
have a class which defines cars. When you call:

my_car = Car()

Python automatically calls the __init__ method with the newly created 
car instance as `self`. The __init__ method then runs, giving the car an 
engine, and setting the gear to neutral. Now, you should be able to 
start and stop the car:

my_car.start()
my_car.stop()


and provided the engine class knows how to start and stop, so will the 
car. So, in conclusion:

- the point of the `__init__` method is that Python automatically calls 
  that method whe

Re: [Tutor] How to print certain elements

2014-01-21 Thread Keith Winston
If you are playing around at the Python prompt (the >>>), which you
really should be to get the hang of this stuff, you might notice that
the bracket indexing that you and everyone is talking about works both
on strings (Y) and on lists (X: in this case, a list of strings). They
may not behave the same way as you expect. Try to imagine what Y[13]
or X[3][3] would get you, and why.

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Understanding Classes

2014-01-21 Thread eryksun
On Tue, Jan 21, 2014 at 7:18 AM, Steven D'Aprano  wrote:
> The same applies to methods, with just one additional bit of magic: when
> you call a method like this:
>
>  instance.method(a, b, c)  # say
>
> Python turns it into a function call:
>
>  method(instance, a, b, c)
>
> [For advanced readers: to be precise, Python uses what is technically
> known as an *unbound method*, which it gets by looking inside the class
> of `instance`. To be even more precise, in Python 3, it no longer uses
> unbound methods but just regular functions. But the basic concept is
> the same.]

You're using 2.x but didn't subclass `object`. Old-style classes
behave differently in many cases, starting with the most basic
difference:

>>> type(my_car)


If you're targeting both 2.x and 3.x, remember to subclass `object` explicitly.

A minor correction about methods: it's a "bound" method:

>>> my_car.start_engine.__self__ is my_car
True

The wrapped function is `__func__`:

>>> type(my_car.start_engine.__func__)

>>> my_car.start_engine.__func__.__name__
'start_engine'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] when is "pythondontwritebytecode" useful?

2014-01-21 Thread Albert-Jan Roskam


>On Mon, Jan 20, 2014 at 5:42 AM, Albert-Jan Roskam  wrote:
>>
>> When is setting a PYTHONDONTWRITEBYTECODE environment variable useful? Or
>> set sys.dont_write_bytecode to True? Or start Python with the -B option?
>> I know what it does
>> (http://docs.python.org/2/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE),
>> i.e. no pyc or pyo fiules are written, but WHY is that sometimes a good
>> thing? The only useful scenario I can think of is when you don't have write
>> rights to create pyc files but you want to use a package anyway.
>
>The bug tracker can provide insight into why a given feature exists,
>or why it's implemented a certain way:
>
>Issue 602345: option for not writing .py[co] files
>http://bugs.python.org/issue602345


Hi Oscar, Eryksun,

Thanks for your replies. Good general tip to check the big tracker. Somehow 
Duckduckgo or Google did not find that page. Glad to read that I was sort of 
right: "Currently python tries to write the .py[co] files even
in situations, where it will fail, like on read-only
mounted file systems."

>> However, I recently opened an EGG file with a zip utility (because I knew
>> egg uses zipimport so it's a zip-like format) and I noticed that there
>> were .pyc files.
>
>Eggs are a binary distribution format. They contain byte-compiled .pyc
>files and extension modules. There's even an option to exclude .py
>source files. The filename should indicate the Python version and
>platform.
>
>Loading extension modules directly from an egg depends on the
>pkg_resources module from setuptools. The modules are extracted to a
>local cache directory. On Windows the default cache directory is
>"%APPDATA%\Python-Eggs", else it uses "~/.python-eggs". You can
>customize this with the environment variable PYTHON_EGG_CACHE.

Hmmm, number of OS * number of Python versions = a lot of packages. Isn't a 
.zip file easiest? Or maybe msi or wininst*) on Windows and .deb on Linux (with 
alien that can easily be converted to e.g. rpm).

*) Last time I tried creating a wininst under Linux it created an .exe file of 
correct/plausible size, but with a huge traceback. Never bothered to try if it 
did work. Maybe it was this: http://bugs.python.org/issue8954. The bug tracker 
is a useful source of information indeed. ;-)

regards,
Albert-Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print certain elements

2014-01-21 Thread Mkhanyisi Madlavana
How would I print washington and monroe using   [:]?
print X[::3]
How would I print every element but those two names?
print X[1::2]


On 21 January 2014 12:18, Alan Gauld  wrote:

> On 21/01/14 06:18, Adriansanchez wrote:
>
>> Hello everyone,
>> I am newbie to Python and programming in general. My question is, given a
>> list:
>> X=['washington','adams','jefferson','madison','monroe']
>> And a string:
>> Y='washington,adams,jefferson,madison,monroe'
>>
>> How would I print washington and monroe using   [:]?
>> How would I print every element but those two names?
>>
>
> The [:] syntax is used for selecting a range of values
> from a starting point to a finish.  Its not appropriate
> for selecting arbitrary items out of the list.
>
> If you know which items you want you can use a simple
> index to access them (remember the first item is index 0)
>
> So to print the first item and the fourth item:
>
> print(X[0],X[3])
>
> In your case it's the first and last so we can do
> a similar thing:
>
> print(X[0], X[4])
>
> But for the last element we can alternatively use
> a shortcut to save counting the indexes; that's use
> an index of -1:
>
> print(X[0],X[-1])
>
> Printing every element except those two is harder.
> The simplest approach is to use a loop to process
> the list and test each value:
>
> for name in X:
> if name not in (X[0], X[-1]):
>print name
>
> For the special case of excluding the first and
> last names you could use the [:] notation like
> this:
>
> print X[1:-1]
>
> But that only works where you want *all* the
> names in a sequence between two end points.
>
> Finally there is a more advanced way of filtering
> out items from a list called a list comprehension:
>
> print ( [name for name in X if name not in (X[0],X[-1])] )
>
> Which is pretty much our 'for' loop above, written in
> a shorthand single line form.
>
> hth
>
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print certain elements

2014-01-21 Thread Mark Lawrence

On 21/01/2014 10:24, Mkhanyisi Madlavana wrote:

How would I print washington and monroe using   [:]?
print X[::3]
How would I print every element but those two names?
print X[1::2]


On 21 January 2014 12:18, Alan Gauld mailto:alan.ga...@btinternet.com>> wrote:

On 21/01/14 06:18, Adriansanchez wrote:

Hello everyone,
I am newbie to Python and programming in general. My question
is, given a list:
X=['washington','adams','__jefferson','madison','monroe']
And a string:
Y='washington,adams,jefferson,__madison,monroe'

How would I print washington and monroe using   [:]?
How would I print every element but those two names?


The [:] syntax is used for selecting a range of values
from a starting point to a finish.  Its not appropriate
for selecting arbitrary items out of the list.

If you know which items you want you can use a simple
index to access them (remember the first item is index 0)

So to print the first item and the fourth item:

print(X[0],X[3])

In your case it's the first and last so we can do
a similar thing:

print(X[0], X[4])

But for the last element we can alternatively use
a shortcut to save counting the indexes; that's use
an index of -1:

print(X[0],X[-1])

Printing every element except those two is harder.
The simplest approach is to use a loop to process
the list and test each value:

for name in X:
 if name not in (X[0], X[-1]):
print name

For the special case of excluding the first and
last names you could use the [:] notation like
this:

print X[1:-1]

But that only works where you want *all* the
names in a sequence between two end points.

Finally there is a more advanced way of filtering
out items from a list called a list comprehension:

print ( [name for name in X if name not in (X[0],X[-1])] )

Which is pretty much our 'for' loop above, written in
a shorthand single line form.

hth



If you must top post please get your facts right.

In [1]: X=['washington','adams','jefferson','madison','monroe']

In [2]: print(X[::3])
['washington', 'madison']

In [3]: print(X[1::2])
['adams', 'madison']

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor