6  Module1 - Day1

Today’s Topics

1 + 2
3
4 * 5
20

6.1 Numeric and Text data

2 + 5 # there are integers and integers operate with opeartors like +, *, -, /, //, **
7
(3 + 5)*4
32
3 **2 # power
9
2 ** 100
1267650600228229401496703205376
5 / 2 # this will create real number
2.5
5 // 2 # integer division
2
5  % 2
1
13 % 10
3
4.5 + 5.5
10.0
"this is python training"
'this is python training'
'this is also text'
'this is also text'
"नमस्कार"
'नमस्कार'

problem

Compound interest is calculated using formula P (1 + r/n)^nt For this formula, P is the principal amount, r is the rate of interest per annum, n denotes the number of times in a year the interest gets compounded, and t denotes the number of years. Use python to compute compound interest for principle amount of 26780, rate of interest 7%, interest is compounded 4 quarterly, and amount is invested for 5 years.

c(a+b)

100*(200+300)

100(200+300)
<>: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_53860/424738918.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
  100(200+300)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 100(200+300)

TypeError: 'int' object is not callable

These symbols +, -, *, **, /, // are called as operators. And calculator commands above are called statements. Two or more operators can be used in single statement. When two operators come in single statement , there is predefined hirarchy or precedence. Here is list arranged from highest precedence to lowest precedence operator.

  ============   ========
  operators      priority
  ============   ========
  ``**``         1
  ``% // / *``   2
  ``+ -``        3
  ============   ========

6.2 Variables and Literals

x = 10
x + 3 # we will recall what is in x
13
y = 45
x + y
55
P = 26780
r = 0.07
n = 4
t = 5
P
26780
P*(1 + r/n)**(n*t)
37887.76008234032
"x" # it is text
'x'
x # anything that does not start with ' or " it is not text... it is taken as variable name
10
z
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[32], line 1
----> 1 z

NameError: name 'z' is not defined
20 # this is not a variable but it is an integer with value 20, this is called as literal
20
x = 20 # x is a variable in which value 20 is stored
x + 20 # I am adding literal 20 into value that is stored 
40
name = "python"
name
'python'
3  + 4
7
"hello" + " python"
'hello python'
"hello " + name # here "hello " is a literal and name is a variable
'hello python'
x + y
65
x+y
65

There are some rules for forming variable names

  • The variable name can’t start with a number
  • it can be single word (it can not have space in it)
  • it can alphabets (upper as well as lower case), numbers (not at start), underscore
  • names are case seisitive
a , b = 2, 3
a
2
b
3
Z = 42 # i am not executing this cell delibearately
Z + 3
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[49], line 1
----> 1 Z + 3

NameError: name 'Z' is not defined
Name = Piyush # Name is variable, the value assigned is also variable
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[51], line 1
----> 1 Name = Piyush # Name is variable, the value assigned is also variable

NameError: name 'Piyush' is not defined
Name = "Piyush"
"Hi " + Name
'Hi Piyush'
x = 10
z = x
x
10
z
10
m = h
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[58], line 1
----> 1 m = h

NameError: name 'h' is not defined
Name + " hello"
'Piyush hello'
P = 26780
r = 0.07
n = 4
t = 5
r = 0.08
n = 2
P*(1 + r/n)**(n*t)
39640.941950113265
# case 1
P = 26780
r = 0.07
n = 4
t = 5

finalsum1 = P*(1 + r/n)**(n*t)

# case2 
r = 0.08
n = 2

finalsum2 = P*(1 + r/n)**(n*t)
finalsum1
37887.76008234032
finalsum2
39640.941950113265

problem

Have a look at following python statements.

    x = 10
    y = x
    x = x + 10

What will be value of y after this?

x = 10
y = x # before changing x in next line , I have creatd alias to original value of x
x = x + 10
y
10

problem What will be value of x after executing all statements?

    x = 10
    y = x
    y = 25

6.3 Text indexing

name = "python"
name + " hello"
'python hello'
name * 3 # you can also multiply text with integers. it repeats the text those many times
'pythonpythonpython'
star = "*"
star*5
'*****'
star*10
'**********'
star
'*'
name
'python'
name[0] # square bracket after the variable name means indexing
'p'
name[1]
'y'
name[2]
't'
name[3]
'h'
name[4]
'o'
name[5]
'n'
name[6] # if you give index more than or eqal to lenght of text it will give error
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[84], line 1
----> 1 name[6] # if you give index more than or eqal to lenght of text it will give error

IndexError: string index out of range
name[-1] # this means give me last item
'n'
name
'python'
name[-2] # give me second last
'o'
name[-6] 
'p'

Indices work like this

   +---+---+---+---+---+---+
   | P | y | t | h | o | n |
   +---+---+---+---+---+---+
     0   1   2   3   4   5   
    -6  -5  -4  -3  -2  -1
name
'python'
Name # case sensitive
'Piyush'
text = "This is some statement"
poem = '''this is first line
and this is second line
and this is third line
this is end line'''
poem
'this is first line\nand this is second line\nand this is third line\nthis is end line'
poem[-1]
'e'

6.4 Collections

Collections are nothing but combining the basic data types to from literraly “collection”

6.4.1 list

primes = [2, 3, 5, 7, 11, 13, 17, 19]
primes
[2, 3, 5, 7, 11, 13, 17, 19]
primes[0] # this gives me zeroth item from the list
2
primes[-1] # last item 
19
primes + [23, 29] # you can ad two list ... which just concatenates the two lists
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
ones = [1, 1, 1]
ones*3 # it will replicate the list three times
[1, 1, 1, 1, 1, 1, 1, 1, 1]
words = ["one", "two", "three"]
words
['one', 'two', 'three']
words[0]
'one'
words[-1]
'three'
mixed = ["one", "two", 1, 2, 3.4]
mixed[0]
'one'
mixed[2]
1
mixed[-1]
3.4
ones
[1, 1, 1]

it is also possible to modify the list in place

ones[0] = 0
ones
[0, 1, 1]
text = "hello"
text[0] = "H"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[118], line 1
----> 1 text[0] = "H"

TypeError: 'str' object does not support item assignment
primes
[2, 3, 5, 7, 11, 13, 17, 19]
primes[0] = 1
primes
[1, 3, 5, 7, 11, 13, 17, 19]
star1 = "*"
star1
'*'

6.4.2 tuples

Tuples are siblings of list excpet that you can not modify tuple once created

color = (255, 0, 255)
color[0]
255
color[0] = 56
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[127], line 1
----> 1 color[0] = 56

TypeError: 'tuple' object does not support item assignment

6.4.3 Boolean

True
True
False
False

6.5 Functions

Function is a variable, which can be used in special way. if we put () after the variable … it called

Lets have a look at some built in functions… called builtins!

primes
[1, 3, 5, 7, 11, 13, 17, 19]
name
'python'
name()  # I am calling name ...as a callable
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[132], line 1
----> 1 name()  # I am calling name ...as a callable

TypeError: 'str' object is not callable
len(primes)
8
primes
[1, 3, 5, 7, 11, 13, 17, 19]
name
'python'
len(name)
6
words
['one', 'two', 'three']
len(words)
3
len(color)
3
x = 100
x
100
len(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[142], line 1
----> 1 len(x)

TypeError: object of type 'int' has no len()
textx = "100" # here "100" is a text is it not a number 
textx
'100'
len(textx)
3
x
100
"hello"
'hello'
len("hello")
5
len(56)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[149], line 1
----> 1 len(56)

TypeError: object of type 'int' has no len()
2**100
1267650600228229401496703205376
str(100)
'100'
str(45.5)
'45.5'
x
100
str(x)
'100'
v = 2**100
vtext = str(v)
len(vtext)
31

6.5.1 Dictionary

  • list is ordered collection of objects
  • dictionary is named collection of objects
stock = {"name":"INFY", "value":2003.3, "high":2005.0, "low":2003}
stock['name']
'INFY'
stock['value']
2003.3
stock_list = ["INFY", 2003.3, 2005.0, 2003]
stock_list[0]
'INFY'
stock1 = {"name":"TATA", "value":500, "high":510, "low" : 500}
stock1['name']
'TATA'
student = {"name": "Alice", "place":"Wonderland"}

dictionary is collection of key and value pairs. - keys can be text/integer - values can be anything

len(student)
2
len(stock)
4
stock = {"name":"INFY", 
         "value":2003.3, 
         "high":2005.0, 
         "low":2003}
stock['name']
'INFY'
len(stock)
4
stock['name']
'INFY'
len(stock['name']) # you will get length of values that is stored for key "name"
4
name
'python'
stock
{'name': 'INFY', 'value': 2003.3, 'high': 2005.0, 'low': 2003}
stock1
{'name': 'TATA', 'value': 500, 'high': 510, 'low': 500}
stocks = [stock, stock1]
stocks
[{'name': 'INFY', 'value': 2003.3, 'high': 2005.0, 'low': 2003},
 {'name': 'TATA', 'value': 500, 'high': 510, 'low': 500}]
stocks[0]
{'name': 'INFY', 'value': 2003.3, 'high': 2005.0, 'low': 2003}
stocks[1]
{'name': 'TATA', 'value': 500, 'high': 510, 'low': 500}
stocks[0]['name']
'INFY'
stocks[1]['low']
500
len(stock)
4
len(stocks) 
2
str(454)
'454'
primes
[1, 3, 5, 7, 11, 13, 17, 19]
max(primes)
19
min(primes)
1
sum(primes)
76
sorted(primes)
[1, 3, 5, 7, 11, 13, 17, 19]
randomorder = [23, 12, 3, 1, 54, 42, 76, 3, 1]
sorted(randomorder)
[1, 1, 3, 3, 12, 23, 42, 54, 76]
randomorder
[23, 12, 3, 1, 54, 42, 76, 3, 1]

Represent this data in python using lists/dictionaries/basic data types

symbol,value,high,low,volume
APPLE,234.5,240.3,233,100
AT&T,300.0,301,299,20
IBM,125.7,127.3,123,50
NIKE,100.5,105.0,104,1000
stock
{'name': 'INFY', 'value': 2003.3, 'high': 2005.0, 'low': 2003}
stock[name]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[196], line 1
----> 1 stock[name]

KeyError: 'python'
stock['name']
'INFY'
stocks = [
    {"symbol":"APPLE", "value":234.5, "high":240.3, "low":233, "volume":100},
    {"symbol":"AT&T", "value": 300.0, "high": 301, "low":299, "volume":20},
    {"symbol":"IBM", "value":125.7, "high":127.3, "low":123, "volume":50},
]
stocks[0]
{'symbol': 'APPLE', 'value': 234.5, 'high': 240.3, 'low': 233, 'volume': 100}
data = [["APPLE",234.5,240.3,233,100],
["AT&T",300.0,301,299,20],
["IBM",125.7,127.3,123,50],
["NIKE",100.5,105.0,104,1000]]
data[0][0] # does not give enough info, zeroth row , zeroth column
'APPLE'
stocks[0]['symbol'] # zeorth row access symbol
'APPLE'