Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python."— Presentation transcript:

1

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

3  ‘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.

4 FunctionReturn 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.

5  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.

6  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”.  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 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

7  “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.

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

9  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.

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

11 class Rectangle: def calculateArea(self,width, height): # “self” is nothing more than a convention. return width*height …. … … r=Rectangle() #creates new Rectangle area=r.calculateArea(3, 4) print("Area: ",area) class Rectangle: def calculateArea(self,width, height): # “self” is nothing more than a convention. return width*height …. … … r=Rectangle() #creates new Rectangle area=r.calculateArea(3, 4) print("Area: ",area)

12  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.

13  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

14  https://docs.python.org/3/library/math.html https://docs.python.org/3/library/math.html  https://docs.python.org/3.5/tutorial/modules.html https://docs.python.org/3.5/tutorial/modules.html  https://docs.python.org/3.5/tutorial/inputoutput.html https://docs.python.org/3.5/tutorial/inputoutput.html


Download ppt " Python’s ‘math’.  Modules.  Files.  Python’s ‘time’.  Classes in python."

Similar presentations


Ads by Google