DFOR510 Week05 Func Flow Time

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

George Mason University

Week 05: Python – Functions, Control Flow, Timestamps


▪ HW1 Questions/Comments/Observations

▪ Week 5 – Python Basics


▪ Revisit – Functions
▪ Control Flow – pass, break, continue
▪ Date/time stamps

2
4

Functions, Control Flow, & Date/Timestamps


❑ Functions
❑ Understand the ‘main’ function and ‘if __name__ == “__main__”’
❑ How to import scripts – to include your own

❑ Control Flow
❑ Learn how to navigate program for more efficient flow
❑ Understand the differences between break, continue, and pass

❑ Date/Time Stamps
❑ Identify common libraries associated with date/time stamp
❑ How to format date/time stamps for user readability
5
❑ Extracting timestamps from files
6

main() & if __name__ == ‘__main__’


▪ Reusable blocks of code

▪ Can take in input and return and output… or not

▪ Initialed by using def, followed by the function name, and


any input parameters

▪ If returning a value, ends with ‘return <variable>’


7
Function definition

Function that takes in an argument (someNum)


Docstrings – not
ordinary comments

Returns an integer back to the function that called it


Special ‘main’ Function

Ordinary 8
comments
▪ Most languages have a ‘main’ function that’s required to execute
programs – this is known as an entry point
▪ Not the case in Python
▪ However, we use main() function for best coding practices,
additionally…
1. We organize code into functions (def) or classes (class) to
optimize code
2. We call our entry point, main()
3. Function calls should be made from main() function
4. We utilize a special function to control what is executed when
scripts are run vs imported
9
▪ Why we use it:
▪ Code reuse
▪ Importing modules

▪ Allow for specific blocks of code to be executed when


needed
▪ Self-test code/unit test

10
IF __NAME__ == ‘__MAIN__’
From BB → Course Content → Week05… → Special_Main_Func_Exercise
Download:
❑ learnSpecialMain (.ipynb)
❑ Not using Juypyter Notebooks: Download learnSpecialMain (.py)
❑ specialMainFunc (.py)
❑ specialPart2 (.py)

Follow the steps given in the learnSpecialMain.ipynb file

11
QUICK CHECK 1
1. The ‘main’ function of a program is called the ______.

2. T or F. Functions must take in variables.

3. Why do we use functions in coding?

4. Describe the benefits of using if __name__ == ‘__main__’

12
14

break, continue, & pass


▪ We automate tasks with ‘for’ and ‘while’ loops
▪ How can we break out of these loops?
▪ break – exits loop when an external condition is met
▪ continue – skips over some elements of the loop

▪ Another control flow element:


▪ pass – code continues regardless of an external
condition, unless a break statement is encountered

15
▪ Used within ‘while’ and ‘for’ loops

▪ Exits loop when an external


condition occurs; executes the
next line of the code immediately
following the loop

▪ In nested loops, exits out of the


innermost loop
https://www.tutorialspoint.com/python/python_break_statement.htm

16
Output:

17
▪ Used within ‘while’ and ‘for’ loops

▪ Ignores statements after the


‘continue’ within a loop and returns
to the beginning of the loop

https://www.tutorialspoint.com/python/python_continue_statement.htm
18
19
Output:

20
▪ Can be used anywhere in a program
▪ Essentially a null operator
▪ Can be used as a placeholder for code that will be
developed
▪ Executes all code, unless a ‘break’ is encountered

21
22
From BB → Course Content → Week05... → ControlFlowExercises
❑ Download breakContPass (.ipynb or .py)

❑ Follow the steps outlined

❑ Complete “Quick Check 2” with your group

23
QUICK CHECK 2
Match the command to its definition

a. allows code to continue regardless


_break of an external condition, unless a
break is encountered
_continue b. skips over some elements of your
loop
_pass c. exits a loop when an external
condition is triggered

24
26

datetime & time libraries


▪ Module for handling time in UNIX format (epoch) and time
conversions
▪ Epoch = Jan 1,1970 00:00:00 (UTC)
▪ How to check systems epoch time?
▪ time.time() - time in seconds since the epoch as a floating
point number
▪ time.strftime(‘…’) – used to format time object

27
▪ Handles dates and times, separately and together
▪ datetime.datetime() – creates datetime object;
▪ Output: (YYYY, MM, DD, hr, min, sec, msec, tzone)
▪ datetime.datetime.now() – return current system time
▪ datetime.timedelta() – difference between 2 datetime
objects;
▪ Output is the difference in days, seconds, and
microsecond
▪ datetime.datetime.strftime() – used for formatting date and
time 28
▪ Functions reside in ‘os’ library

▪ Return value is a number giving the number of seconds


since the epoch
Function Command
Created (or metadata update) time: os.path.getctime(file)
Modified time: os.path.getmtime(file)
Accessed time os.path.getatime(file)
29
From BB → Course Content → Week05... → TimestampExercise
❑ Download:
❑ timeStamps (.ipynb or .py)
❑ Files for Timestamp Exercise (.7z)
❑ You will use this for Assignment 3 – so keep them

❑ Follow the steps outlined

❑ Complete “Quick Check 3” with your group


30
QUICK CHECK 3
1. For most systems epoch time starts when?

2. Command for retrieving a directory/file’s:


a. Creation time
b. Modification time
c. Access time

3. This library is able to handle dates and times separately or


together.

4. This method is used to format timestamps. 31


33
https://clipartfox.com/categories/view/8afc348758b19996c75d34ea32d903b144a67dca/spring-break-2017.html
▪ HW2 assigned – posted on BB

▪ 10 – March: Midterm (2 weeks)


▪ Covers lectures (to include slides, class discussions, and
assignments)

▪ 17 – March: Spring Break! NO CLASS

▪ 24 & 31 March: Presentations


▪ Submit all presentation to Blackboard (Discussion Board) NO
LATER THAN 24-March 34
General Python
1. Luzt Mark (2013). Learning Python 5th Edition. CA: O’Reilly Media, Inc.
2. https://www.python.org/dev/peps/
3. https://docs.python.org

Main Function
4. https://docs.python.org/3/tutorial/modules.html
5. https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html
6. https://realpython.com/python-main-function/
7. https://www.youtube.com/watch?v=sugvnHA7ElY

Control Flow – break, continue, pass


8. https://www.digitalocean.com/community/tutorials/how-to-use-break-continue-and-pass-statements-
when-working-with-loops-in-python-3

Datetime_time Python Libraries


9. https://docs.python.org/3/library/datetime.html?highlight=datetime#strftime-strptime-behavior

35
36

Date/time formatter, Comparison Operators,


ASCII Table
37
https://docs.python.org/3/library/datetime.html?highlight=datetime#strftime-strptime-behavior
Python Timestamps – strftime() formats (2)

38
https://docs.python.org/3/library/datetime.html?highlight=datetime#strftime-strptime-behavior
Python Timestamps – strftime() formats (3)

39
https://docs.python.org/3/library/datetime.html?highlight=datetime#strftime-strptime-behavior
C
O
o
p
m
e
p
r
a
a
r
t
i
o
s
r
o
s
n
40
41

You might also like