# data[].attributes.body
def not_bad(s):
bad = s.find('bad')
nt = s.find('not')
if(bad > nt):
new_string = s.replace('not', '').replace('bad', 'good')
return new_string
return s
print(not_bad('This movie is not so bad'))
print(not_bad('This dinner is not that bad'))
print(not_bad('This tea is not so hot'))
print(not_bad("It's bad yet not"))

# data[].attributes.body
def not_bad(word):
not_index = word.find("not")
bad_index = word.find("bad")

if not_index < bad_index :
return word[0:not_index] + word[not_index:bad_index+3].replace(word[not_index:bad_index+3],"good") + word[bad_index+3:]
else:
return word

print (not_bad('This movie is not so bad'))
print (not_bad('This dinner is not that bad!'))
print (not_bad('This tea is not hot'))
print (not_bad("It's bad yet not"))

# data[].attributes.body
def not_bad(s):

not_index = 0
not_index = s.index("not")
if "bad" in s[not_index:]:
return s[0:not_index] + " good"
else:
return s

print(not_bad('This movie is not so bad'))
print(not_bad('This dinner is not that bad!'))
print(not_bad('This tea is not hot'))
print(not_bad("It's bad yet not"))

# data[].attributes.body
def not_bad(s):
if s.find("not")>1and s.find("bad")>1:
s=s[:s.find("not")]+"good"
return s

# data[].attributes.body
def verbing(word):
sentence =""
if len(word)>=3:
if word[-3:] == "ing":
sentence = word + "ly"
else:
sentence = word + "ing"
else:
sentence = word

return sentence

print(verbing('hail'))
print(verbing('swiming'))
print(verbing('do'))

# data[].attributes.body
def verbing(s):
if(len(s) >= 3):
if(s[-3:] == 'ing'):
return s+'ly'
return s+"ing"
return s

print(verbing('hail'))
print(verbing('swimming'))
print(verbing('do'))

# data[].attributes.body
def verbing(s):
if len(s)>=3:
if s[-3:]=="ing":
return s+"ly"
else:
return s+"ing"
return s

print(verbing("hail"))
print(verbing("swimming"))
print(verbing("do"))

# data[].attributes.body
def verbing(s):
"add 'ing' if string >=3,if 'ing', add 'ly' instead"
if len(s)>=3:
if "ing" in s[-3:]:
s = s + 'ly'
else:
s = s + 'ing'
return s

print(verbing('hail'))
print(verbing('swiming'))
print(verbing('do'))

# data[].attributes.body
def mix_up(m,n):
x = n[:2]+m[2:]+" "+m[:2]+n[2:]
return x

print(mix_up('mix','pod'))
print(mix_up('dog','dinner'))
print(mix_up('gnash','sport'))
print(mix_up('pezzy','firm'))

# data[].attributes.body
def mix_up(a,b):
print(b[:2]+a[2:], a[:2]+b[2:])

mix_up('mix','pod')
mix_up('dog','dinner')
mix_up('gnash','sport')
mix_up('pezzy','firm')

# data[].attributes.body
def mix_up(a, b):
first_two = a[0:2]
second_two = b[0:2]

return second_two+a[2:] + " " + first_two+b[2:]

print(mix_up('mix', 'pod'))
print(mix_up('dog', 'dinner'))
print(mix_up('gnash', 'sport'))
print(mix_up('pezzy', 'firm'))

# data[].attributes.body
def mix_up(a,b):
'mix up the first 2 characters for 2 string'
getAFirst2 = a[0:2]
getBFirst2 = b[0:2]

answer = getBFirst2 + a[2:] + " " + getAFirst2 + b[2:]
return answer

print(mix_up('mix', 'pod'))
print(mix_up('dog', 'dinner'))
print(mix_up('gnash', 'sport'))
print(mix_up('pezzy', 'firm'))

# data[].attributes.body
def mix_up(char1,char2):
"mix and and swap the first 2 chars of each string"
return char2[0:2] + char1[2:] + ' ' + char1[0:2] + char2[2:]

print(mix_up('mix', 'pod'))
print(mix_up('dog', 'dinner'))
print(mix_up('gnash', 'sport'))
print(mix_up('pezzy', 'firm'))

# data[].attributes.body
def fix_start(s):
first_letter = s[0]
return first_letter + s[1:].replace(first_letter, '*')

print(fix_start("babble"))
print(fix_start('aardvark'))
print(fix_start('google'))
print(fix_start('donut'))

# data[].attributes.body
def fix_start(s):
'replace * that is same char to the start of the word'
get_string = s[0:1]
replace_stirng = "*"
return s[0:1] + s[1:].replace(get_string, replace_stirng)

print(fix_start("babble"))
print(fix_start('aardvark'))
print(fix_start('google'))
print(fix_start('donut'))

# data[].attributes.body
def both_ends(s):

if len(s) <2:
message = ""
else:
message = s[0:2] + s[-2:]

return message

print(both_ends('spring'))
print(both_ends('Hello'))
print(both_ends('a'))
print(both_ends('xyz'))

# data[].attributes.body
def both_ends(s):
if len(s)<2:
print("")
else:
print(s[:2]+s[-2:])

both_ends("spring")
both_ends("Hello")
both_ends('a')
both_ends('xyz')

# data[].attributes.body
def both_ends(s):
if len(s) < 2:
return ''
return s[0:2] + s[-2:]
print(both_ends("spring"))
print(both_ends("Hello"))
print(both_ends("a"))
print(both_ends("xyyz"))

# data[].attributes.body
#Exercise 1: DONUT

donut = [4,9,10,99]

donut1 = [num for num in donut if num <10]
donut2 = ['many' for num in donut if num >=10]

all = donut1 + donut2

print(list(all))

# data[].attributes.body
Donut
def donuts(num):
if num < 10:
print("Number of donuts:",num)
else :
print("Number of donuts:many")

donuts(4)
donuts(9)
donuts(10)
donuts(99)

# data[].attributes.body
def donuts(x):
'To display the number of donuts'
message = "Number of donuts: "
if x >= 10:
message = message + "many"
else:
message = message + str(x)

return message

print(donuts(4))
print(donuts(9))
print(donuts(10))
print(donuts(99))

# data[].attributes.body
Exercise: donut
def dou(donuts):
"function: display num of donuts. if >5, display many, else display num of donuts"
if donuts <= 5:
print("Number of donuts: ", donuts)
else:
print("Number of donuts: many")

mixup
def mixup(s,s1):
firstw=s[:2]
secondw=s1[:2]
text=secondw + s[2:] + " " + firstw + s1[2:]
return text

verbing
def verbing(verb):
if (len(verb))>=3:
if 'ing' in verb:
return verb +'ly'
else:
return verb +'ing'
else:
return verb

# data[].attributes.body
Donuts
def donuts(x):
"return number of donuts, however if more than equal 10 will return many"
if x>=10:
print("Number of donuts: many")
else:
print("Number of donuts: ", x)

donuts(4)
donuts(9)
donuts(10)
donuts(99)

Both Ends
def both_ends(x):
"return first 2 and the last 2 chars,however return empty if less than 2 chars"
if len(x)<2:
return ''
else:
return x[:2] + x[-2:]

print(both_ends('spring'))
print(both_ends('Hello'))
print(both_ends('a'))
print(both_ends('xyz'))

Fix Start
def fix_start(s):
"get first char and change to * for next occurence but not the first char"
firstchar = s[0] #get first char
return s[0] + s[1:].replace(firstchar,"*") #join back first char

print(fix_start('babble'))
print(fix_start('aardvark'))
print(fix_start('google'))
print(fix_start('donut'))

# data[].attributes.body
def donuts(n):
if(n >= 10):
return "Number of donuts: many"
return "Number of donuts " + str(n)

print(donuts(5))
print(donuts(9))
print(donuts(10))
print(donuts(99))

# data[].attributes.body
countries = [['China','Beijing', 1386],
['India','Delhi',1339],
['United States','Washington DC', 325],
['Malaysia','Kuala Lumpur',31]]
beijing=(int(countries[0][2])/countries[3][2])
delhi=(int(countries[1][2])/countries[3][2])
dc=(int(countries[2][2])/countries[3][2])

print("China's population is %.2f times bigger than Malaysia" %beijing)
print("India's population is %.2f times bigger than Malaysia" %delhi)
print("United States' population is %.2f times bigger than Malaysia" %dc)

def sum_list(listt):
sum=0
for i in listt:
sum += i
return sum

listt=[1,2,3]
print(sum_list(listt))

COMPREHENSION
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b = [num**3 for num in a if num%2!=0]
print(b)

# data[].attributes.body
i=0
while i<=99:
if i%2==0:
print(i, end=" ")
i=i+1

# data[].attributes.body
def sum (a,b):
    "Function: returns results of summation of two integers."
    sum = a + b
    if sum >= 15 and sum <= 20:
        print("The sum of the the two integers is between 15 to 20.")
        return 20
    elif sum < 15:
        print("The sum of the two integers is below 15.")
    else:
        print("The sum of the two integers is above 20.") 

sum(9,9)

# data[].attributes.body
str1="Emma is a data scientist who knows Python. \
Emma works at google."
def emma():
print(str1.index(". E")+1)

emma()

# data[].attributes.body
def verbing(s):
if (len(s) >= 3 and s.endswith("ing")):
return s + "ly"
else:
return s + "ing"

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b=a

b=[num**3 for num in a if num != num % 2==1 ]

print(b)

#Exercise 3
for evnumber in range(0,99):
if(evnumber % 2 == 0):
print(evnumber)

#Excercise 2
str1="Emma is a data scientist who know Python. Emma works at google."
wstr1=str1.find("Emma")
wstr2=str1.find(". Emma")
print (wstr1)
print (wstr2)

#Exercise 1
def sum(a,b):
"sum: is to total up two integer given. However, if sum is between 15 to 20, it will return 20."
total=a+b
if total >= 15 and total <= 20:
return 20
else:
return total

# data[].attributes.body
num=[7, 8, 120, 25, 44, 20, 27]

remove_even=[n for n in num if n%2!=0]
print(list(remove_even))

# data[].attributes.body
print("Please key in 2 integers.")
int1=int(input("Integer1:"))
int2=int(input("Integer2:"))
sum=int1+int2
if sum>=15 and sum<=20:
sum=20
print("Sum is %d" % sum)

# data[].attributes.body
def result(a,b):

s = a + b
if (s >= 15) and (s <=20):
return 20
else:
return s

print(result(9,9))
print(result(20,2))
print(result(-2,50))
print(result(10,11))

--Exercise 3--
for i in range(0,99):
if i % 2 == 0:
print(i)

--Exercise3.1--
for i in range(3):
population = countries[i][2]/countries[3][2]

print(countries[i][0],"population is %.2f times bigger than Malaysia"%population)

----

def sum_list(lists):
sum = 0
for s in lists:
sum = sum + s
return sum

lists = [1,2,3,4,7,8,9,10]
print(sum_list(lists))

--Exercise 3.4--
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b = [odd**3 for odd in a if odd%2==1]
print(b)

# data[].attributes.body
def sum(a, b):
"Return the sum of two integers but if the sum is between 15 and 20 return 20"
total = a + b
if (total >= 15) and (total <= 20):
return 20
return total

print(sum(9, 9))
print(sum(20, -2))
print(sum(-2, 50))

Excercise 2
def last_substring(sentence):
return sentence.rfind('Emma')

print(last_substring("Emma is a datascientist who knows Python. Emma works at google."))

Excercise 3
for num in range(1, 99):
if num % 2 == 0:
print(num)

Excercise 3.1
for i in range(len(countries)-1):
malaysia = countries[-1]
print(countries[i][0] + " is " + str(countries[i][2] - malaysia[-1]))

Excercise 3.2
nums = [34, 53, 252, 62, 64, 13, 75, 75]

def sum(array):
total = 0
for num in array:
total += num
return total

print(sum(nums))

def find(num, numlist):
if num in numlist:
return "Found"
return "Not Found"

print(find(10, range(5)))

Remove even Numbers
num = [7, 8, 120, 25, 44, 20, 27]
def remove_even(x):
for n in x[:]:
if n % 2 == 0:
x.remove(n)
return x
print(remove_even(num))

List Comprehension
b = [n**3 for n in a if n % 2 != 0]

# data[].attributes.body
def s(num1,num2):
"function: add 2 numbers and display 20 if values between 15 - 20"
ans=num1+num2
if (ans >=15) and (ans <= 20):
print("20")
else:
print(ans)

Exercise 3
num=int(input("Enter number"))
for n in range(num):
if n % 2 ==0:
print(n)

remove even num
num = [7, 8, 120, 25, 44, 20, 27]
def remove_even(num):
for nl in num:
if nl%2 ==1:
print(nl)

list comprehension
# complete this line
b = [alpha**3 for alpha in a if alpha%2==1]
print(b)

# data[].attributes.body
str="Emma is data scientist who knows python. Emma whoks at google."

def last_position(Emma):
"return sum of two integer. however, if the sum between 15 and 20 it will return 20"
return str.find(" Emma")+1

print(last_position("Emma"))

Quick Exercise 3.1
countries = [['China','Beijing', 1386],
['India','Delhi',1339],
['United States','Washington DC', 325],
['Malaysia','Kuala Lumpur',31]]

for s in range(3):
num = countries[s][2]/countries[3][2]
print(countries[s][0],num)

def sum_up(list1):
"sum up in list"
sum = 0
for n in list1:
sum = sum +n
return sum

print(sum_up([1,2,3,4,5]))

num = [7, 8, 120, 25, 44, 20, 27]
def remove_even(x):
"Remove even number in list"
for i in num:
if i%2 != 0:
ans=i
return ans
print(remove_even(num))

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

b=[outp*outp*outp for outp in a if outp%2 ==1]
print(b)

# data[].attributes.body
def checksum(num1,num2):
"return sum of two integer. however, if the sum between 15 and 20 it will return 20"
total  = num1 + num2
if total>=15 and total<=20:
return 20
else:
return total

print(checksum(9,9))
print(checksum(20,-2))
print(checksum(-2,50))

exe 2
str1 = "Emma is a data scientist who knows Python. Emma works at google."
def lastposition(look):
"get last position of substring Emma"
return str1.find(look)+1

print(lastposition('.'))

exe 3
def showeven(start,end):
"print even number"
for i in range(start,end):
if i%2==0:
print(i)

showeven(0,99)

exe 3.1
# Nested Lists Example, let's use population (millions).
countries = [['China','Beijing', 1386],
['India','Delhi',1339],
['United States','Washington DC', 325],
['Malaysia','Kuala Lumpur',31]]

def print_size(country,country1):
print(countries[country][2])
print(countries[country1][2])

#print(countries[0][2])
print_size("China","Malaysia")

exe 3.2
def total_all(list):
sum = 0
for item in list:
sum = sum + item
return sum

all_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
print(total_all(all_list))

exe 3.3
def checkFound(num,list):
"check if number appears on a list"
result = "Not Found"
for s in list:
if s == num:
result = "Found !"
return result

checkFound(130,list(range(100)))

exe 3.4
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b = [num*num*num for num in a if num%2==1]

print(b)

# data[].attributes.body
def sum(x,y):
'Sum total of 2 numbers if the values is between 15 and 20 hence return 20 else return value'
total = x + y
if total >= 15 and total <= 20:
total = 20

return total

sum(9,9)
sum (20,-2)
sum (-2,-50)

exe 2
#find the index of emma
strpass = "Emma is a data scientist who knows Python. Emma works at google."

def FindName1 (str1):
'Find the second name'
endS =str1.find(".")
print (endS+1 )
FindName1(str2)

exe 3
i = 0
while i <= 99:
indc = i%2

    if indc == 0:

print(i)

i = i + 1

exe 3.1
countries = [['China','Beijing', 1386],
['India','Delhi',1339],
['United States','Washington DC', 325],
['Malaysia','Kuala Lumpur',31]]

mas = countries[3][2]

for count in countries :
if count[0] != "Malaysia":
ans = count[2]/mas
print(count[0] + " Population is bigger than Malaysia by %.2f" %ans )

exe 3.2
def sumList(list):
'Total up the values of the list'
Totalsum = 0
for element in list:
Totalsum = Totalsum + element

return Totalsum

list = [1, 4, 9, 16, 25,55,6,100,200,1000,23]
print(sumList(list))

3.3
def search (findVal, List1):
status =""
for a in List1:
if findVal == a:
status = "Found"
elif status !="Found":
status = "Not Found"

return status

print(search(10, list(range(100))))
print(search(250, list(range(100))))

Remove Even Numbers
num = [7, 8, 120, 25, 44, 20, 27]
def remove_even(x):
## write your code here ##
odd=[]
for a in x:
# print(a)
indc = a%2
if indc == 1 :
odd.append(a)
return odd

nl = remove_even(num)
print(nl)     # answer should be [7, 25, 27]