Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No curly braces in python?
#1
Hi everyone. I'm brand new to python. I'm used to the Java programming language, which has opening and closing curly braces marking exactly where the beginning and end of loops and functions are, but so far it appears that Python doesn't use curly braces. So my question is:

in the following code, is the statement on line 9 outside of the for loop just by me taking it out of the indentation with the other statements:

# SamsSrings.py

name = input("What's your name? ")

#print the inputted name 18 times:
for x in range(18):
   #print the name followed by a space, not a new line
   print(name, end = " ")
print("rules! ")
Reply
#2
yes, in python the indentation is used to separate blocks of code
Reply
#3
It's out of the loop.
The indentation is making the code blocks in Python.

def say():
    print("Hello")
    print("pyguys")

print("Hello again")
Here the third print function is not a part of the function definition.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
for x in range(3):
  #print the name followed by a space, not a new line
   print('first line of loop body, x = {}'.format(x)) #loop body
   print('second line of loop body, x = {}'.format(x)) #loop body
print('line outside of loop body, x = {}'.format(x)) #out of loop body
Output:
first line of loop body, x = 0 second line of loop body, x = 0 first line of loop body, x = 1 second line of loop body, x = 1 first line of loop body, x = 2 second line of loop body, x = 2 line outside of loop body, x = 2
Reply
#5
Does my comment in line 6 interpret the for loop statement correctly:

# SamsSrings.py

name = input("What's your name? ")

#print the inputted name 18 times:
for i in range(18): #for(int i = 0; i < 18; i++)
   
   print(name, end = " ")#end is a special python keyword
print("rules! ")
Reply
#6
yes, i will take values from 0 to 17, incl. i.e. 18 is not included
Reply
#7
(Jul-25-2017, 08:15 AM)RedSkeleton007 Wrote:
for i in range(18): #for(int i = 0; i < 18; i++)
   
   print(name, end = " ")#end is a special python keyword

end is not a special python keyword. It's just a keyword argument to the print() function. This should feel familiar, as Java also has keyword arguments (possibly called "named" arguments).
Reply
#8
Java does not have named arguments, unfortunately, except for with annotations.
Reply
#9
(Jul-25-2017, 08:15 AM)RedSkeleton007 Wrote: Does my comment in line 6 interpret the for loop statement correctly:
The short answer is yes (which you can verify empirically), the long answer is that there is an object which lazily generates the values. It's similar to (though not the same as) an Iterator<Integer> in Java.
Reply
#10
I am learning Python as well. Coming for other language, I found it odd at first without braces in function, Pytjom uses indents instead, takes a bit to get uas to.
Another odd thing is code comments, unlike c, Swift... Python uses # for single line """ or '''.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output set items without the curly braces ? jimthecanadian 3 2,913 May-11-2019, 07:02 AM
Last Post: perfringo
  search and replace dots inside curly braces kchinnam 1 2,642 May-01-2018, 06:53 PM
Last Post: Larz60+
  Are braces rather than indentation ever wanted? michaelcollier 22 14,160 May-02-2017, 08:37 AM
Last Post: volcano63
  JSON API / no braces marucho21 1 3,876 Feb-01-2017, 07:35 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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