Python Forum
issue with syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: issue with syntax (/thread-8438.html)

Pages: 1 2


issue with syntax - volcano - Feb-20-2018

please see this code : https://prnt.sc/iheuot

there is ":" after "for"

but there is no ":" after "print"

why ?


RE: issue with syntax - buran - Feb-20-2018

please, post code in code tags, no screenshots/images


RE: issue with syntax - snippsat - Feb-20-2018

Post in code tags,no picture.
Read BBcode help


RE: issue with syntax - mepyyeti - Feb-20-2018

@volcano
Short answer is because some languages like Python (and Ruby) use whitespace (enter/return) to signify end of lines and new lines. Other languages such as php, javascript, go, c++, etc use semicolons. This is what you're mostly likely thinking of.

In python the colon is necessary after
if/elif/else/if not:
,
with open(filename) as f:
,
for:
and
while:
loops. Your example involves a for loop
for f in foods:
. Hence the colon.

Also, as of python3, print is a function as
print('foo')
will output
Output:
foo
. Also
bar = moo
print(f'{bar}')
outputs
Output:
moo
--but this is a slightly advanced topic.

The BBcodes are also super helpful . The ones most commonly used are [(/)python] open(close)tag, [(/)error] open(close) tag.

So you example would look like this:
foods = ['bacon','eggs','etc']
for f in foods:
    print(f)
    print(len(f))
hopefully the admins can amend this post to make its vernacular a bit more pythonic... Wink


RE: issue with syntax - buran - Feb-20-2018

we are not going to edit your post to make it 'more pythonic'. That is not what we do. However, note that we didn't answer not because we were not able to help, but to make OP post their code according to rules.


RE: issue with syntax - mepyyeti - Feb-20-2018

(Feb-20-2018, 09:23 PM)buran Wrote: we are not going to edit your post to make it 'more pythonic'. That is not what we do. However, note that we didn't answer not because we were not able to help, but to make OP post their code according to rules.

Oops. Sorry. Didn't mean to step on any toes. Meant well. Apologies.

However, it is possible the op didn't know how to use the BBcodes. So I reposted it according to the rules. Just thought it was a pretty good question (from the op's pov).


RE: issue with syntax - volcano - Feb-21-2018

(Feb-20-2018, 09:04 PM)mepyyeti Wrote: @volcano
Short answer is because some languages like Python (and Ruby) use whitespace (enter/return) to signify end of lines and new lines. Other languages such as php, javascript, go, c++, etc use semicolons. This is what you're mostly likely thinking of.

...................

hopefully the admins can amend this post to make its vernacular a bit more pythonic... Wink

Thanks ... You are very helpful and a Python genious for sure . ...Thansks for your help & time man.

This is my first post in the forum !

I have one more question in this regard.

I have C/C++/JAVA & Fortran background. I see "{" ,"}" there . Does python dont use brackets ?

I'm learning this language ..and I dont see brackets in any of the example code. They are mostly using indentation.

I'mnot sure if this language use brackets . Could you please confirm ?

Thanks for the time.


RE: issue with syntax - buran - Feb-21-2018

python uses indentation to designate blocks of code, not braces.
curly braces denote a dict
if you have a programming background you may check some crash course or this book http://books.goalkicker.com/PythonBook/


RE: issue with syntax - volcano - Feb-22-2018

(Feb-21-2018, 04:14 PM)buran Wrote: python uses indentation to designate blocks of code, not braces.
curly braces denote a dict
if you have a programming background you may check some crash course or this book http://books.goalkicker.com/PythonBook/
Wall

Its very hard to use indentation... Is there any alternative to this ?


RE: issue with syntax - buran - Feb-22-2018

(Feb-22-2018, 06:47 AM)volcano Wrote: Its very hard to use indentation... Is there any alternative to this ?
Well, it's subjective. We think it's very intuitive and results in clean and readable code. There is no alternative.