"ERROR: String Expected!": # The None Type
"ERROR: String Expected!": # The None Type
In [4]: 1 print(is_tired)
executed in 6ms, finished 08:23:39 2020-04-29
In [5]: 1 age = 35
2 print(age)
executed in 6ms, finished 08:23:39 2020-04-29
In [6]: 1 print(5+5)
2 print(10-5-5)
3 print(2*2)
executed in 7ms, finished 08:23:39 2020-04-29
In [7]: 1 # Careful when you divide integers!
2 # In Python 2.7, when you divide 2 integers, the result is a whole number.
3 # In Python 3.x, the result will be a float.
4
5 print(22/10)
2.2
0.5
0.5
0.5
He said:
"Let's do this"
Hello World
Tampines Street 21
In [12]: 1 age = 35
2 message = 'I am {} years old'.format(age)
3 print(message)
executed in 6ms, finished 08:23:39 2020-04-29
I am 35 years old
In [13]: 1 empty = []
2 colours = ['blue','red']
3 print(colours)
4 print(colours[0])
executed in 5ms, finished 08:23:39 2020-04-29
['blue', 'red']
blue
('blue', 'red')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-16-942432b7d3ec> in <module>
3
4 # This line will crash, cause an error
----> 5 colours.append('green')
In [44]: 1 person = {
2 'name':'Ali',
3 'age':18,
4 'address':{
5 'country':'Singapore',
6 'zip': '123456'
7 }
8 }
9
10 print(person)
11 print(person['name'])
12 print(person['address']['country'])
executed in 8ms, finished 08:24:10 2020-04-29
In [45]: 1 person = {
2 'name':'Ali'
3 }
4
5 person['age'] = 35
6
7
8 print(person)
executed in 7ms, finished 08:24:10 2020-04-29
In [46]: 1 print(person.keys())
executed in 5ms, finished 08:24:10 2020-04-29
dict_keys(['name', 'age'])
1 Functions
In [47]: 1 def squared(value):
2 return value * value
3
4 #anything that has not been idented to the right is not part of the function.
5 results = squared(2)
6
7 print(results)
4
In [48]: 1 def concat(string1, string2):
2 return string1 + ' ' + string2
3
4 result = concat('Hello', 'World')
5
6 print(result)
executed in 7ms, finished 08:24:10 2020-04-29
Hello World
John
8 Claymore Hill
Singapore 229572
Singapore
John
8 Claymore Hill
Singapore 229572
Singapore
2 Classes
In [51]: 1 class Animal(object): #it is convention to capitalize class names
2 legs=4
3 speed=0
4
5 def run(self):
6 self.speed=10
7
8 # for explanation of self, refer to:
9 # https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/
10
WOOF! WOOF!
Out[54]: 4
In [55]: 1 my_terrier.speed
2 #speed of dog before running
3 #note that speed was defined in the parent/super class named Animal
executed in 6ms, finished 08:24:10 2020-04-29
Out[55]: 0
In [56]: 1 my_terrier.run()
2 my_terrier.speed
3 #speed of dog after running
4 #note that the method run() was defined in the parent/super class named Animal
Out[56]: 10
1
2018
3 The if statement
In [58]: 1 age=35
2
3 if age>18: #the other comparisons are >=, <=, <, ==
4 print("You are older than 18.")
executed in 5ms, finished 08:24:10 2020-04-29
In [59]: 1 age=17
2
3 if age>18:
4 print("You are older than 18.")
5 else:
6 print('You are not older than 18.')
executed in 5ms, finished 08:24:10 2020-04-29
In [60]: 1 age=100
2
3 if age>18:
4 print('You are older than 18.')
5 elif age==100:
6 print('You are really old!')
7 else:
8 print('You are not older than 18.')
executed in 5ms, finished 08:24:10 2020-04-29
In [62]: 1 this=1
2 that=3
3 something=4
4
5 if (this==1 or that>2) and something<5:
6 print("WOW!")
executed in 8ms, finished 08:24:10 2020-04-29
WOW!
Bob
Eve
Steve
Tim
Tim
Bob
Eve
Eve
Steve
Tim
Bob
Eve
Eve
In [67]: 1 for person in people:
2 if person =='Eve':
3 continue
4 print(person)
executed in 7ms, finished 08:24:11 2020-04-29
Bob
Steve
Tim
0
1
2
3
4
5
6
7
8
9
is some text
In [70]: 1 print(text[:5])
executed in 5ms, finished 08:24:11 2020-04-29
This
In [71]: 1 print(text[:-5])
executed in 5ms, finished 08:24:11 2020-04-29
This is some
In [72]: 1 print(text[5:-5])
executed in 7ms, finished 08:24:11 2020-04-29
is some