new to python, like really new.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mendy dan
    New Member
    • Feb 2012
    • 2

    new to python, like really new.

    hi, I just started python, I'm 14 years old and new experience in any programming whatsoever. I recently got a new machine and I want to make use of it. so instead of gaming like I have done in the past,I decided I'll start doing some programming. I am currently running Linux Mint 12. I have python 2.7.

    now here is the syntax problem. I'll show you it. of course this isn't the exact thing, Because I made it simpler.

    a = 1 + 1
    b = 2 + 2
    c = a + b

    print C

    here's the problem.

    >>>if 6:
    ... print "value of C is:"

    now something isn't right there. now you see that I'm trying to print the value of it if its right but I don't know how.

    if you can help, that would be great.


    By the way, if anyone here who is a professional programmer and wouldn't mind helping me with a few other things, please email me @ xx@xxx

    Thanks.
    Last edited by bvdet; Feb 9 '12, 01:31 AM. Reason: Remove email address
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I removed your email address for your protection. Since you are a member, you can private message other people. Feel free to ask any questions you may have about programming! We will try to help in any way possible.

    I am not a professional programmer. I do structural steel detailing and write Python scripts to facilitate my work in the 3D model.

    The statement if 6: will always evaluate True just like if 0: will always evaluate False. You will want to learn how to format strings for printing to stdout.
    Code:
    >>> a = 1 + 1
    >>> b = 2 + 2
    >>> c = a + b
    >>> if c == 6:
    ... 	print "value of C is: %s" % (c)
    ... 	
    value of C is: 6
    >>>
    BV - Moderator

    Comment

    • mendy dan
      New Member
      • Feb 2012
      • 2

      #3
      bvdet,

      thanks for editing it. I'm sorry I failed to follow that rule (just read it now)

      as for that code you gave me, are you sure that is the only way of doing it? (with the %?) what is % even used for?

      thanks,
      mendy.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        There is a pretty good explanation of the string format operator "%" at http://infohost.nmt.ed u/tcc/help/pubs/lang/pytut/str-format.html.

        You can print the result by issuing multiple print statements or string concatenation, but generally those methods are slower.

        Code:
        print "value of C is: " + str(c)
        
        print "value of C is:",
        print c
        It's generally a good idea to issue only one print statement where possible. Use string method join() to create a long string from string fragments instead of concatenation.
        Code:
        >>> stringlist = ["Sentence 1", "Sentence 2"]
        >>> print "\n".join(stringlist)
        Sentence 1
        Sentence 2
        >>>
        String formatting is very important. The best time to learn how to use it is now!

        Comment

        Working...