10 Replies - 2034 Views - Last Post: 24 October 2014 - 09:13 AM Rate Topic: -----

#1 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

What does constant mean in python?

Posted 22 October 2014 - 05:18 AM

Hello there.I have been reading a tutorial python book and i came across a sentence which i can't understand it.Here is the sentence:

A Boolean expression is anything that can be evaluated to produce a Boolean
value (True or False). In Python, such an expression evaluates to False if it is
the predefined constant False, the special object None, an empty sequence or
collection (e.g., an empty string, list, or tuple), or a numeric data item of value
0; anything else is considered to be True


I want to know what does a constant mean in python? I have searched all over internet but there was no result.

Please help me.Thank you so much.

Is This A Good Question/Topic? 0
  • +

Replies To: What does constant mean in python?

#2 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: What does constant mean in python?

Posted 22 October 2014 - 05:40 AM

Python doesn't have constants in the way other languages do, this is talking about the specific, pre-defined values of True and False.

True and False are referred to in the docs as constant objects. (They are also keywords.)

This post has been edited by andrewsw: 22 October 2014 - 05:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Re: What does constant mean in python?

Posted 22 October 2014 - 06:15 AM

Thank you so much for helping me.What do you mean by specific, predefined values of " True " and " False " ? Do you mean a value like 0 which we assign to a variable then we use it in a boolean expression?

This post has been edited by andrewsw: 22 October 2014 - 10:13 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: What does constant mean in python?

Posted 22 October 2014 - 10:12 AM

Yes, try

x = True
if x:
    print("it is True")

Was This Post Helpful? 1
  • +
  • -

#5 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Re: What does constant mean in python?

Posted 22 October 2014 - 10:31 AM

Thank you sir! You are really kind.Why do we call x variable a "constant" ? I'm so confused.

This post has been edited by andrewsw: 22 October 2014 - 10:42 AM
Reason for edit:: removed previous quote

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: What does constant mean in python?

Posted 22 October 2014 - 10:41 AM

We don't call "x variable a constant". It is a variable. x = False just assigns it the constant value False the same as x = 42 assigns it the fixed, integer, value of 42.

Please note that you do not have to quote the previous post every time, there is a Reply button further down the page.
Was This Post Helpful? 1
  • +
  • -

#7 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Re: What does constant mean in python?

Posted 23 October 2014 - 04:44 AM

But the book is kinda saying variable x is a constant:

In Python, such an expression evaluates to False if it is
the predefined constant False, the special object None, an empty sequence or
collection (e.g., an empty string, list, or tuple), or a numeric data item of value
0; anything else is considered to be True


i do apologize if i quoted posts every time, i didn't know that.

by constant value, do you mean a value that cannot be changed? is 42 a constant value as well? if so, then why we can change it:

x = 42

x = 47

Sorry if i bother you so much.Hope someday i could repay you.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: What does constant mean in python?

Posted 23 October 2014 - 04:52 AM

x = 42
x = 47

You aren't changing the value 42, you are assigning x a different value.. 47.

Both 42 and False are fixed, known values. You cannot do

42 = 47??!

nor can you do

False = Eric
or
False ="Eric"

I think you will find it easier, and save yourself confusion, just to consider that the words False and True are keywords of the Python language. Yes, False is a pre-defined constant, but just think of it as a keyword.
Was This Post Helpful? 2
  • +
  • -

#9 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Re: What does constant mean in python?

Posted 24 October 2014 - 08:57 AM

I think i have finally got what you have been saying..(as well as the book).

Every value like 42, 'hello', True, False is a constant.Which means they are fixed and we can't change them.So the book is simply saying, if you used a false constant through a variable or directly in a boolean expression, after evaluation the product would be false.Am i right?

By the way, there is no difference between a constant and constant value, am i right on this too?

Thank you so much! just don't know how to appreciate all of your patience and kindness!
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6693
  • View blog
  • Posts: 27,471
  • Joined: 12-December 12

Re: What does constant mean in python?

Posted 24 October 2014 - 09:06 AM

Em, yes, that's pretty much it ;)
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11390
  • View blog
  • Posts: 19,431
  • Joined: 19-March 11

Re: What does constant mean in python?

Posted 24 October 2014 - 09:13 AM

42 is a constant - it's always 42. False is a constant - it's always going to be false.

x = 42 is a variable: it has a value, which at the moment is 42, but that could change, and it's got a name, which is x. Notice that "42" is really just a name, it's just one way of representing the quantity which is one greater than the 13th prime number. You could also represent this as x2A (hex notation) or 052 (octal) or 0010 1010 (binary).

Quote

In Python, such an expression evaluates to False if it is the predefined constant False, the special object None, an empty sequence or collection (e.g., an empty string, list, or tuple), or a numeric data item of value 0; anything else is considered to be True


Notice the difference between "is" and "evaluates to".

a = False
b = 1==2
c = isPrime(42)

all evaluate to False, but a, b, and c are not the constant False.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1