Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Review Chapters 1 ~ 4 in your textbook Lecture slides In-class exercises 2

3 Review Multiple Choice True/False Statements Programming –Find bugs –Write the code Bonus Question (20 extra points) 3

4 Chapter 1: Introduction to Python Basic components in a computer system The evolution of programming language Structured programming Object-oriented programming Can you list several differences between C++ and Python? 4

5 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 Commonly used Python rules/functions –# –print() –input() –int() –id() –type() Escape characters Arithmetic operators and their precedence String formatting 6

7 First Program in Python: Printing a Line of Text Python –The # symbol Used to denote a single line comment –The print function Used to send a stream of text to be output to the user Executing –Saving as a file Type code into a.py file and save it To run it type python fileName.py –Executing code Type python in the command line Runs the python interpreter "//" or "/* … */" is used in C/C++/Java "printf" or "cout" is used in C/C++ "System.out.println(...)" in Java 7

8 Examples of Escape Characters 8

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

10 Precedence of Arithmetic Operators 10

11  2002 Prentice Hall. All rights reserved. Outline Fig02_19.py # Fig. 2.19: fig02_19.py # String formatting. integerValue = 4237 print ("Integer ", integerValue) print ("Decimal integer %d" % integerValue) print ("Hexadecimal integer %x\n" % integerValue) floatValue = 123456.789 print ("Float", floatValue) print ("Default float %f" % floatValue ) print ("Default exponential %e\n" % floatValue ) print ("Right justify integer (%8d)" % integerValue) print ("Left justify integer (%-8d)\n" % integerValue ) stringValue = "String formatting" print ("Force eight digits in integer %.8d" % integerValue ) print ("Five digits after decimal in float %.5f" % floatValue ) print ("Fifteen and five characters allowed in string:" ) print ("(%.15s) (%.5s)" % ( stringValue, stringValue ) )

12 Chapters 3 & 4: Control Structures The syntax of basic sequence, selection, and repetition structures in Python Selection –if, if/else, if/elif/else –Empty statement: pass Counter-controlled and sentinel-controlled repetition –while, for –break, continue Augmented assignment Logical operators 12

13 Control Structure 3 control structures –Sequential structure Built into Python –Selection structure The if statement The if/else statement The if/elif/else statement –Repetition structure The while repetition structure The for repetition structure 13

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

15 Syntax of Control Structure (cont'd) 15 if Grade>=90: print ("A") else: if Grade>=80: print ("B") else: if Grade >=70: print ("C") else: if Grade>=60: print ("D") else: print ("F") if Grade>=90: print ("A") elif Grade>=80: print ("B") elif Grade >=70: print ("C") elif Grade>=60: print ("D") else: print ("F")

16 Syntax of Control Structure (cont'd) 16 Product = 1 while Product < 1000: Product = 2* Product for Product in range(1, 1000): Product = 2* Product

17 17 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] 17

18 break and continue Statements The break statement –Used to make a loop stop looping –The loop is exited and no more loop code is executed The continue statement –Used to continue the looping process –All following actions in the loop are not executed But the loop will continue to run 18

19 Logical Operators Operators –and Evaluates to true if both expressions are true –or Evaluates to true if at least one expression is true –not Returns true if the expression is false Not required in any program 19

20 and Logical Operator 20

21 or Logical Operator 21

22 not Logical Operator 22

23 Chapter 5: Functions Modules and pre-defined functions –import moduleName –math math.floor () math.ceil () math.cos () math.pow () … –random random.randrange() Syntax of user-defined functions Recursive function Default arguments 23

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

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

26 Examples of Floor and Ceiling floor function: – math.floor(2.10) = 2 – math.floor(2.00) = 2 – math.floor(1.90) = 1 – math.floor(1.80) = 1 ceil function: – math.ceil(0.00) = 0 – math.ceil(0.10) = 1 – math.ceil(0.20) = 1 – math.ceil(0.30) = 1 26

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

28 Recursion Method that calls itself A recursive method solves only a simple problem (base case) For any thing other than the base case, it calls itself with a slightly simpler problem –Eventually it becomes the base case for which it knows the answer 28

29 29 Default Arguments Function arguments –Functions may commonly receive a particular value type –When this is true a default argument can be set Must appear to the right of any other arguments –A default value can also be set If passes a value then the default value is overridden

30 Good Luck! Q/A


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

Similar presentations


Ads by Google