You are on page 1of 4

Basics of Python

Now that you have a good command on previous chapter, let's do something more.

print(type('Hello World'))
print(type(8))
print(type(8.0))

Output

<class 'str'>
<class 'int'>
<class 'float'>

'type()' gives us the type of anything given to it. For example, type("Hello
World") is 'str'. str understands for string.

String is a 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. For now, just understant that a string
is collection of characters and these are written between ' ' or " ". So, Hello
World written between ' ' i.e. 'Hello World' is a string.

Similarly, int is for integers and float is for decimals.

Notice that simple 8 is an int but 8.0 is a float.

Even a digit written inside ' ' or " " is treated as a string and not as an integer
or a decimal. For example, print(type("8")) will give us <class 'str'>.

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

Python Keywords

There are few words which are used by Python language itself. So, we can't
use those words for the name of our variables as they are reserved for the
Python. For example, del is used to delete objects and using it for the name
of a variable will give us an error.

Here is the list of keywords reserved for Python.


and def yield if not return
assert del finallyimportor try
break elif for in pass while
class else from is nonlocalyelid
continueexceptglobal lambdaraise False
True None with as

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
Working!
This is the Pythonic way to get your things done. These type of features of
Python can be of good use for you in future.

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. Comments are written
to make our 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
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 its 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 good 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.

You might also like