Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539

Similar presentations


Presentation on theme: "CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539"— Presentation transcript:

1 CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

2 Review Textbook –Self-Review Exercises –Quick Quiz –Exercises Time –8am - 9:45am, May 14 (Thursday) Place –ENGR 1.290 (classroom) 2

3 Review Lecture slides –Basic concepts in Python –Control Structures –Functions –Lists, Tuples, and Dictionaries –Introduction to Object-Oriented Programming –Graphical User Interface Components –Python XML Processing –Database & SQL 3

4 Review Multiple Choices True/False Statements Programming –Debugging –Write the code Bonus Question 4

5 Chapter 1: Python Python is a scripting language –It is not compiled as an executable file Python –Structured programming Divide and conquer –Object-oriented programming Class and object –Data encapsulation –Inheritance –Polymorphism 5

6 Chapter 2: Basic Concepts in Python Programming Arithmetic operators and their precedence Commonly used Python rules/functions –print() 6

7 Arithmetic Operators Symbols –* # multiply –/ # divide –% # modulus –** # exponential –// # floor division Order –Operators are done in order of parenthesis, exponents, multiply and divide (left to right), and lastly add and subtract (left to right) 7

8 Chapters 3 & 4: Control Structures The syntax of basic sequence, selection, and repetition structures in Python –Selection if, if/else, if/elif/else –Repetition while, for 8

9 Syntax of Control Structure 9 total = total + Grade counter = counter + 1 if Grade>=60: print ("Passed") if Grade>=60: print ("Passed") else: print ("Failed")

10 10 for Repetition Structure The for loop –Function range is used to create a list of values –range ( integer ) »Values go from 0 to given integer –range ( integer1, integer2) »Values go from first up to second integer –range ( integer1, integer2, integer ) »Values go from first up to second integer, but increases in intervals of the third integer –The loop will execute as many times as the value passed –for counter in range ( value ): [0, integer-1] [integer1, integer2-1] 10

11 Chapter 5: Functions Modules and pre-defined functions –import moduleName –math math.floor () math.ceil () –random random.randrange() Syntax of user-defined functions 11

12 12 Module math Functions Module –Contains function definitions and other elements All of which are related in some way –Calling a function functionName ( argument1, argument2 ) –The import keyword is used to include a module –Invoking functions from a module Use the module name followed by the dot operator (.) moduleName.functionName( argument )

13 13 Random-Number Generation The random module –Used to generate a random number for the programmer –Function randrange Generates a number from the first argument up to, but not including, the second argument Each number in the range has the same likelihood of being selected by the function –random.randrange(a, b)

14 14 User-Defined Functions Definitions –Functions must be defined before they are used –def functionName ( paramList ): functionName is a valid identifier paramList is a comma separated list of parameters received The actions of the functions then follows –They should all be indented appropriately –The actions are also called the block or the function body

15 Chapter 6: Lists, Tuples, and Dictionaries Creation and manipulation of sequences –String –List –Tuple 15

16 Example of Sequences C[0]-45C[-12] C[1]6C[-11] C[2]0C[-10] C[3]72C[-9] C[4]34C[-8] C[5]39C[-7] C[6]98C[-6] C[7]-1345C[-5] C[8]939C[-4] C[9]10C[-3] C[10]40C[-2] C[11]33C[-1] Name sequence (C) Position number of the element within sequence C 16

17 Creating Sequences – List Lists –Use brackets –Separate multiple items with a comma list1 = [1, 2, 3, 4] –Empty list list2 = [] Length of the list –len (list1) list1[0]=0 # OK! 17

18 Creating Sequences – Tuple Tuples –Use parenthesis –Separate multiple items with a comma tuple1 = (1, 2, 3, 4) tuple1 = 1, 2, 3, 4 –Empty tuple tuple2 = () –Singleton (or one-element tuple) singleton3 = 1, Comma (,) after 1 identify the variable signleton3 as a tuple Error! –tuple1[0]=0 18

19 Chapter 7: Introduction to Object- Oriented Programming in Python Classes –Attributes Private attributes Class attributes –Methods Constructor: __init__ Get and Set methods Destructor: __del__ Objects –Creation of objects Inheritance 19

20 Class Declaration Defining a Class –Class header Keyword class begins definition –Followed by name of class and colon (:) –Body of class Indented block of code –Documentation string Describes the class Optional Appears immediately after class header 20

21 Class Declaration (cont'd) Defining a Class –Constructor method __init__ Executes each time an object is created Initialize attributes of class Returns None –Object reference (self) All methods must at least specify this one parameter Represents object of class from which a method is called Called self by convention 21

22 Get and Set Methods Access methods –Allow data of class to be read and written in controlled manner –Get and Set methods Allow clients to read and write the values of attributes respectively 22

23 Destructors –Method is named __del__ –Executed when object is destroyed No more references to object exist –Performs termination housekeeping before Python reclaims object memory Typically used to close network or database connections 23

24 Chapter 8: Graphical User Interface Components Components –Label –Entry –Button –Checkbutton & Radiobutton 24

25 Entry Component – TextBox Textbox –Areas in which users can enter text or programmers can display a line of text –Created by Entry class event occurs when user presses Enter key inside Entry component 25

26 Checkbutton and Radiobutton Components Checkbox –Small white square –Either blank or contains a checkmark –Descriptive text referred to as checkbox label –Any number of boxes selected at a time –Created by class Checkbutton Radio button –Mutually excusive options – only one radio button selected at a time –Created by class Radiobutton Both have on/off or True/False values and two states – selected and not selected (deselected) 26

27 Chapter 9: Python XML Processing XML documents –Tree structure –Tags –Elements –Data 27

28 Chapter 10: Database & SQL Relational database –Database schema –A set of relational tables SQL (Structured Query Language) –A database language –Syntax of SQL Use Python to submit SQL to databases 28

29 Example of Table: Employees 29

30 SQL A program might select data from the table to create a query result – E.g., to retrieve the location of each department, in increasing order by Department number – SQL: SELECT DISTINCT Department, Location FROM Employees ORDER BY Department 30

31 SQL Results 31

32 SQL on Books Database SELECT * FROM tableName – SELECT * FROM Authors – SELECT AuthorID, LastName FROM Authors SELECT columnName1, columnName2, … FROM tableName WHERE criteria – SELECT Title, EditionNumber, Copyright FROM Titles WHERE Copyright > '2014' 32

33 Example of Using pyodbc Package http://en.wikibooks.org/wiki/Python_Programming/Dat abase_Programming import pyodbc DBfile = '/data/MSAccess/Music_Library.mdb' conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile) #use below conn if using with Access 2007, 2010.accdb file #conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBfile) cursor = conn.cursor() SQL = 'SELECT Artist, AlbumName FROM RecordCollection ORDER BY Year;' for row in cursor.execute(SQL): # cursors are iterable print (row.Artist, row.AlbumName) # print row # if print row it will return tuple of all fields cursor.close() conn.close() 33

34 Good Luck! Q/A


Download ppt "CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539"

Similar presentations


Ads by Google