0% found this document useful (0 votes)
4 views

Lecture09 Input Time Math Exception

Python user input

Uploaded by

botchannel280
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture09 Input Time Math Exception

Python user input

Uploaded by

botchannel280
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Python Programming

(User Input)

Hyuntae Cho
Dept. of Digital Content
Tongmyong University
User Input
• Python allows for user input.

• That means we are able to ask the user for input.

• The method is a bit different in Python 3.6 than Python 2.7.

• Python 3.6 uses the input() method.

• 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.

• The datetime() class requires three parameters to create a date: year,


month, day.

• Example
• Create a date object:

7
The strftime() Method
• The datetime object has a method for formatting date objects into
readable strings.

• The method is called strftime(), and takes one parameter, format, to


specify the format of the returned string:
• Example
• Display the name of the month:

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:

• The abs() function returns the absolute (positive) value of the


specified number:

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.

• To use it, you must import the math module:

• When you have imported the math module, you can start using
methods and constants of the module.

• The math.sqrt() method for example, returns the square root of a


number:

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:

• The math.pi constant, returns the value of PI (3.14...):

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 except block lets you handle the error.

• 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.

• These exceptions can be handled using the try statement:

• 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:

The program can continue, without


leaving the file object open.

23
Raise an exception
• As a Python developer you can choose to throw an exception if a
condition occurs.

• To throw (or raise) an exception, use the raise keyword.

• 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

You might also like