Home » Tutorials » Python » Variables

Variables

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:

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

2

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

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

02

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.

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

2

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.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Variables". After Hours Programming. Accessed on September 20, 2024. https://www.afterhoursprogramming.com/tutorial/python/variables-py/.

  • Stewart, Suzy. "Variables". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/variables-py/. Accessed 20 September, 2024.

  • Stewart, Suzy. Variables. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/variables-py/.



44 thoughts on “Variables”

  1. 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’))
    print(c)
    #Will add the integer ‘3’ plus the integer ‘2’ which equals ‘5’.

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

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

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

  5. 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’))
    print(c)

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

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

Leave a Comment

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