Is this the right forum for Python questions ?

I am a newbie, just studying Python.
I am certain,and I know that the skills are here to answer my rudimentary questions, but is there a better forum perhaps ?

One forum that I saw via Google that seems fairly active is this:
Python Programming Forums

What do you think ?

thanks

Ok, I am going to ask it anyway.

I saw this as a Python print statement.

n = “nut”
b = “bolt”
print “This is a nut %s, and this is a bolt %s.”** %** (n,b)

I am not sure that I have it exactly right but the part I can’t find an explanation for is the %
that is all by itself. I have underlined and bolded it.
What the heck is it there for. What is it telling the print function ?

I’ve never used Python, and only dabbled in C, C++, Qt, etc, but it looks like “%s” tells the print command to insert a string. I assume the first string will be n (“nut”) and the second one b (“bolt”).

It’s the % that is underlined that is a puzzle to me.
If I leave it out, I get an error.

In this case ‘%’ is an operator like ‘+’ or ‘-’, it is the “String Formatting” operator (although ‘%’ is also the modulo operator when used with numbers).
In python ‘%’ does the work of replacing “%s” (or similar) in a string with an argument.

So, f.e. “This is a %s” % “test” would result in the string “This is a test”.

Here this is needed because print just prints the string as it is:

Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="test"
>>> print a
test
>>> a="This is a %s"
>>> print a
This is a %s
>>> print a % "test"
This is a test
>>> a="This is a %s" % "test"
>>> print a
This is a test
>>>

Have a look, this works:

a = "nut"
b = "bolt"
print 'This is a', a, b

It outputs

This is a nut bolt

:smiley:

Of course there’s always more than one way to do a certain thing.
But the question here was what this ‘%’ is about… :wink:

On 2013-09-19, hextejas <hextejas@no-mx.forums.opensuse.org> wrote:
>
> It’s the % that is underlined that is a puzzle to me.

That’s just determined determined by the settings of your text editor.

> If I leave it out, I get an error.

The % operator is overloaded to perform two routinely used functions in Python.

  1. As a format operator (ala `sprintf’ in C):

Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "%s %d 	 %3.2f" % ("Eleven",11,1.1111)
Eleven 11        1.11
>>>

  1. As a modulus operator (not remainder):

Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 % 3
0
>>> 8 % 3
2
>>> -8 % 3
1
>>>

HTH

Cheers. Got me looking into python :smiley:

On 2013-09-19, Knurpht <Knurpht@no-mx.forums.opensuse.org> wrote:
> Cheers. Got me looking into python :smiley:

Hehe, caught the bug?

My only quibble with Python is its absence of switch-case statements.

On 2013-09-19 15:49, flymail wrote:
> On 2013-09-19, Knurpht <Knurpht@no-mx.forums.opensuse.org> wrote:
>> Cheers. Got me looking into python :smiley:
>
> Hehe, caught the bug?
>
> My only quibble with Python is its absence of switch-case statements.

It doesn’t? Wow. Now I will never learn to use it :-p


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

On 2013-09-19, Carlos E. R. <robin_listas@no-mx.forums.opensuse.org> wrote:
> It doesn’t?

Apparently it’s not pythonic', whatever that means. The recommended approach is using dictionaries. I find that a contrived obfuscated workaround and hardly easy to read, certainly less so then the inelegant but inevitable use of sticking a lot of elif’ blocks together.

> Wow. Now I will never learn to use it :-p

Hehe :). Stick to ya Delphi!

Isn’t the correct term “pythonesque”? rotfl!

Oh oh, it sounds like I can skip the section dealing with dictionaries if flymail thinks that they are dumb.

On 2013-09-20, hextejas <hextejas@no-mx.forums.opensuse.org> wrote:
> Oh oh, it sounds like I can skip the section dealing with dictionaries
> if flymail thinks that they are dumb.

No! Dictionaries are useful… just don’t use them to replace switch…case statements.

On 2013-09-20 14:36, hextejas wrote:
> Oh oh, it sounds like I can skip the section dealing with dictionaries
> if flymail thinks that they are dumb.

Isn’t there a saying that if you go to the moon, do as the locals do? :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

That’s a new one to me Carlos. I think I remember it as:
“When in Rome, do as the Romans do”. :stuck_out_tongue:

On 2013-09-20 17:36, hextejas wrote:
>
> robin_listas;2586393 Wrote:
>> On 2013-09-20 14:36, hextejas wrote:
>>> Oh oh, it sounds like I can skip the section dealing with dictionaries
>>> if flymail thinks that they are dumb.
>>
>> Isn’t there a saying that if you go to the moon, do as the locals do?
>> :slight_smile:

> That’s a new one to me Carlos. I think I remember it as:
> “When in Rome, do as the Romans do”. :stuck_out_tongue:

That one - but in Spanish the city is different.


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

On 2013-09-20, Carlos E. R. <robin_listas@no-mx.forums.opensuse.org> wrote:
> Isn’t there a saying that if you go to the moon, do as the locals do? :slight_smile:

Beware of religion. There are many fanatics. I know C coders who scorn upon C++ coders. I know C++ coders who scorn upon
Python coders. But I know Python coders who scorn upon C/C++ coders for ending lines in whatever language with `;', thus
completing the circle.

In the end, the only important thing is to get the job done. Use what works. No language is perfect. I feel very
ignorant for not touching web-based code. But I code assembler and inline assembler to perform routines faster than any
other equivalent. Does that make me happy? Sometimes, if only when it means my students can go home in time for dinner
or to meet their wife/husband/child that much sooner.

Still, I have no webpage because I don’t know how to make one. I haven’t approached any degree of competency in PHP,
Javascript, or HTML5. It makes me feel ignorant. But even though I’m ignorant, I disagree with the following assertion:

On 2013-09-20, Carlos E. R. robin_listas@no-mx.forums.opensuse.org wrote:

do as the locals do? :slight_smile:

No. I say use what works. Coding itself is not an end. It can be fun. When you’ve done it for a long time it becomes
less fun. If it ever stopped being fun at all however, I’d stop coding. If I was told I must follow a given template to
`do as the locals do’, I would also stop. That’s not fun. I preferred C/C++ to Pascal because there was more ways to do
things. Ultimately I believe the coder is best position to decide how to use code to fulful a specific task moreso than
the language itself. In my opinion, it’s up to the coder to decide to use one or more languages to perform a desired
task.

But if you’re talking about visiting different countries throughout all the continents, I’d entirely agree :).

On 2013-09-20 19:58, flymail wrote:
> On 2013-09-20, Carlos E. R. <robin_listas@no-mx.forums.opensuse.org> wrote:
>> Isn’t there a saying that if you go to the moon, do as the locals do? :slight_smile:
>
> Beware of religion. There are many fanatics. I know C coders who scorn upon C++ coders. I know C++ coders who scorn upon
> Python coders. But I know Python coders who scorn upon C/C++ coders for ending lines in whatever language with `;', thus

completing the circle.

Oh, yes, I know.

(Pascal for ever!) :stuck_out_tongue:

On 2013-09-20, Carlos E. R. robin_listas@no-mx.forums.opensuse.org wrote:

do as the locals do? :slight_smile:

No. I say use what works. Coding itself is not an end. It can be fun. When you’ve done it for a long time it becomes
less fun. If it ever stopped being fun at all however, I’d stop coding. If I was told I must follow a given template to
`do as the locals do’, I would also stop. That’s not fun. I preferred C/C++ to Pascal because there was more ways to do
> things. Ultimately I believe the coder is best position to decide how to use code to fulful a specific task moreso than
> the language itself. In my opinion, it’s up to the coder to decide to use one or more languages to perform a desired
> task.

My meaning is to do as the rest of the programmers in that language
usually do, use the same constructs. It probably works better in that
language, and others will understand better your code.

That saying, I don’t follow my own advice that much. I hate obfuscated
C, I write it like if I were writing pascal somehow.

> But if you’re talking about visiting different countries throughout all the continents, I’d entirely agree :).

:slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)