What Is Python
What Is Python
Even though most of today’s Linux and Mac have Python preinstalled in it, the version might be
out-of-date. So, it is always a good idea to install the most current version.
Python Keywords
We cannot use a keyword as variable name, function name or any other identifier. They are used to
define the syntax and structure of the Python language.
There are 33 keywords in Python 3.3. This number can vary slightly in course of time.
All the keywords except True, False and none are in lowercase and they must be written as it is. The
list of all the keywords is given below.
Keywords in Python programming language
1)False
2)class
3)finally
4)is
5)return
6)None
7)continue
8)for
9)lambda
10)try
11)True
12)def
13)from
14)nonlocal
15)while
16)and
17)del
18)global
19)not
20)with
21)as
22)elif
23)if
24)or
25)yield
26)assert
27)else
28)import
29)pass
30)break
31)except
32)in
33)raise
Python Indentation
Most of the programming languages like C, C++, Java use braces { } to define a block of code.
Python uses indentation.
A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line. The amount of indentation is up to you, but it must be consistent throughout that
block.
Generally four whitespaces are used for indentation and is preferred over tabs. Here is an example.
for i in range(1,11):
print(i)
if i == 5:
break
The enforcement of indentation in Python makes the code look neat and clean. This results into
Python programs that look similar and consistent.
Indentation can be ignored in line continuation. But it's a good idea to always indent. It makes the
code more readable. For example:
if True:
print('Hello')
a=5
and
if True: print('Hello'); a = 5
both are valid and do the same thing. But the former style is clearer.
Incorrect indentation will result into IndentationError.
Python Comments
Comments are very important while writing a program. It describes what's going on inside a
program so that a person looking at the source code does not have a hard time figuring it out. You
might forget the key details of the program you just wrote in a month's time. So taking time to
explain these concepts in form of comments is always fruitful.
In Python, we use the hash (#) symbol to start writing a comment.
It extends up to the newline character. Comments are for programmers for better understanding of a
program. Python Interpreter ignores comment.
#This is a comment
#print out Hello
print('Hello')
Multi-line comments
If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the
beginning of each line. For example:
#This is a long comment
#and it extends
#to multiple lines
Another way of doing this is to use triple quotes, either ''' or """.
These triple quotes are generally used for multi-line strings. But they can be used as multi-line
comment as well. Unless they are not docstrings, they do not generate any extra code.
"""This is also a
perfect example of
multi-line comments"""
They are given unique names to differentiate between different memory locations. The rules for
writing a variable name are same as the rules for writing identifiers in Python.
We don't need to declare a variable before using it. In Python, we simply assign a value to a variable
and it will exist. We don't even have to declare the type of the variable. This is handled internally
according to the type of value we assign to the variable.
Variable assignment
We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned
to any valid variable.
a=5
b = 3.2
c = "Hello"
Similarly, 3.2 is a floating point number and "Hello" is a string (sequence of characters) assigned to
the variables b and c respectively.
Multiple assignments
If we want to assign the same value to multiple variables at once, we can do this as
x = y = z = "same"
Every value in Python has a datatype. Since everything is an object in Python programming, data
types are actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the important types are listed below.
1. Python Numbers
An integer, floating point numbers and complex numbers falls under Python numbers category.
They are defined as int, float and complex class in Python.
We can use the type() function to know which class a variable or a value belongs to and the
isinstance() function to check if an object belongs to a particular class.
Example
a=5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Result
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
2. Python List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very
flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [
].
We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts
form 0 in Python.
Example
a = [5,10,15,20,25,30,35,40]
print("a[2] = ", a[2])
print("a[0:3] = ", a[0:3])
print("a[5:] = ", a[5:])
Result
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
3. Python Tuple
Tuple is an ordered sequences of items same as list. The only difference is that tuples are
immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.