The Student Room Group

What does print() mean in python ???

What does print() mean in python ???
what does it do?
It's a function in Python 3.0 (For printing things funnily enough - including blank spaces)


What are you trying to do?
(edited 9 years ago)
Original post by crookie
What does print() mean in python ???
what does it do?


I think it displays whatever is in the () on screen as text.

So print("HelloWorld") would display the text HelloWorld

If you didn't put the "" it would print the contents of the variable instead
So if you declared X = Hello, then print(X) would display the text Hello


This is what I think anyway, I've don't use Python though :P
(edited 9 years ago)
Reply 3
Because Google is too hard to utilize.
Reply 4
Original post by MilitantMindset
It's a function in Python 3.0 (For printing things funnily enough - including blank spaces)


What are you trying to do?


basically, i've been given this code:

def fib(n):

////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b

(it produces the fibonacci sequence until n)

and i have to add the missing line of code in to make it work

i think the missing line is print() (i just googled it :tongue:)

so like this:
def fib(n):
////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b
/////print()

but i have to explain why and how it's print() and i don't really know...:frown:

like does print() close the loop or something??
(edited 9 years ago)
Original post by crookie
basically, i've been given this code:

def fib(n):

a, b = 0, 1
while a < n:
print(a)
a, b = b, a+b

(it produces the fibonacci sequence until n)

and i have to add the missing line of code in to make it work

i think the missing line is print() (i just googled it :tongue:)

so like this:
def fib(n):
a, b = 0, 1
while a < n:
print(a)
a, b = b, a+b
print()

but i have to explain why and how it's print() and i don't really know...:frown:


I might be mistaken, but assuming you don't have to call the function, I'm pretty sure that would give you a new line. So think about it in terms of pseudocode

You define the function

Obtain the variables a and b, set them to 0 and 1 respectively

Now while a is less then n

a is printed

a is redefined to b and b becomes a+b

new line


This continues until a exceeds n
Original post by crookie
basically, i've been given this code:

def fib(n):

////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b

(it produces the fibonacci sequence until n)

and i have to add the missing line of code in to make it work

i think the missing line is print() (i just googled it :tongue:)

so like this:
def fib(n):
////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b
/////print()

but i have to explain why and how it's print() and i don't really know...:frown:

like does print() close the loop or something??


I think the last line should be:

fib()

With the number of numbers you want within the brackets. So for the first 5 numbers of the sequence your code would be:

def fib(n):
////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b
/////fib(5)
Reply 7
Original post by MilitantMindset
I might be mistaken, but assuming you don't have to call the function, I'm pretty sure that would give you a new line. So think about it in terms of pseudocode

You define the function

Obtain the variables a and b, set them to 0 and 1 respectively

Now while a is less then n

a is printed

a is redefined to b and b becomes a+b

new line


This continues until a exceeds n



Yeah I do call the function, eg:
fib(10)
If I set n to 10 I'm supposed to get the numbers of the sequence below 10 so: 0,1,1,2,3,5,8

do you think the missing line could be to call the function??


I don't know if you can see this picture but the missing line is print() here

(https://docs.python.org/3/tutorial/controlflow.html#defining-functions)
Reply 8
Original post by crookie
basically, i've been given this code:

def fib(n):

////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b

(it produces the fibonacci sequence until n)

and i have to add the missing line of code in to make it work
As far as I can tell, it already works.

def fib(n):
a,b=0,1
while a<n:
print(a)
a,b=b,a+b


Then fib(10) (for example) produces

0
1
1
2
3
5
8

(If you've got Python 3.something then adding the end=' ' bit to the print make the numbers come out in a horizontal list rather than a vertical one.)

What is wrong with the current output? (Should it be horizontal rather than vertical? Should it start at 1 rather than 0?) Without knowing that, there's no way to tell what changes you need to make.
(edited 9 years ago)
Reply 9
Original post by harr
As far as I can tell, it already works.

def fib(n):
a,b=0,1
while a<n:
print(a)
a,b=b,a+b


Then fib(10) (for example) produces

0
1
1
2
3
5
8

(If you've got Python 3.something then adding the end=' ' bit to the print make the numbers come out in a horizontal list rather than a vertical one.)

What is wrong with the current output? (Should it be horizontal rather than vertical? Should it start at 1 rather than 0?) Without knowing that, there's no way to tell what changes you need to make.


I literally have no clue what's wrong with the current one, i wasn't told
I know that the current one (eg fib(10)) gives:
0
1
1
2
3
5
8
the horizontal/vertical thing isn't the issue, neither is the 0/1 thing

It's supposed to be a really easy question with not much to it so it must be really simple

I think the missing line is either print() or calling the function
Original post by crookie
Yeah I do call the function, eg:
fib(10)
If I set n to 10 I'm supposed to get the numbers of the sequence below 10 so: 0,1,1,2,3,5,8

do you think the missing line could be to call the function??


I don't know if you can see this picture but the missing line is print() here

(https://docs.python.org/3/tutorial/controlflow.html#defining-functions)


I'm not a expert in this (learned some C last year)


It would make more sense to me, when you mentioned the print(), I assumed that would simply print a blank line, so when you run the program you would get the terms of the Fibonacci sequece running vertically rather than across the page, but that seems like a superficial point.
(edited 9 years ago)
Reply 11
Original post by crookie
I literally have no clue what's wrong with the current one, i wasn't told
I know that the current one (eg fib(10)) gives:
0
1
1
2
3
5
8
the horizontal/vertical thing isn't the issue, neither is the 0/1 thing

It's supposed to be a really easy question with not much to it so it must be really simple

I think the missing line is either print() or calling the function
What is the exact wording of the question? If it's from somewhere online, would it be possible to link to it?

You can try different variations on print(), but it's only going to change how the output is displayed. Without any instructions on how the output should be displayed, it's not obvious that it's possible for it to be any more correct than it is already.

It's possible that they are just looking for you to say that you need to call the function (though in that case it's a bit of an odd question).
Original post by MilitantMindset
I'm not a expert in this (learned some C last year)


It would make more sense to me, when you mentioned the print(), I assumed that would simply print a blank line, so when you run the program you would get the terms of the Fibonacci sequece running vertically rather than across the page.
print() by itself does print a blank line, but in this case it's actually outside the loop (Python does everything based on indentation), so it just puts a new line at the end of the list.
(edited 9 years ago)
Reply 12
Original post by harr
What is the exact wording of the question? If it's from somewhere online, would it be possible to link to it?

You can try different variations on print(), but it's only going to change how the output is displayed. Without any instructions on how the output should be displayed, it's not obvious that it's possible for it to be any more correct than it is already.

It's possible that they are just looking for you to say that you need to call the function (though in that case it's a bit of an odd question).print() by itself does print a blank line, but in this case it's actually outside the loop (Python does everything based on indentation), so it just puts a new line at the end of the list.


this is the exact wording of this question, it's on a paper (no link, sorry):

def fib(n):
////a, b = 0, 1
////while a < n:
////////print(a)
////////a, b = b, a+b

1) As it stands this code will not run. Add in an extra line of code to fix it. Explain why you have added this line.

I think they might just want me to say that you need to call the function...

Btw, thanks for all your help guys!
Original post by harr

It's possible that they are just looking for you to say that you need to call the function (though in that case it's a bit of an odd question).print() by itself does print a blank line, but in this case it's actually outside the loop (Python does everything based on indentation), so it just puts a new line at the end of the list.


Didn't realise that the structure itself affects how the code is read in Python, that's actually quite nice
Reply 14
Original post by crookie
I think they might just want me to say that you need to call the function...
I think you're probably right. I can't think of any plausible alternatives.
Original post by MilitantMindset
Didn't realise that the structure itself affects how the code is read in Python, that's actually quite nice
Opinions vary, but I think it's pretty cool. Never having to read unindented or poorly indented code makes things much easier.

Compared to C, Python is very readable in general.
It just prints the new line in the output i guess if you are in a loop...
Original post by sanidhya16
It just prints the new line in the output i guess if you are in a loop...


This is nearly a 3-year old thread, but 'print' has nothing to do with being in a loop. it's just a function, and it obeys all the normal rules of functions. Have a look here: https://www.learnpython.org/en/Functions
Original post by winterscoming
This is nearly a 3-year old thread, but 'print' has nothing to do with being in a loop. it's just a function, and it obeys all the normal rules of functions. Have a look here: https://www.learnpython.org/en/Functions
Oh Okay. Gotcha! Thanks.
hey there,
print() : It By default the print() function in python ends with a newline.
In Python print() function comes with a parameter called "end" and by default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character or string using this parameter.

1. for example
print( "Welcome to" , end = ' ' )
print("the world", end = ' ')

output: Welcome to the world

2. for example
print( "Welcome" , end = '@' )
print( "world" )
output: Welcome@world

THANK YOU.
(edited 6 years ago)
Just type fib() to run the function thats all

Quick Reply

Latest

Trending

Trending