There are two types of cells in jupyter notebook 1. Markdown cell - in which you can type a text or information like this cell 2. code cell - in which you can type python code and then execute it
You should know this - by default a new cell is code cell - esc + m will convert that cell in to markdon cell - esc + y will convert the cell into code cell - shift + enter will execute current cell
Basics of python programming
Numeric Data
42+42# it has integers
84
42*43# multiplication
1806
45/2
22.5
45//2# integer division
22
2**5# power
32
5%2# modulus operator , remainder when divided what ever is on right side
1
1.2+4.2# real number .. we call it float in python
5.4
5.0*2.0
10.0
5.0*3*45
675.0
5*3*45
675
see the difference in results.. one is float/real number and another is integer
operators have priorities just like in excel or what we know from mathematics
1+4*5# multiplication/division/modulus have higher priority as compared +-
21
4*5**2# highest priority is for power
100
(1+4)*5# you can adjust the priority by puting bracket
25
4*5%2# * will be executed first and then % ..
0
If operators of same priority are there in a statement then which ever comes first (left) will be executed first
Text data
"this is text data"# this is text data
'this is text data'
'this is also text data'# data in single quotes is also text
'this is also text data'
"this is ' # this is syntax error
SyntaxError: unterminated string literal (detected at line 1) (800093987.py, line 1)
"this has to start with double quote and end with same"
'this has to start with double quote and end with same'
'similarly you can start with single quote and end with'
'similarly you can start with single quote and end with'
"you can have special chars like \n\t\\"
'you can have special chars like \n \t \\'
"c:\Program Files\Python3.10"# this is not correct because \ has a menaing in python text data
'c:\\Program Files\\Python3.10'
"c:\\Program Files\\Python3.10"
'c:\\Program Files\\Python3.10'
"hello"*5# this means repeate the string 5 times
'hellohellohellohellohello'
"hello "+"world"# concatenate
'hello world'
"hello"+'world'
'helloworld'
"He said, 'I am fine with quote!'"
"He said, 'I am fine with quote!'"
"a string with double quotes at both ends can have single quote ' inside the text"
"a string with double quotes at both ends can have single quote ' inside the text"
'a string with single quotes on both side can have double quote inside"the text'
'a string with single quotes on both side can have double quote inside"the text'
'can not have single quote ' here ' # because the second quote here is eneding the string
SyntaxError: unterminated string literal (detected at line 1) (3036092906.py, line 1)
"hello"*-4# it resulted into empty string
''
"hello"*-1
''
"*"*5
'*****'
n =5
"*"*n # this I expect as five stars
'*****'
n =-1"*"*n
''
"hello"+5
TypeError: can only concatenate str (not "int") to str
Problem
Compound interest calculated as using this formula P(1+r/n)^nt.
P = principle amount
n = number of times a year the interest is compounded
t = number of years
r = rate of interest
P = 26780
r = 7%
n = 4
t = 5
2(3+5)
<>: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_1729313/740595946.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2(3+5)
/tmp/ipykernel_1729313/740595946.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2(3+5)
/tmp/ipykernel_1729313/740595946.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2(3+5)
TypeError: 'int' object is not callable
23# python does not understand what to do with these two number
SyntaxError: invalid syntax (162316803.py, line 1)
2+3
5
(3+5)
8
2 (3+5) # it doesn't know that 2 and result of bracket should be multiplied
<>: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_1729313/1144834379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2 (3+5) # it doesn't know that 2 and result of bracket should be multiplied
/tmp/ipykernel_1729313/1144834379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2 (3+5) # it doesn't know that 2 and result of bracket should be multiplied
/tmp/ipykernel_1729313/1144834379.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
2 (3+5) # it doesn't know that 2 and result of bracket should be multiplied
TypeError: 'int' object is not callable
2*(3+5)
16
# in one code cell you can write multiple linesP =26780r =0.07n =4t =5
26780*(1+0.07/4)**(n*t) # P(1+r/n)^nt
37887.76008234032
X =10Y =20
X # this will show what is in X
10
Y # this will show what is in Y
20
X + Y
30
X *2
20
P =26780r =0.07n =4t =5
P*(1+ r/n)**(n*t)
37887.76008234032
result = P*(1+ r/n)**(n*t)
result
37887.76008234032
Variables and Literals
X
10
Y
20
P
26780
"X"# this is not variable X, this is string "X"
'X'
X is variable
“X” is literal string
2
2
m =2# m is variable and 2 is literal
n = m +2# here again n and m is variable and 2 is literal
5# literal 5
5
five =5# here five is variable
Rules for name of a variable
The variable name can not be a number
The varibale name can not start with a number
It can not have hyphen or any operator in the name
a, b =2, 3# you can assign two variables at a time
a
2
b
3
first, last ="vikrant","patil"
first
'vikrant'
last
'patil'
x, y, z =1, 2, 3# only condition is it has to seperated by comma
x,y =1, 2, 3
ValueError: too many values to unpack (expected 2)
x, y, z =1, 2
ValueError: not enough values to unpack (expected 3, got 2)
quiz
x = 10
y = x
x = x + 10
what will be the value of y after this
x =10y = xx = x +10
y
10
x
20
x = 10
y = x
y = 25
value of x?
x =10y = xy =25
x
10
Y # python is case sensitive so Y and y are taken different variables
20
y
25
more on text data
name ="Arcesium"# there is some text data in a variable called name
What if I want to access elements of this text
name[0] # in python 0 is the start
'A'
name[1]
'r'
name[4]
's'
name[7]
'm'
name[-1] # what this can be
'm'
name[-1] # if -ve number is given indexing will start from other end of text data!
'm'
P Y T H O N
-> 0 1 2 3 4 5
-6 -5 -4 -3 -2 -1 <---
s ="PYTHON"
s[0]
'P'
s[-6]
'P'
s[-1] # last element
'N'
Collection
The data types that we saw as of now are called basic basic data types. Then there are higher level data types which is nothing but collection of basic data types.
List List is collection where you can put together many objects serially.
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
nums = [1, 2, 3, 4, 5]
nums
[1, 2, 3, 4, 5]
nums[0] # just like text/string you can access nth item from the list
1
list_of_words = ["one", "two", "three"]
list_of_words
['one', 'two', 'three']
list_of_words[0]
'one'
list_of_words[-1]
'three'
list_of_mix = [1, 2, "hello", "world", "one"]
list_of_mix[0]
1
list_of_mix[4]
'one'
words # there is no such variable defined
NameError: name 'words' is not defined
another_list = [list_of_words, nums]
another_list # it is has two elements ... two lists.. list_of_words, nums
[['one', 'two', 'three'], [1, 2, 3, 4, 5]]
another_list[0] # what will be this?
['one', 'two', 'three']
another_list[1]
[1, 2, 3, 4, 5]
another_list[0][0]
'one'
another_list[0][-1]
'three'
name ="vikrant"
name[0]
'v'
bignumber =6578423
bignumber[0] # this will not give me first digit!
TypeError: 'int' object is not subscriptable
bignumber_str ="6578423"
bignumber_str[0]
'6'
bignumber*2
13156846
bignumber_str+2
TypeError: can only concatenate str (not "int") to str
bignumber_str*2# this will concatenate it two times
'65784236578423'
list_of_words[0] # square bracket
'one'
list_of_words(0) # round bracket has different meaning
TypeError: 'list' object is not callable
Just like strings, you can have * and + operators working on list
We have a different collection kind of data structure where objects can be saved with name and values. Dictioanry is collection of key and value. Key is string most of the times (but not necessary), value can be anything