0% found this document useful (0 votes)
18 views5 pages

CET313 - Introduction To AI

Week-1: Testing Exercises

Uploaded by

9teenMine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

CET313 - Introduction To AI

Week-1: Testing Exercises

Uploaded by

9teenMine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Testing_Exercises

June 16, 2024

[3]: # variables
a = 2
b = a * a
print (b)
name = "jo" #single or double quotes allowed
print (name)

4
jo

[6]: counter = 300 # An integer assignment


miles = 2000.0 # A floating point assignment
name = "Htut" # A string assignment

counter = counter * 2
print(counter)

travel = miles / 2.5


print(travel)

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

[22]: l=[] # example of an array structure


l.append(['a', 'b', 'c'])
l.append(['d','e', 'f']) # results in [['a', 'b', 'c'], ['d', 'e', 'f']]

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']]
[]

[32]: # all lists have a sort method


x = [4, 2, 3, 1, 5]
print("Sorted by sorted(x) and print:", sorted(x)) # [1, 2, 3, 4, 5]
print("Unsorted x:", x) # [4, 2, 3, 1, 5]
y = sorted(x)
print("Sorted x inside y", y) # [1, 2, 3, 4, 5]
print("Unsorted x", x) # [4, 2, 3, 1, 5]
x.sort() # sort() is the method for sorting a list in place
print("Sorted using x.sort()", x) # [1, 2, 3, 4, 5]

Sorted by sorted(x) and print: [1, 2, 3, 4, 5]


Unsorted x: [4, 2, 3, 1, 5]
Sorted x inside y [1, 2, 3, 4, 5]
Unsorted x [4, 2, 3, 1, 5]
Sorted using x.sort() [1, 2, 3, 4, 5]

[35]: #transforming Lists


x = [1, 2, 3, 4, 5]
evens = [x for x in range(6) if x % 2 == 0]
odds = [x for x in range(6) if x % 2 != 0]
print(evens)
print(odds) # [1, 3, 5]

2
[0, 2, 4]
[1, 3, 5]

[41]: #tuples - Like lists but cant be changed


mytuple1 = (1, 2)
mytuple2 = 3, 4
print(mytuple2)

(3, 4)

[53]: # Dictionaries associate things like a DB


emptydict = {}
emptydict2 = {}

grades = {"jo": 80, "tim": 90}

print("Jo's grade", grades["jo"]) # Output: 80


print("Tim's grade", grades["tim"])
# Create a dictionary to store stock data
stock_data = {}

# Add key-value pairs to the dictionary


stock_data[1] = {'date': '1/1/16', 'symbol': 'FOX', 'price': 4.44}
stock_data[2] = {'date': '2/1/16', 'symbol': '2PP1', 'price': 24.44}

# Print the data for stock 1


print("stock data 1:", stock_data[1])
print("stock data 2:", stock_data[2])

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}

[56]: #Useful for histograms


from collections import Counter
c = Counter([0,1,2,0])
print(c)

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))

# one-off 'lambda' functions


add_five = lambda number: number + 5
print(add_five(number=10))

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')

## Search for pattern 'iii' in string 'piiig'.


## ALL of the pattern must match, but it may appear anywhere.
## On success, match.group() is matched text.
match = re.search(r'iii', 'piiig') #=> found, match.group() == "iii"
print(match)
match = re.search(r'igs', 'piiig') #-> not found, match == None
print(match)

#3 zip-ing data together. Python works left to right


x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
print (zipped) # [(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
print (x2)
print (y2)
x == list(x2) and y == list(y2)

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 __init__(self, name, salary): # class constructor


self.name = name
self.salary = salary
Employee.empCount += 1

def displayCount(self): # further methods are defined like functions


print ("Total Employee %d" % Employee.empCount)

def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ", self.salary)

"This would create first object of Employee class"


emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee %d" % Employee.empCount)

#\n newline; \r carriage return; \t tab;

Name : Zara , Salary: 2000


Name : Manni , Salary: 5000
Total Employee 2

You might also like