New to Python; Command equivalents

Dang Griffith noemail at noemail4u.com
Wed Nov 5 13:32:42 CET 2003


On 4 Nov 2003 23:30:01 -0800, rooting_j00 at hotmail.com (Code_Dark)
wrote:

>Hi, I'm new to the Python programming language, and eager to learn. I
>know C and C++, but I'm sure you all know that feeling when you just
>_need_ a new language. I chose python because I'd heard good things
>about it. I have python 2.3 installed on my computer, and I am glad to
>say that I've created a "Hello, World" program. Right, the only
>problem is that the only command I've been able to figure out is
>'print'. That's quite a problem. I was just wondering if someone could
>give me the  command for 'cin' or 'scanf' in the C-based languages,
>and if it is required to define variabled before using them as in the
>C based languages (such as ;int numbofPizza' in the beginning of the
>program). Sorry for asking such newbieish questions, but I am, in
>fact, a newbie. Please reply to this, or email me at
>dshaw858 at hotmail.com ... the email address I use for my newsgroups
>(rooting_j00 at hotmail.com) isn't regularly checked because of spam...
>meh.
>
>Thanks in advance,
>
>- Code Dark
The raw_input() function is roughly equivalent to cin:
   >>> x = raw_input()
   Hello, world!
   >>> print x
   Hello, world!

raw_input() always returns string, so you will need to parse it
according to your requirements.  I usually use regular expressions if
I'm getting arbitrary input and need to break it into parts.  This is
roughly equivalent to scanf.  You can of course do "manual" string
parsing.  The split() method is useful if you just need it broken into
separate tokens.
   >>> y = x.split(" ")
   >>> print y
   ['Hello,', 'world!']
   >>>

   But John is right--reading the tutorial will probably give you more
insight than this direct response.  Good luck--I hope you also learn
that you love Python more than C/C++.
      --dang




More information about the Python-list mailing list