14
INTRODUCTION TO PYTHON #2

Python’s ‘math’. Modules. Files. Python’s ‘time’. Classes in python

Embed Size (px)

Citation preview

Page 1: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

INTRODUCTION TO PYTHON #2

mahmoud eed
Introductoin to Python(Mathematics, file system, DATES AND TIME and Modules and Classes)Task or assigment(with grading criteria)
Page 2: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

AGENDA

Python’s ‘math’. Modules. Files. Python’s ‘time’. Classes in python.

Page 3: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

MATHEMATICAL FUNCTIONS‘MATH’

‘math’ provides access to the mathematical functions defined by the C standard.

‘math’ cannot be used with complex number, instead ‘cmath’ module can be used with the same function(s) name(s).

The ‘math’ module consists mostly of thin wrappers around the platform C math library functions.

Page 4: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

SOME USEFUL ‘MATH’ FUNCTIONSFunction Return value

math.ceil(x) Returns the ceiling of x, the smallest integer greater than or equal to x.

math.fabs(x) Returns the absolute value of x.

math.factorial(x) Returns x factorial. Raises ValueError if x is not integral or is negative.

math.floor(x) Returns the floor of x, the largest integer less than or equal to x.

math.fmod(x, y) Returns x mod y, math.fmod is more accurate than x%y.

math.gcd(a, b) Returns the greatest common divisor of the integers a and b. If either a or b is nonzero, then the value of math.gcd(a, b) is the largest positive integer that divides both a and b.

math.exp(x) Returns e**x.

math.log2(x) Returns the base-2 logarithm of x. This is usually more accurate than log(x,2).

math.sqrt(x) Returns the square root of x.

Page 5: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

MODULES

Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be “imported” into other modules or into the main module. 

Page 6: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

MODULE EXAMPLE

Create a file and name it “fibo.py” for example. Write the following code in the module file:

Now we can use the created fibonacci function by importing this module : “import fibo”, or “from fibo import *” , or “from fibo import fib2”.

Then “fibo.fib2(1000)” to call the function named “fib2”.

def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result

Page 7: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

READING FROM/WRITING TO FILES

“Open()”: returns a file object, and is most commonly used with two arguments: open(filename, mode).

“mode” can be assigned as follows: ‘r’->reading, ‘w’->writing, ‘a’-> appending, ‘r+’->for both reading and writing. ‘r’ is assumed by default if this argument is omitted.

Normally, files are opened in text mode, that means, you read and write strings from and to the file, 'b‘ appended to the mode opens the file in binary mode: now the data is read and written in the form of bytes objects.

Page 8: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

‘FILES’ EXAMPLE

f = open('NEWinfoFILE.txt', 'w') #also creates the file if it doesn’t exist.

f1 = open('info.txt', 'r+')

value = ('the answer is: ', 42)

s = str(value)

f1.seek(0)

f1.write(s)

f1.seek(0)

data = f1.read(size) #the entire contents of the file is read and returned if ‘size’ is negative or if it’s omitted, otherwise a number of characters specified by ‘size’ will be returned.

#data1 = f1.readline()

print(data1)

f.close()

f1.close() f1.seek(0) #sets the file's current position at the offset. f2.truncate() #truncates the contents of the file.

Page 9: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

PYTHON’S ‘TIME’ MODULE

This module provides various time-related functions. Most of the functions defined in this module call platform

C library functions with the same name. Python also includes the ‘calendar’ module, which

provides additional useful functions related to the calendar, and also ‘datetime’ module.

Page 10: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

‘TIME’ EXAMPLE

timeStruct=time.localtime() # or gmtime() simpleFormUsingStruct=time.asctime(timeStruct) #converts the struct_time to a string of the following form: 'Sun Jun 20 23:21:05 1993'.

print(timeStruct)print(simpleFormUsingStruct)print(time.strftime('%x %Z')) #%x Locale’s appropriate date representation, %Z time zone name.

Page 11: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

CLASSES

class Rectangle: def calculateArea(self,width, height): # “self” is nothing more than a convention. return width*height

…. … …r=Rectangle() #creates new Rectanglearea=r.calculateArea(3, 4)print("Area: ",area)

Page 12: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

TASK #2 Create a class called person. This class includes a function called “check_age” that

returns true if this person is 18 years of age or older, false otherwise.

Manually create a text file ‘NumberOfLicenses.txt’ that includes a numerical value only, if the result of the previous check is true, increment this number in the file and print the new value, otherwise print how many years he/she shall wait to be able to get a license.

So the program shall receive the age from the user then continue from this point.

Page 13: Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python

GRADING CRITERIA 5 Marks: The person class and “check_age” function: 2 Correctly changing the number of licenses in the file: 2 Printing how many years the under 18 user have to

wait to be able to get a license: 1