Posts: 84
Threads: 36
Joined: Jul 2017
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! ")
Posts: 8,165
Threads: 160
Joined: Sep 2016
yes, in python the indentation is used to separate blocks of code
Posts: 2,953
Threads: 48
Joined: Sep 2016
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.
Posts: 8,165
Threads: 160
Joined: Sep 2016
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
Posts: 84
Threads: 36
Joined: Jul 2017
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! ")
Posts: 8,165
Threads: 160
Joined: Sep 2016
yes, i will take values from 0 to 17, incl. i.e. 18 is not included
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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).
Posts: 2,342
Threads: 62
Joined: Sep 2016
Java does not have named arguments, unfortunately, except for with annotations.
Posts: 2,342
Threads: 62
Joined: Sep 2016
(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.
Posts: 45
Threads: 27
Joined: Jul 2017
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 '''.
|