Programming Principles in Python (CSCI 503)
Programming Principles in Python (CSCI 503)
Functions
Dr. David Koop (some slides adapted from Dr. Reva Freedman)
D. Koop, CSCI 503, Spring 2021
if, else, elif, pass
? if a < 10: print("Small")
else: if a < 100: print("Medium") else: if a < 1000: print("Large") else: print("X-Large")
? if a < 10: print("Small")
elif a < 100: print("Medium")
elif a < 1000: print("Large")
else: print("X-Large")
? Indentation is critical so else-if branches can become unwieldy (elif helps) ? Remember colons and indentation ? pass can be used for an empty block
D. Koop, CSCI 503, Spring 2021
2
while, break, continue
? while :
? Condition is checked at the beginning and before each repeat
? break: immediately exit the current loop
? continue: stop loop execution and go back to the top of the loop, checking the condition again
? while d > 0: a = get_next_input() if a > 100: break if a < 10: continue d -= a
D. Koop, CSCI 503, Spring 2021
3
TheEGdgoar DTiojksStrat:aGtoeTmo SetantetmDenet Cboanstiedered Harmful
"...I became convinced that the go to statement should be abolished from all 'higher level' programming languages... The go to statement as it stands is just too primitive; it is too much an invitation to make a mess of one's program."
D. Koop, CSCI 503, Spring 2021
[Dijkstra, 1968]
4
Loop Styles
? Loop-and-a-Half
d = get_data() # priming rd while check(d):
# do stuff d = get_data()
? In nite-Loop-Break
while True: d = get_data() if check(d): break # do stuff
? Assignment Expression (Walrus)
while check(d := get_data): # do stuff
D. Koop, CSCI 503, Spring 2021
5
if
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- chapter 5 functions and parameter passing
- i am reading through the python tutorials and i have
- the function of functions python programming introduction
- 1 functions in python
- pass by object reference in python university of tulsa
- programming principles in python csci 503
- 3 1 system calls
- ppyytthhoonn ffuunnccttiioonnss tutorialspoint
- working with functions in python
- python function basics introduction
Related searches
- financial management principles in healthcare
- financial management principles in health
- financial principles in healthcare
- finance principles in health care
- adult learning principles in nursing
- ethical principles in the workplace
- 7 ethical principles in nursing
- personal principles in life
- ethical principles in nursing definitions
- 4 ethical principles in medicine
- basic ethical principles in research
- basic ethical principles in business