How to declare a int and string in python?

I would like to know how can we declare a int and string in python? like we can declare a
list as
array = []
dict = {}
tuple = ()

Open in new window

beer9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
Int and string are not distinct types in Python. They are simply the same type of object and will become strings or numbers as their use demands.
Nas-BanovCommented:
You don't declare types in Python, any variable can have values of different types at different times.

The examples you gave do not declare list/dict/tuple - rather they simply initialize (assign) an empty list, an empty dictionary and a 0-tuple to the variables. There is no magic in them - what `[ ]` does is construct a new empty list (essentially it's a syntax sugar for `list()` constructor invocation).

If you like to think of variable initialization with some "empty" value as declaration, then in the same sense you can think of this code as declaration:
str_var = ""
num_var = 0

Open in new window


PS. Oh, by the way - do not re-define `dict` and `tuple` as in your example, that may (and will at some point) cause confusion - dict() and tuple() are already existing functions. (Data types/data constructors are not reserved words in Python so theoretically you can re-define/re-use the variables list/dict/tuple... but avoid that temptation).
peprCommented:
jmcg is wrong. Int and string are distinct types. Actually, Python does not use declarations. It has different attitude to variables. Values of the simple types (like int, bool, string, float, complex) are created as objects in the time when you write them on the right side of the assignment as constants, or when they are the result of an expression. The variable is just named reference to the target object. The reference is untyped, but the target object knows its type.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
My thinking has been polluted by Perl, I guess.
peprCommented:
@jmgc: I understand your situation. To compare, I have almost forgot Perl (since I started to use Python). I remember my confusion with Python at the beginning. This was because some terminology differs because dynamic languages are simply that way. I came from the "classic compiled languages" like Pascal, C, and later C++.

I have written the article that may help to understand Python (written based on my previous experience with compiled languages and trying to explained what was not understandable for me at the beginning) -- see Python illustrated (part 3).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.