CET313 - Introduction To AI
CET313 - Introduction To AI
[3]: # variables
a = 2
b = a * a
print (b)
name = "jo" #single or double quotes allowed
print (name)
4
jo
counter = counter * 2
print(counter)
print(name)
600
800.0
Htut
[7]: z = [1, 2, 3]
print(z)
z.extend([4, 5, 6])
print(z)
z.append(8)
print(z)
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 8]
1
[10]: # Lists can be modified
integer_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(integer_list[2]) # indices of a list indicated by []
23 in integer_list # Check if a value exists in a list
[10]: False
first_two = integer_list[:2]
print(first_two)
four_to_end = integer_list[4:]
print(four_to_end)
first_two = l[:2]
print(first_two)
four_to_end = l[4:]
print(four_to_end)
[1, 2]
[5, 6, 7, 8, 9]
[['a', 'b', 'c'], ['d', 'e', 'f']]
[]
2
[0, 2, 4]
[1, 3, 5]
(3, 4)
Jo's grade 80
Tim's grade 90
stock data 1: {'date': '1/1/16', 'symbol': 'FOX', 'price': 4.44}
stock data 2: {'date': '2/1/16', 'symbol': '2PP1', 'price': 24.44}
Counter({0: 2, 1: 1, 2: 1})
[58]: #sets
stopwords = set(["a", "an", "the", "yet", "you"]) # and many other words
print("you" in stopwords)
print("gong" in stopwords)
True
False
3
[62]: #functions
def mysquare(x):
return (x * x)
mn = 3
print(mysquare(mn))
9
15
[63]: # 1 Random
import random
random.seed(10)
print(random.random())
# 2 Regular expressions
import re
str = 'an example word: cat !!'
match = re.search(r'word:\w\w\w', str)
#If-statement after search() tests if it succeeded
if match:
print ('found'), match.group() ## 'found word: cat'
else:
print ('did not find')
0.5714025946899135
did not find
4
<re.Match object; span=(1, 4), match='iii'>
None
<zip object at 0x000001A6F5820780>
(1, 2, 3)
(4, 5, 6)
[63]: True
[64]: # classes
#!/usr/bin/python
class Employee: # name of class is Employee
'Common base class for all employees'
empCount = 0
def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ", self.salary)