In [0]:
apple = 5
orange = 6
total = apple + orange
print(total)
In [0]:
mugs = 12
plates = 5
tea_cup = 8
total = 3 * mugs + 2 * plates + tea_cup
print(total)
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")
In [0]:
result = 1/3
print (result)
In [0]:
name = "Nur Anis"
name
Out[0]:
Will be printed out as string
In [0]:
name = "Ryan"
food = 'cheese'
age = "19"
print (name)
print (food)
age
Out[0]:
In [0]:
def age():
age = 20
return age
print(age())
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")
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!")
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())
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))
remember multiply is * not 'x'
In [0]:
names = [ "Andy", "Aaron", "Jacky", "Leon" ]
names
Out[0]:
In [0]:
data = [1, "banana", 2, "apples", 3, "oranges", 4.5, "kiwis"]
for item in data:
print(item)
print(data[3])
position starts from 0,1,2,3
In [0]:
employee = {"name": "Jacky", "age":30}
employee['state'] = "Selangor"
age = employee['age']
print(employee)
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
In [0]:
for i in range(10):
if i<5:
print(i)
In [0]:
output = [str(value) for value in range(10) if value > 5]
output
Out[0]:
In [93]:
s = 'the quick brown fox jumps over the lazy dog'
tokens = s.split(' ')
print(tokens)
In [94]:
numbers = [1,2,3,4,5,6]
squared = [n * n for n in numbers if n > 3]
print (squared)
looping and condition can be combined
In [97]:
names = ['anis', 'azmi']
print (names)
upper_names = [n.upper() for n in names]
print(upper_names)
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
Out[99]:
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)
Out[102]: