Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

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.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    thiru Dec. 18, 2016, 8:48 a.m.

    input : a = 0 b = 2 print(a + b) output: 0 THE OUTPUT IS 0 if i want to get output like this what i should do please help me

  • User

    sethN Aug. 6, 2016, 12:43 a.m.

    Seth = '2' N = '4' print(int(seth)%N) result i dunno

  • User

    sethN Aug. 6, 2016, 12:43 a.m.

    Seth = '2' N = '4' print(int(seth)%N) result i dunno

  • User

    sethN Aug. 6, 2016, 12:43 a.m.

    Seth = '2' N = '4' print(int(seth)%N) result i dunno

  • User

    GEOFFREY MASOFTWARE April 15, 2015, 11:22 a.m.

    iam liking these guys,

  • User

    GEOFFREY MASOFTWARE April 15, 2015, 11:21 a.m.

    iam liking these guys,

  • User

    kourosh April 2, 2015, 7:44 a.m.

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

  • User

    Simba_son_of_Mufasa Feb. 11, 2015, 1:11 a.m.

    cool staff

  • User

    Pango Jan. 11, 2015, 5:03 p.m.

    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.

  • User

    PapaJack Jan. 4, 2015, 11 p.m.

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

  • User

    PapaJack Jan. 4, 2015, 10:59 p.m.

    This works in Ver. 3.2

  • User

    lastone2survive Dec. 19, 2014, 9:09 p.m.

    In IDLE I'm not able to get any of this, it says (b) is a syntex error... dafuq, using 2.7.9

  • User

    Michael Opinde July 8, 2014, 3:27 a.m.

    Variable Initialization & changing the type a="10" b=20 print(int(a)+b) Result 30

  • User

    abdullah anisetty June 29, 2014, 8:11 a.m.

    or i can say i born in a="1" b="9" c="6" print(a+b+b+d) 1996 #_^

  • User

    abdullah anisetty June 29, 2014, 5:25 a.m.

    i born in a="1" b="9" c="9" d="6" print(a+b+c+d) 1996 ^_^

  • User

    Clau Lacatus June 28, 2014, 2:08 a.m.

    Easy & great since now!

  • User

    Bob Evans May 28, 2014, 12:19 p.m.

    Now this is very useful, I like this a lot.

  • User

    Gaurav Kulkarni May 22, 2014, 10:23 p.m.

    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!

  • User

    IWhippedYou April 29, 2014, 3:49 p.m.

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

  • User

    Riversoil April 28, 2014, 1:05 p.m.

    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

  • User

    Vasiluk Wolf April 15, 2014, 10:45 a.m.

    And we have sample i = 0 j = 1 print (bool(i) or bool(j) )

  • User

    Vasiluk Wolf April 15, 2014, 10:43 a.m.

    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

  • User

    brbrett March 11, 2014, 10:23 p.m.

    This is getting more interesting and interesting...

  • User

    Lotfi GHAZOUANI Feb. 25, 2014, 12:58 p.m.

    Great it's very important to understand the logic of the variables and the different forms of them.

  • User

    EuniceLopes Feb. 12, 2014, 11:19 a.m.

    WOW Great!! This helps a lot to understand in a easy and fun way Python Basics

  • User

    Squall Feb. 10, 2014, 10:41 a.m.

    This has helped soooo much with python, thank you!!!

  • User

    Abhinav Abinz Jan. 18, 2014, 9:24 p.m.

    My coad simulator is not working... :(

  • User

    Sergio68 Jan. 3, 2014, 12:32 p.m.

    Sounds good

  • User

    Pyn00b Dec. 24, 2013, 9:02 a.m.

    slow and understandable process

  • User

    Nazrin chronicle Dec. 19, 2013, 11:02 p.m.

    very good explanation.....and all this time I suffer myself with lisp because it's explanation is not that good.

  • User

    silas Dec. 6, 2013, 9:17 a.m.

    It would be awesome to have a master definition list all on one page.

  • User

    Olya Brikman Nov. 28, 2013, 11:36 a.m.

    I love the explanation! Thanks!

  • User

    3zrides Nov. 25, 2013, 10:56 a.m.

    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.

  • User

    Joga Bonito Nov. 3, 2013, 3:40 a.m.

    The best destination for learning programming languages.

  • User

    Ramanan Oct. 4, 2013, 1:56 p.m.

    it is easy understand

  • User

    Onofre Aquino Aug. 28, 2013, 8:23 a.m.

    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)

  • User

    Darren Aug. 28, 2013, 12:05 a.m.

    Very straight forward tutorials, Thank you so much..

  • User

    akshat k July 12, 2013, 6:09 a.m.

    this site is lovely!

  • User

    Drake July 6, 2013, 2:24 a.m.

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

  • User

    Michael July 3, 2013, 7:55 p.m.

    Clear and concise, love it!

  • User

    Daniel Wilianto June 4, 2013, 11:21 p.m.

    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.

  • User

    Skinner Sweet May 3, 2013, 2:37 p.m.

    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.



Advertisement