Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issue with syntax
#1
please see this code : https://prnt.sc/iheuot

there is ":" after "for"

but there is no ":" after "print"

why ?
Reply
#2
please, post code in code tags, no screenshots/images
Reply
#3
Post in code tags,no picture.
Read BBcode help
Reply
#4
@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
Reply
#5
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.
Reply
#6
(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).
Reply
#7
(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.
Reply
#8
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/
Reply
#9
(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 ?
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Python 2 to 3 syntax issue OscarBoots 11 5,815 May-21-2019, 05:30 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020