464,822 Members | 1,042 Online
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

# new to python, like really new.

 P: 2 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. Feb 9 '12 #1
3 Replies

 Expert Mod 2.5K+ P: 2,851 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. Expand|Select|Wrap|Line Numbers >>> 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 Feb 9 '12 #2

 P: 2 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. Feb 9 '12 #3

 Expert Mod 2.5K+ P: 2,851 There is a pretty good explanation of the string format operator "%" at http://infohost.nmt.edu/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. Expand|Select|Wrap|Line Numbers 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. Expand|Select|Wrap|Line Numbers >>> 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! Feb 9 '12 #4