What does print() mean in python ??? Watch

crookie
Badges: 2
Rep:
?
#1
Report Thread starter 4 years ago
#1
What does print() mean in python ???
what does it do?
0
reply
MilitantMindset
Badges: 6
Rep:
?
#2
Report 4 years ago
#2
It's a function in Python 3.0 (For printing things funnily enough - including blank spaces)


What are you trying to do?
0
reply
Zargabaath
Badges: 21
Rep:
?
#3
Report 4 years ago
#3
(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
1
reply
Async
Badges: 19
Rep:
?
#4
Report 4 years ago
#4
Because Google is too hard to utilize.
1
reply
crookie
Badges: 2
Rep:
?
#5
Report Thread starter 4 years ago
#5
(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 )

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...

like does print() close the loop or something??
0
reply
MilitantMindset
Badges: 6
Rep:
?
#6
Report 4 years ago
#6
(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 )

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...
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
0
reply
Zargabaath
Badges: 21
Rep:
?
#7
Report 4 years ago
#7
(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 )

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...

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)
0
reply
crookie
Badges: 2
Rep:
?
#8
Report Thread starter 4 years ago
#8
(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/c...ning-functions)
Attached files
0
reply
harr
Badges: 14
Rep:
?
#9
Report 4 years ago
#9
(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.

Code:
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.
0
reply
crookie
Badges: 2
Rep:
?
#10
Report Thread starter 4 years ago
#10
(Original post by harr)
As far as I can tell, it already works.

Code:
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
0
reply
MilitantMindset
Badges: 6
Rep:
?
#11
Report 4 years ago
#11
(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/c...ning-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.
0
reply
harr
Badges: 14
Rep:
?
#12
Report 4 years ago
#12
(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.
0
reply
crookie
Badges: 2
Rep:
?
#13
Report Thread starter 4 years ago
#13
(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!
0
reply
MilitantMindset
Badges: 6
Rep:
?
#14
Report 4 years ago
#14
(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
0
reply
harr
Badges: 14
Rep:
?
#15
Report 4 years ago
#15
(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.
0
reply
sanidhya16
Badges: 1
Rep:
?
#16
Report 1 year ago
#16
It just prints the new line in the output i guess if you are in a loop...
0
reply
winterscoming
Badges: 19
Rep:
?
#17
Report 1 year ago
#17
(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
0
reply
sanidhya16
Badges: 1
Rep:
?
#18
Report 1 year ago
#18
(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.
0
reply
Lokesh Sharma
Badges: 1
Rep:
?
#19
Report 1 year ago
#19
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: [email protected]

THANK YOU.
0
reply
thecryptoguy123
Badges: 1
Rep:
?
#20
Report 11 months ago
#20
Just type fib() to run the function thats all
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Bournemouth University
    Clearing Open Day Undergraduate
    Wed, 31 Jul '19
  • Staffordshire University
    Postgraduate open event - Stoke-on-Trent campus Postgraduate
    Wed, 7 Aug '19
  • University of Derby
    Foundation Open Event Further education
    Wed, 7 Aug '19

Are you tempted to change your firm university choice on A-level results day?

Yes, I'll try and go to a uni higher up the league tables (160)
17.8%
Yes, there is a uni that I prefer and I'll fit in better (77)
8.57%
No I am happy with my course choice (534)
59.4%
I'm using Clearing when I have my exam results (128)
14.24%

Watched Threads

View All