Module 1 - Day 1

3 + 4
7
2 + 4
6

How to operate jupyter notebook

In markdown cell I can type notes for my reference. This all can be done within the program

 ===========   ====================================
  keys          action                              
  ===========   ====================================
  esc+m         convert the cell to markdown        
  esc+y         convert the cell to code            
  shift+enter   execute the cell                   
  !command      execute system command from jupyter 
  ===========   ====================================
3 + 4
7

the colon and square brackets will not be there for markdown cell!

3 + 4

Very basic things supported in python

Numeric data

1 + 3
4
2*34343453454564654654
68686906909129309308
5/2
2.5
5//2 # this is integer division
2

operators

These charecters are special . they are called as operators. Operator needs two items on its boths sides. left and and right hand side

+ - * / // **
+
  Cell In[9], line 1
    +
     ^
SyntaxError: invalid syntax
3 + 4 # both sides have to have compatible items on both sides
7
4 ** 40  # 4 raised to power 40
1208925819614629174706176
3.5 + 4.5 # real numbers, float in python
8.0
3 + 4.5
7.5

Can I have multiple operators in one line?

yes

5 + 5 - 4
6
5 - 5 * 2 # there is operator precedence
-5
4 * 3 / 2
6.0
(5 - 5 )* 2 # round brackets will change the precdence
0

operator precedence

  ============   ========
  operators      priority
  ============   ========
  ``**``         1
  ``% // / *``   2
  ``+ -``        3
  ============   ========
7 % 5 # it gives remainder
2

Problem

Compound interest is computed using this formula \(P (1 + r/n)^{nt}\)

P Pinciple = 26780
r rate of interest = 0.07
n number of times interest is compunded in a year = 4
t number of years = 5
2(1 + 3) # python does not understand this
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/ipykernel_115462/3832789996.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(1 + 3) # python does not understand this
/tmp/ipykernel_115462/3832789996.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(1 + 3) # python does not understand this
/tmp/ipykernel_115462/3832789996.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  2(1 + 3) # python does not understand this
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 2(1 + 3) # python does not understand this

TypeError: 'int' object is not callable
2 * (1 + 3) 
8
26780*(1+0.07/4)**(4*5)
37887.76008234032

Text data

"this is text"
'this is text'
'this is also text data'
'this is also text data'
"*"*5 # you can repeat the text 5 times by multiplying it with 5
'*****'
"="*50
'=================================================='
"vikrant" + "patil" # concatenate the text/string
'vikrantpatil'
 4 + 5
9
"test" + "postfix"
'testpostfix'
1 + "st" # on both sides there are data types which are compatible for addition!
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[36], line 1
----> 1 1 + "st" # on both sides there are data types which are compatible for addition!

TypeError: unsupported operand type(s) for +: 'int' and 'str'
"1" # this is not integer 1, it is text
'1'
1
1
"1" + "st"
'1st'
1 + "st"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[41], line 1
----> 1 1 + "st"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
"""poem line 1
this is next line
this yet another line
and this one is last line"""
'poem line 1\nthis is next line\nthis yet another line\nand this one is last line'

Variable and Literals

There is another operator called =. This operator allows us to create variables

P = 26000 # this creates a variable called P
P
26000
P * 2
52000
26000 # is an int with value 
26000
P # P is a variable with name "P"
26000
P = 30000
P*2 # look at the numbers shown in sqaure brackets on left side
52000
a
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[55], line 1
----> 1 a

NameError: name 'a' is not defined
X*2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[56], line 1
----> 1 X*2

NameError: name 'X' is not defined
X = 10
X * 2
20
2 # this is called literal
2
"vikrant" # literal of text!
'vikrant'
"X" # literal text which has X charecter in it
'X'
X # i have not given any quote here, this taken as variable
10
10 
10
X 
10
int - datatype
10 -> literal value of one int
  Cell In[66], line 2
    10 -> literal value of one int
       ^
SyntaxError: invalid syntax
X # this is just a name
10
Y = "some other text"
Y # Y is a variable with name Y, and it has text data stored in it
'some other text'
X # X is a variable with name X and it has int dat stored in it
10
"ytytreyt"  # literal with type as text
'ytytreyt'
4.5 # literal with type as float
4.5
10 # is literal with type as int
10
10 * 54 * 7
3780
a, b = 2, 3 # this will assign two variables in one statement
a
2
b
3
x,y,z = 10,20,30
x
10
y
20
z 
30
del X,Y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[87], line 1
----> 1 del X,Y

NameError: name 'X' is not defined
X
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[86], line 1
----> 1 X

NameError: name 'X' is not defined

Problem

X = 10
Y = X
X = X + 10

What will be value of Y?
X = 10
Y = X
X = X + 10
Y
10
del X,Y
X = 10
Y = X
X = X + 10
Y # this will show value of Y 
10

Problem

What will be value of x after executing all statements?

    x = 10
    y = x
    y = 25
x = 10
y = x
y = 25
x
10

Indexing of text

text = "Python"
text
'Python'
text*2
'PythonPython'
text + text
'PythonPython'
text[1] # sqaure brackets after a text variable means indexing particular char from that text
'y'
text[0] 
'P'
text[1]
'y'
text[2]
't'
text[10]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[103], line 1
----> 1 text[10]

IndexError: string index out of range
text[-1] # this give last char
'n'
   +---+---+---+---+---+---+
   | P | y | t | h | o | n |
   +---+---+---+---+---+---+
->   0   1   2   3   4   5   
    -6  -5  -4  -3  -2  -1    <-

Collections

numbers = [1, 2, 3, 4, 5]
numbers[0]
1
numbers[1]
2
numbers[-1]
5
words = ["one", "two", "three", "four"]
words
['one', 'two', 'three', 'four']
mixed = ["one", 1, "two", 3847623, 3.5]
mixed
['one', 1, 'two', 3847623, 3.5]
mixed[0]
'one'
mixed[-1] # last element
3.5
mixed[-1]
3.5
mixed[-2]
3847623
mixed[2] # sqauare brackets are important if you want to index
'two'
mixed(2) # round bracket has different meaning
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[120], line 1
----> 1 mixed(2) # round bracket has different meaning

TypeError: 'list' object is not callable
2 # this is not text, this is an int
2
"test" # this is text
'test'
data = ["x", "y", "z"]
data
['x', 'y', 'z']
data = [x, y, z] # this will take x, y, z as variables
data
[10, 25, 30]
ones = [1, 1, 1, 1, 1, 1, 1, 1] 
ones
[1, 1, 1, 1, 1, 1, 1, 1]
one = 1
newones = [one, one, one, one]
newones
[1, 1, 1, 1]

list can have anything in it

all_of_them = [numbers, words, mixed, ones, newones]
all_of_them
[[1, 2, 3, 4, 5],
 ['one', 'two', 'three', 'four'],
 ['one', 1, 'two', 3847623, 3.5],
 [1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1]]
all_of_them[0] # I should be getting numbers
[1, 2, 3, 4, 5]
all_of_them[0][0] # what will be output
1
all_of_them[-1] # last item
[1, 1, 1, 1]
all_of_them[-1][0]
1
all_of_them[1][2]
'three'
matrix = [[11, 12, 13],[21, 22, 23],[31, 32, 33]]
matrix
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
matrix[0]
[11, 12, 13]
matrix[1]
[21, 22, 23]
matrix[2]
[31, 32, 33]

Some commom mistakes

words[1,3] # to access element from list/string in square bracket only one valid index has to be given! 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[148], line 1
----> 1 words[1,3] # to access element from list/string in square bracket only one valid index has to be given! 

TypeError: list indices must be integers or slices, not tuple
words[1]
'two'
words(2) # sqaure brackets are needed
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[150], line 1
----> 1 words(2) # sqaure brackets are needed

TypeError: 'list' object is not callable
x
10
x[0] # sqaure bracket can be given only after text/list
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[152], line 1
----> 1 x[0] # sqaure bracket can be given only after text/list

TypeError: 'int' object is not subscriptable
hfsdgfdgdf[0]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[153], line 1
----> 1 hfsdgfdgdf[0]

NameError: name 'hfsdgfdgdf' is not defined

some opeartions with list

ones
[1, 1, 1, 1, 1, 1, 1, 1]
ones + ones # concatenates
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ones + words
[1, 1, 1, 1, 1, 1, 1, 1, 'one', 'two', 'three', 'four']
ones
[1, 1, 1, 1, 1, 1, 1, 1]
words
['one', 'two', 'three', 'four']
words
['one', 'two', 'three', 'four']
words[0] = "ONE" # you can modify list in place
words
['ONE', 'two', 'three', 'four']
text 
'Python'
text[-1] = "N"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[168], line 1
----> 1 text[-1] = "N"

TypeError: 'str' object does not support item assignment
ones
[1, 1, 1, 1, 1, 1, 1, 1]
textlist = ['P','y','t','h','o','n']
textlist
['P', 'y', 't', 'h', 'o', 'n']
textlist[-1] = "N"
textlist
['P', 'y', 't', 'h', 'o', 'N']
yetanother_textlist = ["python"]
yetanother_textlist[0]
'python'
yetanother_textlist[0][-1]
'n'
yetanother_textlist[0][-1] = "N"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[180], line 1
----> 1 yetanother_textlist[0][-1] = "N"

TypeError: 'str' object does not support item assignment
yetanother_textlist[0] = "PYTHON"
yetanother_textlist
['PYTHON']
ones*3 # it will repeat the list three times
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ones + ones
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ones + words
[1, 1, 1, 1, 1, 1, 1, 1, 'ONE', 'two', 'three', 'four']

Dictionary

in dictionary we save name and value pairs. So that we can retrive values with name instead of index (as in list)

words[0]
'ONE'
words[1]
'two'
stock = {"ticker" :"IBM",
         "open" : 123,
         "close" : 125,
         "low" : 122,
         "high": 125.5
        }
stock
{'ticker': 'IBM', 'open': 123, 'close': 125, 'low': 122, 'high': 125.5}
stock['ticker']
'IBM'
stock['high']
125.5
stock['low']
122
stock['open']
123
person = {"name":"Vikrant",
         "address": "Maharasthra",
         "email": "vikrant@abc.com"}
person['name']
'Vikrant'
person['address']
'Maharasthra'
person['country']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[198], line 1
----> 1 person['country']

KeyError: 'country'

What keys and values are possible?

  • Key can be string, int
  • value can be anything
{1.2: "test"}
{1.2: 'test'}
biodiversity_data = {'species' : 'homosepian',
                     "location": [23.00, 24.8]}
biodiversity_data["location"]
[23.0, 24.8]

some other datatypes

tuples are similar to lists, but they can not be modified once created

color = (0, 0, 255) # r, g, b 
color[0]
0
color[-1]
255
color[0] = 255
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[207], line 1
----> 1 color[0] = 255

TypeError: 'tuple' object does not support item assignment
True # boolean
True
False
False
None  # this is to represent nothing

Key things to remembers

  • to make a list we make use square brackets []

  • to make a string we make use of single or double quotes

  • to make a tuple we make use of round brackets ()

  • to make a dictionary we make use of curley brackets {}, inside you have to give key:value pairs separated by comma

  • to access item from a string we use square bracket and integer index inside that string[2]

  • to access item from a list we use square bracket and integer index inside that list[2]

  • to access item from a tuple we use square bracket and integer index inside that tuple[2]

  • to access item from a dictionary we use square bracket and key inside that d[‘key’]to make a dictionary we make use of curley brackets {}, inside you have to give

{"A":1,
 "B":1}
{'A': 1, 'B': 1}