By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
3 Replies

bvdet
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
  1. >>> a = 1 + 1
  2. >>> b = 2 + 2
  3. >>> c = a + b
  4. >>> if c == 6:
  5. ...     print "value of C is: %s" % (c)
  6. ...     
  7. value of C is: 6
  8. >>>
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

bvdet
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
  1. print "value of C is: " + str(c)
  2.  
  3. print "value of C is:",
  4. 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
  1. >>> stringlist = ["Sentence 1", "Sentence 2"]
  2. >>> print "\n".join(stringlist)
  3. Sentence 1
  4. Sentence 2
  5. >>> 
String formatting is very important. The best time to learn how to use it is now!
Feb 9 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.