modules:52553_python
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | modules:52553_python [2020/06/20 14:39] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | |<100% 25% - >| | ||
+ | ^ \\ DATA ANALYTICS REFERENCE DOCUMENT\\ \\ ^^ | ||
+ | ^ Document Title: | ||
+ | ^ Document No.: | ||
+ | ^ Author(s): | ||
+ | ^ Contributor(s): | ||
+ | |||
+ | |||
+ | |||
+ | **REVISION HISTORY** | ||
+ | |< 100% 10% - - 10% 17% 10% >| | ||
+ | ^ \\ Revision\\ | ||
+ | | [[: | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== Topic8 - Python I ====== | ||
+ | |||
+ | ===== Databases vs Program ===== | ||
+ | |||
+ | ^Employee ID^Name^Dept^Salary^ | ||
+ | ^100|John|HR|25500| | ||
+ | ^101|Mary|R& | ||
+ | ^102|Bill|R& | ||
+ | ^103|Tom|Sales|40000| | ||
+ | |||
+ | <code mysql> | ||
+ | |||
+ | SELECT * from employees WHERE Salary > 42000; | ||
+ | SELECT * from employees WHERE Salary < 30000; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Variables ===== | ||
+ | |||
+ | * Variables are names areas in the computer' | ||
+ | |||
+ | <code python variables.py> | ||
+ | my1stVariable = "Hello World" | ||
+ | my2ndVariable = 1 | ||
+ | |||
+ | print(my1stVariable) | ||
+ | #Hello World | ||
+ | |||
+ | my2ndVariable + 4 | ||
+ | |||
+ | print(my2ndVariable) | ||
+ | # 1 | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | * Variables are named areas in the computer' | ||
+ | |||
+ | <code python variables2.py> | ||
+ | |||
+ | my2ndVariable = 1 | ||
+ | x = my2ndVariable + 4 | ||
+ | |||
+ | |||
+ | print(my2ndVariable) | ||
+ | # 1 | ||
+ | print(x) | ||
+ | # 5 | ||
+ | |||
+ | age = 21 | ||
+ | age = age + 1 | ||
+ | |||
+ | print(age) # 22 | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== IF Statements ===== | ||
+ | |||
+ | <code python ifstatements.py> | ||
+ | |||
+ | age = 17 | ||
+ | |||
+ | if(age > 17): | ||
+ | | ||
+ | |||
+ | print(finished) | ||
+ | # finsied | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | <code python ifstatements1.py> | ||
+ | |||
+ | age = 17 | ||
+ | |||
+ | if(age > 17): | ||
+ | | ||
+ | elif(age < 18): | ||
+ | | ||
+ | print(finished) | ||
+ | #Nok | ||
+ | # finished | ||
+ | </ | ||
+ | |||
+ | <code python ifstatements2.py> | ||
+ | |||
+ | temp = 37 | ||
+ | |||
+ | if(temp >37): | ||
+ | | ||
+ | elif(temp <37): | ||
+ | | ||
+ | else: | ||
+ | | ||
+ | print(" | ||
+ | # ok | ||
+ | # finished | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== input ===== | ||
+ | |||
+ | <code python input.py> | ||
+ | |||
+ | name = input(" | ||
+ | email = name + " | ||
+ | print(email) | ||
+ | # Tom@gmit.ie | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code python salary.py> | ||
+ | |||
+ | salary = input(" | ||
+ | salary = int(salary) | ||
+ | salary = salary + 100 | ||
+ | |||
+ | print(salary) | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== WHILE statement ===== | ||
+ | |||
+ | <code python while.py> | ||
+ | |||
+ | i = 1 | ||
+ | while(i <=5): | ||
+ | | ||
+ | i+=1 | ||
+ | # i = i +1 | ||
+ | # 1 | ||
+ | # 2 | ||
+ | # 3 | ||
+ | # 4 | ||
+ | # 5 | ||
+ | </ | ||
+ | |||
+ | <code python whilebreak.py> | ||
+ | |||
+ | answer = " | ||
+ | |||
+ | while True: | ||
+ | guess = input(" | ||
+ | | ||
+ | | ||
+ | break | ||
+ | |||
+ | print(" | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== Arrays ===== | ||
+ | |||
+ | <code python array.py> | ||
+ | myArr = [" | ||
+ | print(myArr) | ||
+ | # | ||
+ | |||
+ | print(myArr[0]) | ||
+ | # jan | ||
+ | |||
+ | print(len(myArr)) | ||
+ | #4 | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Append() ===== | ||
+ | |||
+ | <code python append.py> | ||
+ | myArr = [" | ||
+ | myArr.append(" | ||
+ | |||
+ | print(myArr) | ||
+ | ## | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== FOR Statement ===== | ||
+ | |||
+ | <code python forloop.py> | ||
+ | name = [" | ||
+ | |||
+ | for name in names: | ||
+ | | ||
+ | |||
+ | # Tom@gmit.ie | ||
+ | # John@gmit.ie | ||
+ | # Mary@gmit.ie | ||
+ | # Bob@gmit.ie | ||
+ | |||
+ | |||
+ | myArr = [1, 5, 12] | ||
+ | |||
+ | for x in myArr: | ||
+ | | ||
+ | # 2 | ||
+ | # 6 | ||
+ | # 13 | ||
+ | |||
+ | print(myArr) | ||
+ | #[1, 5, 12] | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== User-defined functions ===== | ||
+ | |||
+ | <code python userfunctions.py> | ||
+ | def printMonths(): | ||
+ | | ||
+ | |||
+ | def printDays(): | ||
+ | | ||
+ | |||
+ | printDays() | ||
+ | # Mon, Tue, Wed | ||
+ | |||
+ | printMonths() | ||
+ | # Jan, Feb, Mar | ||
+ | </ | ||
+ | |||
+ | ===== name ===== | ||
+ | |||
+ | <code python userfunctions.py> | ||
+ | def printMonths(): | ||
+ | | ||
+ | |||
+ | def main(): | ||
+ | | ||
+ | |||
+ | if __name__ ==" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== Parameters ===== | ||
+ | |||
+ | <code python parameters.py> | ||
+ | |||
+ | print(" | ||
+ | # Hello World | ||
+ | |||
+ | print(" | ||
+ | # Test | ||
+ | |||
+ | |||
+ | s = "This is a string" | ||
+ | |||
+ | print(len(s)) | ||
+ | # 16 | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | <code python parameters1.py> | ||
+ | |||
+ | def checkAge(age): | ||
+ | if age < 18: | ||
+ | return "Too Young" | ||
+ | | ||
+ | |||
+ | def main(): | ||
+ | name = input(" | ||
+ | age = int(input(" | ||
+ | | ||
+ | |||
+ | if __name__=" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | ===== Local Variables ===== | ||
+ | A local variable is a variable that is given local scope. Local variable references in the function or block in which it is declared override the same variable name in the larger scope. | ||
+ | |||
+ | <code python localvariables.py> | ||
+ | |||
+ | def checkAge(age): | ||
+ | limit = 18 | ||
+ | if age < limit: | ||
+ | return "Too Young" | ||
+ | | ||
+ | |||
+ | def main(): | ||
+ | name = input(" | ||
+ | age = int(input(" | ||
+ | | ||
+ | |||
+ | if __name__=" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code python localvariables1.py> | ||
+ | |||
+ | def checkAge(age): | ||
+ | limit = 18 | ||
+ | if age < limit: | ||
+ | return "Too Young" | ||
+ | | ||
+ | |||
+ | def main(): | ||
+ | | ||
+ | name = input(" | ||
+ | age = int(input(" | ||
+ | | ||
+ | |||
+ | if __name__=" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Global Variables ===== | ||
+ | |||
+ | |||
+ | <code python globalvariables.py> | ||
+ | |||
+ | def incrementAge(age): | ||
+ | age += 1 | ||
+ | | ||
+ | # 25 | ||
+ | |||
+ | def main(): | ||
+ | age = 24 | ||
+ | | ||
+ | | ||
+ | # 24 | ||
+ | |||
+ | if __name__=" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | Declaring the variable outside of the function and using the keword global to make it a global variable | ||
+ | |||
+ | <code python globalvariables.py> | ||
+ | age = 24 | ||
+ | |||
+ | def incrementAge(age): | ||
+ | # access using the keyword " | ||
+ | | ||
+ | age += 1 | ||
+ | | ||
+ | # 25 | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | | ||
+ | | ||
+ | #25 | ||
+ | |||
+ | |||
+ | if __name__=" | ||
+ | # execute only if run as a script | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Topic9 - Python II ====== | ||
+ | |||
+ | ===== PyMySQL ===== | ||
+ | |||
+ | * MySQLdb | ||
+ | * mysql.connector | ||
+ | * PyMySQL | ||
+ | |||
+ | ==== connect() ==== | ||
+ | |||
+ | * The connect() function connects to a MySQL database. | ||
+ | * **host** - host where the database server is located | ||
+ | * **user** - username to log in as | ||
+ | * **password** - Password to use | ||
+ | * **db** - Database to use | ||
+ | * **port** - Port to use | ||
+ | * **cursorclass**- Custom cursor class to use | ||
+ | |||
+ | ==== Connecting to the Database ==== | ||
+ | <code python> | ||
+ | conn = pymysql.connect( " | ||
+ | | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | conn = pymysql.connect( " | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== Executing a query ==== | ||
+ | |||
+ | <code python> | ||
+ | query = " | ||
+ | |||
+ | with conn: | ||
+ | | ||
+ | | ||
+ | | ||
+ | for s in sujects: | ||
+ | print(s[" | ||
+ | | ||
+ | </ | ||
+ | |||
+ | |||
+ | {{: | ||
+ | <code python query.py> | ||
+ | query = " | ||
+ | WHERE teacher LIKE %s" | ||
+ | |||
+ | with conn: | ||
+ | | ||
+ | | ||
+ | | ||
+ | for s in subjects: | ||
+ | print(s[" | ||
+ | </ | ||
+ | |||
+ | ==== Inserting new data ==== | ||
+ | <code python insertquery.py> | ||
+ | ins = " | ||
+ | | ||
+ | | ||
+ | |||
+ | with conn: | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | try and except block for error messages | ||
+ | <code python insertquery2.py> | ||
+ | ins = " | ||
+ | | ||
+ | | ||
+ | |||
+ | with conn: | ||
+ | try: | ||
+ | cursor = conn.cursor() | ||
+ | cursor.execute(ins, | ||
+ | conn.commit() | ||
+ | print(" | ||
+ | | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ==== Exceptions ==== | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | name = " | ||
+ | teacher = " | ||
+ | lc =1 | ||
+ | with conn: | ||
+ | try: | ||
+ | cursor = conn.cursor() | ||
+ | cursor.execute(query, | ||
+ | conn.commit() | ||
+ | print(" | ||
+ | except pymysql.err.InternalError as e: | ||
+ | print(" | ||
+ | except pymysql.err.IntegrityError as e: | ||
+ | print(" | ||
+ | except Exception as e: | ||
+ | print(" | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Deleting Data ==== | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | query = " | ||
+ | name = " | ||
+ | |||
+ | with conn: | ||
+ | try: | ||
+ | cursor = conn.cursor() | ||
+ | rowAffected = cursor.execute(query, | ||
+ | conn.commit() | ||
+ | if(rowsAffected ==0): | ||
+ | | ||
+ | else: | ||
+ | | ||
+ | except Exception as e: | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ==== Updating Data ==== | ||
+ | |||
+ | |||
+ | <code python> | ||
+ | |||
+ | query = " | ||
+ | subject = " | ||
+ | newTeacher = " | ||
+ | |||
+ | with conn: | ||
+ | try: | ||
+ | cursor = conn.cursor() | ||
+ | rowsAffected = cursor.execute(query, | ||
+ | conn.commit() | ||
+ | if(rowsAffected ==0): | ||
+ | | ||
+ | else: | ||
+ | | ||
+ | | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ==== Installing PyMySQL ==== | ||
+ | |||
+ | * open command prompt or terminal | ||
+ | * type conda install pymysql | ||
+ | * create a new py file | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | |||
+ | ====== Topic10 - Python III ====== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== pymongo ===== | ||
+ | * client = pymongo.MongoClient() | ||
+ | * client = pymongo.MongoClient(host=" | ||
+ | * try | ||
+ | * client.admin.command(' | ||
+ | ===== Database and Collections ===== | ||
+ | * mydb = myclient[" | ||
+ | * cols = mydb.list_collection_names() | ||
+ | * docs = mydb[" | ||
+ | ===== find() ===== | ||
+ | * people = docs.find({" | ||
+ | * for person in people: | ||
+ | *print(person[" | ||
+ | ===== find() ===== | ||
+ | * people = docs.find({" | ||
+ | * people = docs.find({" | ||
+ | ===== insert_one() ===== | ||
+ | * newDoc = {" | ||
+ | * mycol.insert_one(doc) | ||
+ | ===== insert_many() ===== | ||
+ | * newDocs = [{" | ||
+ | *{" | ||
+ | *{" | ||
+ | * mycol.insert_many(newDocs) | ||
+ | ===== Exceptions ===== | ||
+ | * pymongo.errors.ConnectionFailure | ||
+ | * pymongo.errors.DuplicateKeyError | ||
+ | * newDocs = [{" | ||
+ | * {" | ||
+ | * {" | ||
+ | * mycol.insert_many(newDocs) mycol.insert_many(newDocs, | ||
+ | ===== delete_one() ===== | ||
+ | * filter = {" | ||
+ | * mycol.delete_one(filter) | ||
+ | * mycol.delete_one({" | ||
+ | ===== delete_many() ===== | ||
+ | * filter = {" | ||
+ | * result = mycol.delete_many(filter) | ||
+ | * __DeleteResult__ | ||
+ | * print(result.deleted_count) | ||
+ | ===== update_one() ===== | ||
+ | * filter = {" | ||
+ | * update = {" | ||
+ | * mycol.update_one(filter, | ||
+ | ===== update_many() ===== | ||
+ | * filter = {" | ||
+ | * update = {" | ||
+ | * result = mycol.update_many(filter, | ||
+ | * __UpdateResult__ | ||
+ | * print(result.modified_count) | ||
+ | ===== Review ===== | ||
+ | * MySQL | ||
+ | * MongoDB | ||
+ | * Python | ||
modules/52553_python.txt · Last modified: 2020/06/20 14:39 by 127.0.0.1