Lecture09 Input Time Math Exception
Lecture09 Input Time Math Exception
(User Input)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
User Input
• Python allows for user input.
• The following example asks for the username, and when you entered
the username, it gets printed on the screen:
Python stops executing when it comes to the input() function, and continues when
the user has given some input.
2
Add Two Numbers with User Input
• In this example, the user must input two numbers. Then we print the
sum by calculating (adding) the two numbers:
•
3
Python Programming
(Datetime)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Dates
• A date in Python is not a data type of its own, but we can import a
module named datetime to work with dates as date objects.
• Example
– Import the datetime module and display the current date:
5
Date Output
• When we execute the code from the example above the result will
be:
– 2024-01-28 19:09:01.017162
• The date contains year, month, day, hour, minute, second, and
microsecond.
• The datetime module has many methods to return information about
the date object.
• Example
– Return the year and name of weekday:
6
Creating Date Objects
• To create a date, we can use the datetime() class (constructor) of the
datetime module.
• Example
• Create a date object:
7
The strftime() Method
• The datetime object has a method for formatting date objects into
readable strings.
8
A reference of all the legal format codes:
9
A reference of all the legal format codes:
10
Python Programming
(math)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Math
• Python has a set of built-in math functions, including an extensive
math module, that allows you to perform mathematical tasks on
numbers.
12
Built-in Math Functions
• The min() and max() functions can be used to find the lowest or
highest value in an iterable:
13
Built-in Math Functions
• The pow(x, y) function returns the value of x to the power of y (xy).
• Example
– Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
14
The Math Module
• Python has also a built-in module called math, which extends the list
of mathematical functions.
• When you have imported the math module, you can start using
methods and constants of the module.
15
The Math Module
• The math.ceil() method rounds a number upwards to its nearest
integer, and the math.floor() method rounds a number downwards to
its nearest integer, and returns the result:
16
Python Programming
(exception)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Exception Handling
• The try block lets you test a block of code for errors.
• The else block lets you execute code when there is no error.
• The finally block lets you execute code, regardless of the result of the
try- and except blocks.
18
Exception Handling
• When an error occurs, or exception as we call it, Python will normally
stop and generate an error message.
• Example
– The try block will generate an exception, because x is not defined:
– Since the try block raises an error, the except block will be executed.
– Without the try block, the program will crash and raise an error:
19
Many Exceptions
• You can define as many exception blocks as you want, e.g. if you
want to execute a special block of code for a special kind of error:
• Example
– Print one message if the try block raises a NameError and another for other
errors:
20
Else
• You can use the else keyword to define a block of code to be
executed if no errors were raised:
• Example
– In this example, the try block does not generate any error:
21
Finally
• The finally block, if specified, will be executed regardless if the try
block raises an error or not.
22
Finally
• This can be useful to close objects and clean up resources:
• Example
– Try to open and write to a file that is not writable:
23
Raise an exception
• As a Python developer you can choose to throw an exception if a
condition occurs.
• Example
– Raise an error and stop the program if x is lower than 0:
24
Raise an exception
• The raise keyword is used to raise an exception.
• You can define what kind of error to raise, and the text to print to
the user.
• Example
– Raise a TypeError if x is not an integer:
25
Conclusion
26