# This is a comment line.
for i in [1,2]:
for j in [1,2]:
print(i)
print(j)
print(i+j)
my_list = [1,
2,
3]
my_list_of_lists = [[1,2],
[3,4]]
print(my_list_of_lists)
# Example integer numbers
a = 2
b = 1
c = -1
# import the math module
import math
# Code to compute solution to the quadratic equation of the form a x^2 + b x + c = 0
sol1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)
sol2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a)
math.sqrt(16)
print([sol1,sol2])
type(sol1)
my_string = "Hello world"
my_string.replace("world", " Mahidol")
print(my_string)
second_string = 'Mahidol University'
print(second_string)
third_string = '''Department of Mathematics,
Faculty of Science,
Mahidol University'''
print(third_string)
my_string + " from " + second_string
# Quote from Albert Einstein
"\"Imagination is the highest form of research\". - Albert Einstein"
print(my_string[0])
print(my_string[0:5])
print(my_string[-5:])
for c in second_string[:5]:
print(c)
my_string = "Hello world"
x = 10 # x is of type int
x = "SCMA" # x is now of type str
print(x)
x = str(10)
type(x)
y = float(10)
type(y)
z = int(10.1)
z
my_list = [1, 2, 3]
third_list = list((1 , 2, 3))
print(third_list)
my_list == third_list
second_list = ["a", 2, "e", 4, "i", 6, "o", 8, "u"]
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
my_list.insert(8,3.5)
print(my_list)
my_list.remove(2.5)
print(my_list)
my_list.pop(0)
print(my_list)
my_list
#my_list.append('fong')
print(type(my_list.pop()))
type(my_list)
my_list = [0, 1, 2, 3, 4]
del my_list[0]
print(my_list)
my_list = [0, 1, 2, 3, 4]
del my_list
my_list = [0, 1, 2, 3, 4]
second_list = ["a", 2, "e", 4, "i", 6, "o", 8, "u"]
print(my_list + second_list)
second_list = ["a", 2, "e", 4, "i", 6, "o", 8, "u"]
second_list[0] = 1
second_list[2] = 3
second_list[4] = 5
second_list[6] = 7
second_list[8] = 9
print(second_list)
last_list = []
for x in range(1,4):
last_list += ["Math"]
print(last_list)
for x in range(1,4):
print(x)
old_list = ["apple", "banana", "orange"]
new_list = old_list
print(new_list)
old_list = ["apple", "banana", "orange"]
new_list = old_list.copy()
print(new_list)
old_list = ["apple", "banana", "orange"]
new_list = list(old_list)
print(new_list)
squared_evens = [x ** 2 for x in range(11) if x % 2 == 0]
print(squared_evens)
fongerhd=[x for x in range(-2,20,1) if x%2==1]
fongerhd
print(8 > 2)
print(8 == 2)
print(2 < 8)
a = 22/7
b = 3.14
print(True==b<a,type(b>a),type(False))
#b>a==false
#'''
if b>a:
print("b is greater than a")
else:
print("b is not greater than a")
#'''
a = 22
b = 7
c = 22/7
print(float(c))
22/7
22/7
m = roundfloat(22/7 , 5)
new_dict = {}
other_dict= dict()
new_dict = {
"brand": "Tesla",
"model": "Model X",
"year": 2021
}
print(new_dict)
print(new_dict['brand'])
new_dict.keys()
new_dict.values()
new_dict.items()
movies = ["Doctor Strage", "Venom", "Thor"]
ratings = [8.9, 7.8, 8.4]
movie_choice_index = movies.index("Venom")
print(ratings[movies.index('Thor')])
ratings = {
"Doctor Strange": 8.9,
"Venom": 7.8,
"Thor" : 8.4
}
print(ratings["Venom"])
ratings["Ant-Man"] = 8.3
print(ratings)
ratings['Doctor Strange']= 9.1
#print(ratings)
del ratings['Ant-Man']
ratings
#print(ratings)
ratings = {
"Doctor Strange": 8.9,
"Venom": 7.8,
"Thor" : 8.4
}
#print all key names in the dictionary
for x,g in ratings.items():
print(x,g)
#print all values in the dictionary
for x in ratings:
print(ratings[x])
#loop through both keys and values
for x, y in ratings.items():
print(x)
def name():
print("What’s your name?")
name = input()
return name
print(name())
# Define function with parameters
def product_info(productname, price):
print("Product Name: " + productname)
print("Price: " + str(price))
# Call function with parameters assigned as above
product_info('30000',5)
# Call function with keyword arguments
product_info(price=20000, productname= "Ipad Air 3")
a = 22/7
if a > math.pi:
print("a is greater than pi!", math.pi,22/7)
Score = 67
if Score >= 80:
print("Your grade is A")
elif Score >= 75:
print("Your grade is B+")
elif Score >= 70:
print("Your grade is B")
elif Score >= 65:
print("Your grade is C+")
elif Score >= 60:
print("Your grade is C")
else:
print("Your grade is D")
for x in "Mahidol":
print(x)
#print as long as x is less than 8
i=1
while i< 8:
print(i)
i += 1
#print if x is less than 8, but skip four
i=1
while i< 8:
print(i)
if i == 4:
break
i += 1
print(i)