CSCICMPE 4341 Topic Programming in Python Review Exam

  • Slides: 30
Download presentation
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of

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

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

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

Review • Multiple Choice • True/False Statements • Programming – Find bugs – Write

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

Chapter 1: Introduction to Python • • • Basic components in a computer system

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

Python • Python is a scripting language – It is not compiled as an

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

Chapter 2: Basic Concepts in Python Programming • Commonly used Python rules/functions – –

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

First Program in Python: Printing a Line of Text • Python – The #

First Program in Python: Printing a Line of Text • Python – The # symbol "//" or "/* … */" is used in C/C++/Java • Used to denote a single line comment – The print function • • "printf" or "cout" is used in C/C++ "System. out. println(. . . )" in Java • 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 file. Name. py – Executing code • Type python in the command line • Runs the python interpreter 7

Examples of Escape Characters 8

Examples of Escape Characters 8

Arithmetic Operators • Symbols – – – * / % ** // # multiply

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

Precedence of Arithmetic Operators 10

Precedence of Arithmetic Operators 10

# Fig. 2. 19: fig 02_19. py # String formatting. integer. Value = 4237

# Fig. 2. 19: fig 02_19. py # String formatting. integer. Value = 4237 print ("Integer ", integer. Value) print ("Decimal integer %d" % integer. Value) print ("Hexadecimal integer %xn" % integer. Value) Outline Fig 02_19. py float. Value = 123456. 789 print ("Float", float. Value) print ("Default float %f" % float. Value ) print ("Default exponential %en" % float. Value ) print ("Right justify integer (%8 d)" % integer. Value) print ("Left justify integer (%-8 d)n" % integer. Value ) string. Value = "String formatting" print ("Force eight digits in integer %. 8 d" % integer. Value ) print ("Five digits after decimal in float %. 5 f" % float. Value ) print ("Fifteen and five characters allowed in string: " ) print ("(%. 15 s) (%. 5 s)" % ( string. Value, string. Value ) ) 2002 Prentice Hall. All rights reserved.

Chapters 3 & 4: Control Structures • The syntax of basic sequence, selection, and

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

Control Structure • 3 control structures – Sequential structure • Built into Python –

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

Syntax of Control Structure total = total + Grade counter = counter + 1

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

Syntax of Control Structure (cont'd) if Grade>=90: print ("A") else: if Grade>=80: print ("B")

Syntax of Control Structure (cont'd) 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") 15

Syntax of Control Structure (cont'd) Product = 1 while Product < 1000: Product =

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

for Repetition Structure 17 • The for loop – Function range is used to

for Repetition Structure 17 • The for loop – Function range is used to create a list of values [0, integer-1] – range ( integer ) » Values go from 0 to given integer [integer 1, integer 2 -1] – range ( integer 1, integer 2) » Values go from first up to second integer [integer 1, integer 2 -1] – range ( integer 1, integer 2, 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 ): 17

break and continue Statements • The break statement – Used to make a loop

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

Logical Operators • Operators – and • Evaluates to true if both expressions are

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

and Logical Operator 20

and Logical Operator 20

or Logical Operator 21

or Logical Operator 21

not Logical Operator 22

not Logical Operator 22

Chapter 5: Functions • Modules and pre-defined functions – import module. Name – math

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

Module math Functions • Module – Contains function definitions and other elements • All

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

Random-Number Generation • The random module – Used to generate a random number for

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 25

Examples of Floor and Ceiling • floor function: – – math. floor(2. 10) =

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

User-Defined Functions • Definitions – Functions must be defined before they are used –

User-Defined Functions • Definitions – Functions must be defined before they are used – def function. Name ( param. List ): • function. Name is a valid identifier • param. List 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 27

Recursion • Method that calls itself • A recursive method solves only a simple

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

Default Arguments • Function arguments – Functions may commonly receive a particular value type

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 29

Good Luck! Q/A

Good Luck! Q/A