Now that you have a good command of previous section, let's do something more.
print type('Hello World') print type(8) print type(8.0)
And if you are not running from file and doing it directly on python shell, then this will also work.
type('Hello World') type(8) type(8.0)
'type()' gives us the type of anything given to it.
->str is for string.
String means collection of characters. In simple English, it is a letter, word, sentence or collection of sentences. You will go through a whole topic on string. So, leave it for that time. If you are getting confused then just keep in mind that strings are characters, words or sentences and these are written between ' ' or " ". So, Hello World written between ' ' i.e. 'Hello World' is a string.
->int means integer
->float means decimal
a = "I am learning something new." b = 10 pi = 3.14 print type(a) print type(b) print type(pi) print a print b print pi
I am learning something new.
These are the words which are reserved for python and we can't use them for our variables. ( Try using any of these and see the error you are getting. )
Let's have some fun
So let's try this:
This is the python way to get your things done. These type of features of python can be of good use for you in future.
You can also include something in your code which won't be compiled and your computer will simply ignore that while running your code. Comment is written to make your code more readable. Comments are written after '#'. Comments written after '#' are single line comments. New line will not be a part of the comment
# I am using a variable x x = 10 print x
You can also use multi-line comments. To use them write comments between ( ''' ''' ). Let's see this.
''' Multiline comment Using variable x and y. ''' x = 10 y = 5 print x+y
Why to use comments?
As mentioned earlier, it makes your code more understandable. Assume that you have written a software and after releasing it, you hired few good programmers for maintenance. Without comments, it would be a very difficult job for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make a habit of writing comments.
Swapping means interchanging the values of two variables. eg - if x is 10 and y is 5 then after swapping x will be 5 and y will be 10.
So, let's see how to do this.
x = 10 y = 5 x,y = y,x print x print y
And it's done. Yes, I know it is easy and that's why I love python.
There is no glory in practice but there is no glory without practice.