Hello everyone, today I am starting my python tutorial series. I have design this tutorial series specially for the students of core engineering branches. I will try to deliver these tutorials in simple manner so that students of core branches can easily understand and apply their skills and knowledge to solve problems of their technical subjects using python programming. In these tutorials you all can use any of software – python shell, pycharm, anaconda jupyter notebook or Google colabs. All these softwares are easy to install except Google colabs which is available online. I had given information about Google colabs in my previous blog. You all can go to “Python in pocket” blog under menu option for detailed information regarding Google colabs. This tutorial is designed as follows:
Tutorial-1 ----> basic operations & variables.
Tutorial-2 ----> print & input function.
Tutorial-3 ----> built in functions & modules
Tutorial-4 ----> comment & python strings.
Tutorial-5 ----> boolean values & operators.
Tutorial-6 ----> if else statements.
Tutorial-7 ----> elif & nested if statements.
Tutorial-8 ----> Python lists.
Tutorial-9 ----> Python tuples.
Tutorial – 1
First we perform basic maths operations on python:
>>> 25 + 25
50
>>> 50 - 25
25
>>> 25 / 3
8.333333333333334
>>> 25 // 3 (// sign gives whole no. or an integer)
8
>>> 10 * 5
50
>>> 25 % 3 (% sign gives remainder)
1
>>> 2 ** 2 (** sign means 2 to power of 2 i.e 2^2)
4
>>> 2e2 (e/E means 2*10^2 = 200)
200.0
>>> 2E2
200.0
>>> (10 + 5) - 2*(15/3)
5.0
In above operation order of precedence is- (we follow from top to bottom)
a) Brackets
b) exponents (**)
c) multiplication & division (*,/,//,% -out of these priority will be given to that which ever comes on left hand side of operation)
d) addition & subtraction (+,- out of these priority will be given to that which ever comes on left hand side of operation)
For performing these operations click on – colabs
Variables:
First we will see how to assign values to variables:
>>> a = 9
>>> a
9
In python you don't need to define data type of variable. For ex:
>>> int a = 9
invalid syntax.
Now we define rules for defining variable:
a) must start with letter or underscore_
b) must consists of letters, no's & undercores_ (variable name cannot start with no.)
c) case sensitive.
Examples:
>>> age = 9
>>> _age = 10
>>> age10 = 89
>>> 8age = 9
invalid syntax
>>> myname = 'abc'
>>> myname
'abc'
>>> mynam1 = 'def'
>>> mynam1
'def'
>>> myname + mynam1
'abcdef'
>>> age + _age
19
we can also store complex no.
>>> mynum = 10j
>>> mynum
10j
keyword type() gives us type of value stored in variable.
>>> type(myname)
str
>>> type(age)
int
For performing these operations click on – colabs
Tutorial – 2
How to use print function ?
print is inbuilt function in python.
>>> print ("hello")
hello
Within parenthesis we give arguments & parameter. "hello" is string argument. Whatever we writes in double quotes, it becomes string.
>>> print (10)
10
Here 10 is an integer argument.
>>> print (50 * 10)
500
Suppose we want to print--> 50 * 10 = 500
>>> print ("50 * 10 = ", 50 * 10)
50 * 10 = 500
We can give multiple arguments using comma in print function.
>>> print ("hello" , " " , "world")
hello world
In above example if we use variables x & y instead of string (50*10).
>>> x = 50
>>> y = 10
>>> print(" {0} * {1} = {2}".format(x, y, x*y))
50 * 10 = 500
'format' is inbuilt method in python. '0,1,& 2' are index values. 'x' will be transferred to index 0. 'y' will be transferred to index 1. Multiplication of 'x*y' will be transferred to index 2.
>>> print ("hello" , "world" , sep = "------")
hello------world
In above example 'sep' keyword is used & separators are used in form of string.
>>> name = "mark"
>>> print ("hello %s" % name)
hello mark
In above example modulo s (%s) symbol is used to print string & name replaces %s.
>>> age = 22
>>> print ("hello %s ! are you %d years old" % (name, age))
hello mark ! are you 22 years old
In above example, we have put name in form of tupple. Tupple is fixed size list. We will discuss list & tupples in coming tutorials. Modulo d is used to print integer.
>>> print (" marks = %f" % 94.6)
marks = 94.600000
In above example, modulo f (%f) symbol is used to print decimal values. If we want to limit decimal value, we will write like this-
>>> print (" marks = %.2f" % 94.6)
marks = 94.60
For performing these operations click on – colabs
How to give input in python ?
input is inbuilt function in python.
>>> value = input ("enter value:")
enter value:100
>>> value
'100'
here '100' is string not number.
For number we will use 'int'-
>>> value = int(input ("enter value:"))
enter value:100
>>> value
100
for decimal values-
>>> value = float(input ("enter value:"))
enter value:100
>>> value
100.0
For performing these operations click on – colabs
Tutorial – 3
Built in functions & modules in python:
Till now you all have studied the use of ‘input, print ,int, float’. All these functions are the built in functions used in python. For detail information about the built in function you can visit python website.
>>> dir (__builtins__)
When we execute above,this will give us the list of all built in fuctions used in python. Let us see some examples of built in functions:
>>> pow (2,10)
1024
The above operation performs the operation of 2 to power 10 i.e (2^10).
>>> len ("hello")
5
The above operation 'len' gives us the length of string.
>>> max (1,3,5,8,9)
9
The above operation returns the biggest item.
If we have to find out the details & syntax of any built in function, we can easily find out by using:
>>> help (max)
This will give us the detail information about max function.
Now we talk about built in module. First we see how to import built in module in python & how to use it.
>>> import math
Here 'math' is built in module in python.
>>> math.sqrt(100)
10.0
Here 'sqrt' is built in function in math module which performs square root operation.
We can also use below function to find out more information:
>>> help (math.sqrt)
For more information on built in methods in python, click on Built in methods.
Tutorial -4
How to comment your code in python?
Suppose due to some reason if we dont want to execute any line of code, we add '#' before that line of code. It will become comment & will not execute.
>>> # print ("Hello")
If we want to write multiple lines comments ,we use double quotes three times. Ex:
""" python
tutorials
by Palash Jain """
Python Strings:
>>> x = " Hello World "
>>> print(x)
Hello World
In double quotes we can add single code without any problem.
>>> x = " Hello's World "
>>> print(x)
Hello's World
If we want to add double quotes then we add backslash-
>>> x = " Hello\"s World "
>>> print(x)
Hello"s World
>>> x = ' Hello World '
>>> print(x)
Hello World
If we want to add single quotes then we add backslash-
>>> x = ' Hello\'s World '
>>> print(x)
Hello's World
If we want to print string with backslash-
>>> x = " Hello\"s \World " (you can also write-->" Hello\"s \\World ")
>>> y = ' Hello\'s \World ' (you can also write-->' Hello\'s \\World ')
>>> print(x)
Hello"s \World
>>> print(y)
Hello's \World
>>> x = "Hello"
>>> y = 'hello'
>>> print(x.capitalize())
Hello
>>> print(y.capitalize())
Hello
Here 'capitalize' is inbuilt method in python which capitalize first letter of string.
>>> x = "Hello"
>>> y = 'HELLO'
>>> print(x.upper())
HELLO
>>> print(y.lower())
hello
Here upper() method converts all letters to upper & lower() method converts all letters to lower.
>>> x = "Hello"
>>> y = ' HELLO '
>>> print(x[0:3])
Hel
Here [0:3] means substring starts from zero index i.e 'H' & end at second index i.e 'l'. Remember index starts with zero.
>>> print(y.strip())
HELLO
Here spaces from start & end will strip off using strip() method.
>>> x = "Hello"
>>> y = 'HELLO'
>>> print(x.islower())
False
'False' means all letters in 'x' are not lower.
>>> print(y.isupper())
True
'True' means all letters in 'y' are upper.
>>> print(x.replace("H" , "J"))
Jello
Here 'replace' method replaces 'H' with 'J'.
>>> y = 'HELLO, WORLD'
>>> print(y.split(','))
['HELLO', ' WORLD']
Suppose if we want to print 'Hello' 3 times,then we write-
>>> x = "Hello"
>>> print(x,x,x)
we can also write-
>>> x = "Hello"
>>> print(x * 3)
HelloHelloHello
If we want to add spaces in between 'Hello' then we can write-
>>> x = "Hello " (add space before second quote)
>>> print(x * 3)
Hello Hello Hello
For performing these operations click on – colabs
Tutorial -5
Boolean values:
In python boolean values are two constant objects ‘True’ & ‘False’. We use boolean values to find out result of some condition.
>>> True
True
>>> False
False
>>> true
Error: name 'true' is not defined.
>>> false
Error: name 'false' is not defined.
'true' & 'false' are not recognized by python.
Comparison operators:

Let's see how we use the above operators in python:
>>> 10 > 9
True
>>> 10 < 9
False
>>> 100 == 100
True
>>> 100 == 99
False
>>> 100 != 99
True
>>> x = 20
>>> y = 30
>>> x >= y
False
>>> x = 30
>>> y = 30
>>> x <= y
True
Here 'x' is equal to 'y'
>>> 'hello' == "hello"
True
>>> 'hello'.isupper()
False
>>> 'hello'.lower()
True
>>> 'hello'.isalpha()
True
>>> 'hello'.isalnum()
True
Logical operators:

Suppose if we have two conditions:
10 > 9 20 < 15
For such cases we use logical operators:
>>> 10 > 9 and 20 < 15
False
Here there are two conditions x(10>9) an y(20<15).
>>> 10 > 9 or 20 < 15
True
>>> 10 > 9 or 20 > 15
True
>>> not 10 > 9
False
>>> not 10 < 9
True
Tutorial – 6
Python IF ELSE statements:
‘if statement’ is used to execute the statement or block of code if & only if a condition is fulfill. Let’s see syntax & some examples:
>>> x = 100
>>> if x == 100:
>>> print ("x is = " , x)
x is = 100
Indent of four spaces or one tab away from start is necessary for execution of statement. When 'if' condition is true, whatever code is written below 'if' is executed. Suppose 'if' condition is false, so print statement will not be executed & it does not print anything. For ex:
>>> x = 100
>>> if x != 100:
>>> print ("x is = " , x)
Now lets talk about INDENTATION. Indentation in python is a way of marking a block of code. If we use Pycharm IDE, then as we press enter after ‘if statement” cursor automatically comes to four spaces from start. This means whatever we write after four spaces is a part of ‘if statement’. In C/C++ curly brackets define indentation, but in python we dont use curly brackets. Lets see some more examples:
>>> x = 99
>>> if x != 100:
>>> print ("x is = ")
>>> print (x)
x is =
99
>>> x = 100
>>> if x != 100:
>>> print ("x is = ")
>>> print (x)
>>> print("finish")
finish
In above case,'if' condition is false. But 'finish' is printed because it is not a part of 'if statement'.
So remember one thing, only those statements which are written after four spaces from start & is below ‘if statement’ is a part of ‘if’ . Lets see some more examples-
>>> x = 100
>>> if x != 100:
>>> print ("x is = ")
>>> print (x)
>>> if x > 0:
>>> print (" x is positive")
>>> else:
>>> print(" x is negative")
>>> print ("finish")
x is positive
finish
In above example, if and only if '(if x>0)' condition is false then only 'else' statement will executes.
>>> x = -100
>>> if x != 100:
>>> print ("x is = ")
>>> print (x)
>>> if x > 0:
>>> print (" x is positive")
>>> else:
>>> print(" x is negative")
>>> print ("finish")
x is =
-100
x is negative
finish
Lets see some examples using logical operators-
>>> x = -100
>>> if x != 100 or x>0: # also put x<0, same o/p comes
>>> print ("x is = ")
>>> print (x)
>>> if x > 0:
>>> print (" x is positive")
>>> else:
>>> print(" x is negative")
>>> print ("finish")
x is =
-100
x is negative
finish
>>> x = -100
>>> if x != 100 and x>0: # for 'and' both conditions 'true'
>>> print ("x is = ")
>>> print (x)
>>> if x > 0:
>>> print (" x is positive")
>>> else:
>>> print(" x is negative")
>>> print ("finish")
x is negative
finish
Tutorial – 7
elif statements:
We take one example in which we compare name given by user with predefined name. For doing this we will use elif statements. Lets see:
>>> name = input("enter a name: ")
>>> if name == "ballu":
>>> print("name entered is : " , name)
>>> elif name == "donald":
>>> print("name entered id : " , name)
>>> elif name == "smith":
>>> print("name entered id : " , name)
>>> elif name == "max":
>>> print("name entered id : " , name)
>>> else:
>>> print(" The name entered is invalid")
enter a name: ballu
name entered is : ballu
If we execute again-
enter a name: max
name entered is : max
If we execute again-
enter a name: abc
The name entered is invalid
The important thing is first we have to write ‘if statement’, then only we use ‘elif statement’. Lets see some more examples:
>>> name = input("enter a name: ")
>>> if name == "ballu":
>>> print("name entered is : " , name)
>>> if name == "ballu":
>>> print("name entered id : " , name)
>>> elif name == "smith":
>>> print("name entered id : " , name)
>>> elif name == "max":
>>> print("name entered id : " , name)
>>> else:
>>> print(" The name entered is invalid")
enter a name: ballu
name entered is : ballu
name entered is : ballu
In above example we use two ‘if’ statements, that is why two times ‘if’ statement is executed. The two ‘elif’ statements are part of second ‘if’ statement.
How to use nested if statement:
Suppose we want to evaluate ‘x’ is positive or negative number. So we will use ‘if’ & ‘else’ statement. If we also want to find out whether the no is odd or even, then we will use ‘if’ statement under ‘else’ statement. This is nested if statement. In nested if statement indentation is very important. Lets see example:
>>> x = 20
>>> if x < 0:
>>> print(" x is negative")
>>> else:
>>> print(" x is positive")
>>> if (x%2) == 0:
>>> print("x is even")
>>> else:
>>> print("x is odd")
x is positive
x is even
Tutorial – 8
Python Lists:
List in a python is a kind of collection which allows to put many values in a single variable. We can also say that list is an ordered set of values. Lets define list-
>>> x = [3,5,4,9,7,10]
>>> x
[3,5,4,9,7,10]
Values inside list is called elements. In above list 3,5,4... all are elements inside list 'x'. All these elements are ordered by their index & index starts with zero.
>>> x[0]
3
>>> x[4]
7
>>> y = ['max' , 1,15.5,[3,2]]
>>> y[0]
'max'
>>> y[3]
[3,2]
>>> len(x)
6
>>> len(y)
4
'len' is used to find out the length of list.
>>> x.insert(2,'tom')
>>> x
[3,5,'tom',4,9,7,10]
'insert' inserts tom at index 2.
>>> x.remove('tom')
>>> x
[3,5,4,9,7,10]
'remove' removes the element.
>>> x.insert(1,3)
>>> x
[3,3,5,4,9,7,10]
>>> x.remove(3)
>>> x
[3,5,4,9,7,10]
In above example,value at index zero is remove. It removes first element found at left i.e it starts from left.
>>> x.pop()
10
>>> x
[3,5,4,9,7]
'pop' is used to remove last value.
To delete whole list-
>>> z = [1,2,5,4]
>>> z
[1,2,5,4]
>>> del z
>>> z
error: name 'z' is not defined
>>> z = [1,2,5,4]
>>> z.clear()
>>> z
[]
'clear' is used to empty the list
>>> x = [3,5,4,9]
>>> x
[3,5,4,9]
>>> x.sort()
>>> x
[3,4,5,9]
using 'sort',all values are sorted in ascending order.
>>> x.reverse()
>>> x
[9,5,4,3]
>>> x.append(10)
>>> x
[9,5,4,3,10]
In above example, 10 is appended in list.
>>> s = x.copy()
>>> s
[9,5,4,3,10]
'copy' copies all contents of list 'x' into list 's'.
>>> x.append(10)
>>> x
[9,5,4,3,10,10]
>>> x.count(10)
2
>>> x.count(3)
1
>>> x.count(100)
0
Tutorial – 9
Python Tuples:
It is very similar to lists. Tuples are used to store collection of elements in a single variable. Tuples are immutable which means once tuples are created they cannot be changed or the content in them cannot be changed. We use parenthesis ( ) to declare tuples. Lets see-
>>> x = (1,5,3,4,8)
>>> x
(1,5,3,4,8)
Similar to list, we can call tuple element by index value.
>>> x[0]
1
>>> x[4]
8
>>> x[100]
IndexError: tuple index out of range.
We cannot change the content or assign any other value to tuple element if they are once initialized. Also we cannot use functions like ‘append, remove…’ in tuple which we were using in lists.
>>> x[0] = 2
TypeError:'tuple'object does not support item assignment.
>>> x.count(8)
1
It counts no. of '8' in tuple 'x'.
>>> len(x)
5
It gives length of tuple.
It is possible to save multiple data type values in tuple.
>>> y = (1,'max',1.6)
>>> y
(1,'max',1.6)
We can also join two tuples using concatenation operator.
>>> z = x + y
>>> z
(1,5,3,4,8,1,'max',1.6)
>>> a = ('hi',)*5
>>> a
('hi','hi','hi','hi','hi')
>>> a[2]
'hi'
We can also find maximum & minimum value in tuple.
>>> max(x)
8
>>> min(x)
1
We can also delete tuple -
>>> del z
>>> z
NameError:name 'z' is not defined.