Difference makes the DIFFERENCE
topic = "Foundations of DataScience"
print(topic)
print(topic[0])
# prints the very first character...
print(topic[10])
print(topic[-1])
# prints the last character in the given string
print(topic[0:10])
# take everything from 0 to 10th character (inclusive of 0th character and excluding 10th character)
print(topic[12:16])
print(topic.lower())
print(topic.upper())
# here the actual variable topic is not converted to lower case but it is converted and displayed in lower case
# to make the string variable into lower case then
topic = topic.lower()
print (topic)
topic = topic.upper()
print(topic)
topic.islower() #returns either True or False, if the data in the variable is lower case
topic.isupper()
topic.isalnum()
print(topic.isupper())
print(topic)
print(topic.find("DATA"))
print(topic[15])
print(topic.replace('DATA', 'DDAATTAA '))
golden_ratio = 55/34
print(type(golden_ratio))
print(golden_ratio.is_integer())
print(golden_ratio.as_integer_ratio())
print(3642617345667313/2251799813685248)
print(golden_ratio)
print(55//34, 55/34)
# to get remainder
print(55 % 34)
print("exponentation: 2 ** 3 = ", 2**3)
hours_per_week = 10
my_name = "Arundhati"
if hours_per_week >= 10:
print (my_name + "'s working hours are greater than 10hrs, doing well")
if hours_per_week > 10:
print("you are doing well")
else:
print("You need to do more hours")
# 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)
i = 1
j = 1
print(i)
print(j)
while j < 100:
temp = i + j
i = j
j = temp
print(temp)
def fibonacci(pos):
a = 1
b = 1
for i in range(pos):
temp = a + b
b = a
a = temp
return temp
# instead of printing the entire output, we can also print only the outcome of the target number
fibonacci(5)
fibonacci(3)
print(fibonacci(2), fibonacci(4))
print(fibonacci(7), fibonacci(8))
# 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)
print(fibo_relative(10,3,2))
# 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))
# 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
print(fibo_overload(10))
print(fibo_overload(5, 5, 5))
def fibo_recursive(n, a = 1, b = 1):
if n > 1:
return fibo_recursive(n - 1, b, a + b)
else:
return a + b
print(fibo_recursive(10))