Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: python variable type

  1. #1
    Join Date
    Jul 2014
    Beans
    315

    Question python variable type

    Hi all,
    I just started to learn python programming and have a question.
    It does not require to set variable data type, hmm.
    So I can just declare
    Code:
    my_variable = 5
    and it will be integer.
    What if I need to assign something like 4.3 (not integer) to that variable later?
    I believe I should instead declare it from the beginning as
    Code:
    my_variable = 5.0
    so that it will accept my 4.3 value later and store it properly (not as 4 integer).
    But... how do I declare variable with datatype (definitely)?

    I believe I will find out this a bit later, but typing my question here helps me to understand what I am looking for.
    So... thank you for your attention.

  2. #2
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    458
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python variable type

    There are three distinct numeric types: integers, floating point numbers, and complex numbers.

    https://docs.python.org/3/library/stdtypes.html
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  3. #3
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,902
    Distro
    Xubuntu 19.10 Eoan Ermine

    Re: python variable type

    But... how do I declare variable with datatype (definitely)?
    You did, in our first post. Twice. You knew what type you were assigning.
    You cannot have a variable that only stores one datatype - variables will store whatever is assigned to them. Come to think if it, I don't think you can "declare" variables. They are created when you assign a value to them.
    Also look at this:
    Code:
    a=5
    a *= 1.0
    This stores an integer in a, and then replaces it with a float, something that's not immediately obvious.

    P.S.
    All variables in python are just references to objects. It's the objects that have types. Even integers are objects in python. Try:
    Code:
    help(3)
    dir(3)
    Last edited by The Cog; November 24th, 2019 at 01:24 PM.

  4. #4
    Join Date
    Jul 2014
    Beans
    315

    Re: python variable type

    Quote Originally Posted by gnusci View Post
    There are three distinct numeric types: integers, floating point numbers, and complex numbers.

    https://docs.python.org/3/library/stdtypes.html
    So it's only for Python 3, right?
    I started from Python 2 as it's still in use as well and can't see anything like this. Did I miss anything?

  5. #5
    Join Date
    Jul 2014
    Beans
    315

    Question Re: python variable type

    Quote Originally Posted by The Cog View Post
    You cannot have a variable that only stores one datatype - variables will store whatever is assigned to them. [/code]
    Hm... so one variable can store integer first, then same variable can store string? Is it correct or am I missing something?

  6. #6
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,902
    Distro
    Xubuntu 19.10 Eoan Ermine

    Re: python variable type

    I started from Python 2 as it's still in use as well and can't see anything like this. Did I miss anything?
    Python3 added a new type for byte arrays that is distinct from strings which are now always unicode. Apart from that, nothing big. https://docs.python.org/2/library/datatypes.html
    Hm... so one variable can store integer first, then same variable can store string? Is it correct or am I missing something?
    I would prefer to say that a variable can refer to an int object first, then refer to a float object or any other type of object later. It refers to whatever is assigned to it. The only thing you can do to a variable is to assign to it (or delete it). If you try to use it you just end up using whatever it refers to.

  7. #7
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,533
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: python variable type

    Quote Originally Posted by marchello_lippi2 View Post
    Hm... so one variable can store integer first, then same variable can store string? Is it correct or am I missing something?
    Yes. This is an aspect of dynamic typing. If you want to declare a variable of a particular type and to ensure that only values of that type can be assigned to it, then you need a statically typed language.

  8. #8
    Join Date
    Jul 2014
    Beans
    315

    Re: python variable type

    Well, I believe I sorted this out a bit with your help, thanks.

    Now wanted to use same thread to speak regarding Python 2. Honestly, I started learning exactly this version because codecademy has its online interactive course for free. Python 3 is PRO plan and it's paid. Not that I'm short of funds right now, but wanted to try it for free because I don't know how it will go.
    Done 10% and then found information that Python 2 is used in legacy projects while Python 3 is more for new and future usage. I believe I would need both because I'm lucky to work on legacy projects recently, though I should be ready to create something from scratch as well.
    Now my question is, how different those two versions are? Like, how a newcomer can understand the logic, why both versions co-exist? Can you imagine that one is able to work on Python 3 project with some googling if he/she learned only Python 2 before?
    Thanks!
    Last edited by marchello_lippi2; November 27th, 2019 at 01:01 AM.

  9. #9
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,902
    Distro
    Xubuntu 19.10 Eoan Ermine

    Re: python variable type

    They really are not that different. You can find lots of info on the difference by web searches.
    The two biggest differences are:
    1:
    print is not a function, not a language built-in. So instead of "print a, b, c", you say "print(a, b, c)". Not so hard, really.
    2:
    Strings are all unicode and there is a new bytes type for a sequence of bytes. I think this is the big difference, that will catch people out when reading/writing files/sockets.
    Code:
    >>> a="hi there"
    >>> type(a)
    <class 'str'>
    >>> b = a.encode("utf8")
    >>> type(b)
    <class 'bytes'>

  10. #10
    Join Date
    Jul 2014
    Beans
    315

    Re: python variable type

    Quote Originally Posted by The Cog View Post
    They really are not that different. You can find lots of info on the difference by web searches.
    The two biggest differences are:
    [...]
    Good news, so I believe it's good that I started from Python 2.

    Another question comes to my mind... If it's not so big difference, then why they even splitted?
    I mean, I would just introduce print2 stuff and so on.
    Is there any real serious reason to have different versions?...

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •