logo codesdope

Basics of Python


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)
Output
<type 'str'>
<type 'int'>
<type 'float'>

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)
Output
<type 'str'>
<type 'int'>
<type 'float'>

'type()' gives us the type of anything given to it.
e.g.-
type("Hello World")
->str
->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

Notice that simple 8 is int but 8.0 is float

Let's brush-up.

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
Output
<type 'str'>
<type 'int'>
<type 'float'>
I am learning something new.
10
3.14

29 keywords


and def exec if not return
assert del finally import or try
break elif for in pass while
class else from is print yelid
continue except global lambda raise

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


print 'Fun'*3
Output
FunFunFun
This code should mean "Fun"+"Fun"+"Fun".
So let's try this:
print "a"+"b"
Output
ab

Yeah, working!
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.

'*' in ruby

Commenting


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
Output
10

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
Output
15
comment in python

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.

Swap


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
Output
5
10

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.


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.