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:
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”.
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.
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.
Definition of the variable types then demo of using them would help me.
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
Seth = ‘2’
N = ‘4’
print(int(seth)%N) result i dunno
Seth = ‘2’
N = ‘4’
print(int(seth)%N) result i dunno
Seth = ‘2’
N = ‘4’
print(int(seth)%N) result i dunno
iam liking these guys,
iam liking these guys,
a=”0″
b=2
print(a*b)
cool staff
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.
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’.
This works in Ver. 3.2
In IDLE I’m not able to get any of this, it says (b) is a syntex error… dafuq, using 2.7.9
Variable Initialization & changing the type
a="10"
b=20
print(int(a)+b)
Result 30
or i can say
i born in
a="1"
b="9"
c="6"
print(a+b+b+d)
1996
#_^
i born in
a="1"
b="9"
c="9"
d="6"
print(a+b+c+d)
1996
^_^
Easy & great since now!
Now this is very useful, I like this a lot.
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!
This is probably the best explanation that I’ve found for this topic. I’m looking forward to the rest of the tutorials.
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
And we have sample
i = 0
j = 1
print (bool(i) or bool(j) )
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
This is getting more interesting and interesting…
Great it’s very important to understand the logic of the variables and the different forms of them.
that is a great method , !!!
WOW Great!! This helps a lot to understand in a easy and fun way Python Basics
This has helped soooo much with python, thank you!!!
woha. preety cool. 🙂
My coad simulator is not working… 🙁
Mine won’t work.
Sounds good
slow and understandable process
very good explanation…..and all this time I suffer myself with lisp because it’s explanation is not that good.
It would be awesome to have a master definition list all on one page.
I love the explanation! Thanks!
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.
The best destination for learning programming languages.
it is easy understand
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)
Very straight forward tutorials, Thank you so much..
Really like your guides. The explaining is so easy to understamd and also written in a fun and intresting way
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.
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.