In [0]:
apple = 5
orange = 6
total = apple + orange
print(total)


11

In [0]:
mugs = 12
plates = 5
tea_cup = 8
total = 3 * mugs + 2 * plates + tea_cup
print(total)


54

In [0]:
age = 32
name = 'Jacky'
married = True
height = 1.75

In [0]:
# Hi I am just a line of comment
# print('Ignore me....')
print("You can only see me")


You can only see me

In [0]:
result = 1/3
print (result)


0.3333333333333333

In [0]:
name = "Nur Anis"
name


Out[0]:
'Nur Anis'

Will be printed out as string


In [0]:
name = "Ryan"
food = 'cheese'
age = "19"
print (name)
print (food)
age


Ryan
cheese
Out[0]:
'19'

In [0]:
def age():
  age = 20
  return age
print(age())


20

python syntax without curly brackets or BEGIN/END


In [0]:
def age_by_year(year_of_birth):
  return 2019-year_of_birth

print("your age is", age_by_year(1993), "years old")


your age is 26 years old

In [0]:
age = input('How old are you? ')
if int(age) > 18:
  print("you are allowed to enter")
else:
  print("sorry, please go home now!")


How old are you? 25
you are allowed to enter

In [0]:
Temp = float (input('Enter celcius temperature:'))
#Temp
def Conversion():
  Conversion = (Temp * (9/5)) + 32
  return Conversion
print("your Celsius temperature in Fahrenheit is", Conversion())


Enter celcius temperature:32
your Celsius temperature in Fahrenheit is 89.6

Need to define first, then return the value to get the output.


In [0]:
temperature = float(input('Enter temperature:'))
unit = input('What is the unit?')

def convert(temperature, unit):
  if unit == "c":
    return (temperature * (9/5)) + 32  
  if unit == "f":
    return (temperature - 32) * 5/9
  
print(convert(temperature, unit))


Enter temperature:89.6
What is the unit?f
32.0

remember multiply is * not 'x'


In [0]:
names = [ "Andy", "Aaron", "Jacky", "Leon" ]
names


Out[0]:
['Andy', 'Aaron', 'Jacky', 'Leon']

In [0]:
data = [1, "banana", 2, "apples", 3, "oranges", 4.5, "kiwis"]
for item in data:
  print(item)
print(data[3])


1
banana
2
apples
3
oranges
4.5
kiwis
apples

position starts from 0,1,2,3


In [0]:
employee = {"name": "Jacky", "age":30}
employee['state'] = "Selangor"
age = employee['age']
print(employee)


{'name': 'Jacky', 'age': 30, 'state': 'Selangor'}

In [0]:
lastName="Smithsonian"
count=0
for letter in lastName:
  print(letter," ",count)
  count+=1
print("-------")

count=0

while (count<5):
  print(lastName[count]," ",count)
  count+=1


S   0
m   1
i   2
t   3
h   4
s   5
o   6
n   7
i   8
a   9
n   10
-------
S   0
m   1
i   2
t   3
h   4

In [0]:
for i in range(10):
  if i<5:
    print(i)


0
1
2
3
4

In [0]:
output = [str(value) for value in range(10) if value > 5]
output


Out[0]:
['6', '7', '8', '9']

In [93]:
s = 'the quick brown fox jumps over the lazy dog'
tokens = s.split(' ')
print(tokens)


['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

In [94]:
numbers = [1,2,3,4,5,6]
squared = [n * n for n in numbers if n > 3]
print (squared)


[16, 25, 36]

looping and condition can be combined


In [97]:
names = ['anis', 'azmi']
print (names)

upper_names = [n.upper() for n in names]
print(upper_names)


['anis', 'azmi']
['ANIS', 'AZMI']

In [99]:
s = 'the quick brown fox jumps over the lazy dog'
tokens = s.split(' ')
print(tokens)

char3 = [t for t in tokens if len(t) > 3]
char3


['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
Out[99]:
['quick', 'brown', 'jumps', 'over', 'lazy']

In [102]:
s = 'the quick brown fox jumps over the lazy dog'
tokens = s.split(' ')
print(tokens)

vowel = [t for t in tokens if t[0] in 'aeiou']
vowel

#sorted(tokens)


['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
Out[102]:
['over']