gallery/data entry fields
PYTHON BASICS
from PadhAI courseware
Padhai_Foundations
In [ ]:

Strings and their... processing

In [1]:
topic = "Foundations of DataScience"
print(topic)
Foundations of DataScience
In [2]:
print(topic[0])
# prints the very first character...
F
In [3]:
print(topic[10])
s
In [4]:
print(topic[-1])
# prints the last character in the given string
e
In [5]:
print(topic[0:10])
# take everything from 0 to 10th character (inclusive of 0th character and excluding 10th character)
Foundation
In [6]:
print(topic[12:16])
of D
In [7]:
print(topic.lower())
foundations of datascience
In [8]:
print(topic.upper())
# here the actual variable topic is not converted to lower case but it is converted and displayed in lower case
FOUNDATIONS OF DATASCIENCE
In [9]:
# to make the string variable into lower case then 
topic = topic.lower()
print (topic)
foundations of datascience
In [10]:
topic = topic.upper()
print(topic)
FOUNDATIONS OF DATASCIENCE
In [11]:
topic.islower()  #returns either True or False, if the data in the variable is lower case
Out[11]:
False
In [12]:
topic.isupper()
Out[12]:
True
In [13]:
topic.isalnum()
Out[13]:
False
In [14]:
print(topic.isupper())
True
In [15]:
print(topic)
FOUNDATIONS OF DATASCIENCE
In [16]:
print(topic.find("DATA"))
15
In [17]:
print(topic[15])
D
In [18]:
print(topic.replace('DATA', 'DDAATTAA '))
FOUNDATIONS OF DDAATTAA SCIENCE
In [19]:
golden_ratio = 55/34
In [20]:
print(type(golden_ratio))
<class 'float'>
In [21]:
print(golden_ratio.is_integer())
False
In [22]:
print(golden_ratio.as_integer_ratio())
(3642617345667313, 2251799813685248)
In [23]:
print(3642617345667313/2251799813685248)
1.6176470588235294
In [24]:
print(golden_ratio)
1.6176470588235294
In [25]:
print(55//34, 55/34)
1 1.6176470588235294
In [26]:
# to get remainder
print(55 % 34)
21
In [27]:
print("exponentation: 2 ** 3 = ", 2**3)
exponentation: 2 ** 3 =  8
In [28]:
hours_per_week = 10
my_name = "Arundhati"
In [29]:
if hours_per_week >= 10:
    print (my_name + "'s working hours are greater than 10hrs, doing well")
Arundhati's working hours are greater than 10hrs, doing well
In [30]:
if hours_per_week > 10:
    print("you are doing well")
else:
    print("You need to do more hours")
You need to do more hours
In [54]:
# printing Fibonacci series with loops
i = 1
j = 1
print(i)
print(j)
for k in range(20):
    temp = i + j
    i = j
    j = temp
    print(k, temp)
1
1
0 2
1 3
2 5
3 8
4 13
5 21
6 34
7 55
8 89
9 144
10 233
11 377
12 610
13 987
14 1597
15 2584
16 4181
17 6765
18 10946
19 17711
In [36]:
i = 1
j = 1
print(i)
print(j)
while j < 100:
    temp = i + j
    i =  j
    j = temp
    print(temp)
1
1
2
3
5
8
13
21
34
55
89
144
In [39]:
def fibonacci(pos):
    a = 1
    b = 1
    for i in range(pos):
        temp = a + b
        b = a
        a = temp
    return temp
In [44]:
# instead of printing the entire output, we can also print only the outcome of the target number
fibonacci(5)
Out[44]:
13
In [41]:
fibonacci(3)
Out[41]:
5
In [46]:
print(fibonacci(2), fibonacci(4))
3 8
In [47]:
print(fibonacci(7), fibonacci(8))
34 55
In [64]:
# the initial values need not be 1 always, but can be desired sequence for a and b
def fibo_relative(pos, a, b):
    print(a)
    print(b)
    for i in range(pos):
        temp = a + b
        a = b
        b = temp
        print(temp)
    return(temp)
In [66]:
print(fibo_relative(10,3,2))
3
2
5
7
12
19
31
50
81
131
212
343
343
In [72]:
# the series starts from the given initial values for a and b 
# in the following example, a = 34 as initial value, instead of 1 and
# b has the value of 55,
# so the loop runs with initial values of 34 and 55 for 3 iterations
print(fibo_relative(3, 34, 55))
34
55
89
144
233
233
In [83]:
# writing the same function with initial values in the funtion definition itself
# in this example, the initial values are optional - meaning
# if the initial values are not given, 1 is taken for a and b, if given, the given values are considered
def fibo_overload(pos, a=1, b=1):
    for i in range(pos):
        temp = a + b
        a = b
        b = temp
        print(temp)
    return temp
In [84]:
print(fibo_overload(10))
2
3
5
8
13
21
34
55
89
144
144
In [85]:
print(fibo_overload(5, 5, 5))
10
15
25
40
65
65

recursive function

In [86]:
def fibo_recursive(n, a = 1, b = 1):
    if n > 1:
        return fibo_recursive(n - 1, b, a + b)
    else:
        return a + b
In [88]:
print(fibo_recursive(10))
144
In [ ]: