In the heart of every good programming language, including Python, are the variables. Variables are an awesome asset to the dynamic world of the web. Variables alone are dynamic by nature. However, Python is pretty smart when it comes to variables, but it is sometimes quite a pain. Python interprets and declares variables for you when you set them equal to a value. Example:

a = 0
b = 2
print(a  + b)


Isn’t that cool how Python figured out how a and b were both numbers. Now, let’s try it with the mindset of wanting the 0 to be a string and the 2 to also be a string to create a new string “02”.

a = "0"
b = "2"
print(a  + b)


Ah, see! Now, it thinks they are both strings simply because we put them in quotations. Don’t worry if you don’t understand strings yet, just note that they are not numbers or integers. This is really awesome, but as with any element or practice in a programming language there is always the flip side, especially with this auto declaration thing. Suppose we started off with “0” being a string, but we change our mind and want it to be a number. Finally, we decide that we want to add it to the number 2. Python bites us with an error. This brings us to the idea of casting the variable into a certain type.

a = "0"
b = 2
print(int(a)  + b)


Whoa, what is that int() thing? This is how you cast one variable type into another. In this example, we cast the string 0 to an integer 0. Let’s take a quick peak at a few of the important variable types in Python:

  • int(variable) – casts variable to integer
  • str(variable) – casts variable to string
  • float(variable) – casts variable to float (number with decimal)

Like I said, those are just the most commonly used types of casting. Most of the other variable types you typically wouldn’t want to cast to.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Comment

  1. Wow, I am impressed by the nice and concise explanation of an integer, string, and float. I remember having this explained to me for the first time, and I didn’t understand the explanation. Fortunately, I learned on my own and am better for doing so.

  2. Brian, the default display for IDLE is the command prompt. Everything you typed will be executed immediately, just like DOS Prompt and Linux’s console. You have to click File –> New, in order to write your program.

  3. Really like your guides. The explaining is so easy to understamd and also written in a fun and intresting way

  4. a = ‘3’ #’3′ is a string and cannot be added to an integer
    b = ‘2’ #’2′ is a string as well
    #we used the int() function to convert the strings into integers so they can #be evaluated.
    c = (int(‘3’) + int(‘2’))

  5. I am doing the first part of the integer variable. It would be nice to know what their real life functions could be. This way I can imagine different scenario’s where I could implement the code.

  6. I’m think is not true.We have Boolean type. Sample
    a = False
    b = True
    We have type None. This type for first variable.
    You have error if write
    a = None
    b = None

  7. i think it is better to say data types and not variable types.
    I take this from other tutorials i have done. Just a suggestion

  8. This is probably the best explanation that I’ve found for this topic. I’m looking forward to the rest of the tutorials.

  9. I really liked the fact that you don’t actually have to define variable type beforehand.. typecasting is little tricky but once practiced enough can work out really well!

  10. This works in Ver. 3.2

    a = ‘3’
    #’3′ is a string and cannot be added to an integer.
    b = ‘2’
    #’2′ is a string as well.
    print(a + b)
    #Will print ’32’ and not add the ‘3’ plus ‘2’.
    #we used the int() function to convert the strings into integers so they can
    #be evaluated.
    c = (int(‘3’) + int(‘2’))
    #Will add the integer ‘3’ plus the integer ‘2’ which equals ‘5’.

  11. I think the use of Zero as a variable could be confusing. Perhaps use 1 instead of 0. The results of “print(int(a) + b” is the same as “b” itself.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.