Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Similar presentations


Presentation on theme: "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"— Presentation transcript:

1 COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Jehan-François Pâris Fall 2017 1

2 THE ONLINE BOOK CHAPTER V MODULES

3 The problem Writing from scratch Reinventing the wheel
Better to use already written code Packaged for our convenience into modules

4 Two big advantages Writing programs take less time
Some of the work was done for us We can trust that code Faster execution Most modules are written in C/C++ Much faster than native Python

5 What is a module? A file containing Python code designed to be used by other users Ready-to-use functions: math.sin(), … New object definitions: turtle.Screen, turtle.Turtle, … Constants math.pi

6 Where to find the right modules?
On-line Python documentation Global Python Module Index Python books Googling "Python 3 random"

7 Using modules Must first import the module import turtle import math
import random Can mow use objects, functions and constants defined in the imported modules Must prefix names with module name turtle.Turtle, math.sin(), random.random(), math.pi

8 The math module import math Contains function definitions
Power and logarithmic functions math.sqrt(), … Trigonometric functions Functions to convert degrees into radians (and radians into degrees) Hyperbolic functions Special functions

9 Random module import random
Many functions for generating pseudo-random numbers Not really random Result of deterministic arithmetic operations Just appear to be random

10 A warning "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin. For, as has been pointed out several times, there is no such thing as a random number– there are only methods to produce random numbers, and a strict arithmetic procedure of course is not such a method.“ John von Neumann

11 Generating integer RNs
random.randomrange(low_end, high_end) Generates uniformly distributed integer random values from low_end (included) to high_end (excluded) random.randomrange(1, 5) will generate vinteger values between 1 and 4

12 Generating floating-point RNs
random.random() Generates uniformly distributed random values from 0 (included) to 1.0 (excluded) That is, [0. 1) To generate random values from min to max, use random.uniform(min, max)


Download ppt "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"

Similar presentations


Ads by Google