COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
COMPSCI 101 Principles of Programming
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Programming Training Main Points: - Python Statements - Problems with selections.
Computer Science 111 Fundamentals of Programming I Overview of Programming.
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Python Libraries Importing and Calling functions.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in.
COSC 235: Programming and Problem Solving Chapter 3: Arithmetic vs Numerics Instructor: Dr. X 1.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
LING 408/508: Programming for Linguists Lecture 20 November 16 th.
Math Facts Single Digit Addition ©
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Invitation to Computer Science 6th Edition
Fundamentals of Programming I Overview of Programming
Why don’t programmers have to program in machine code?
Main Points: - Python Statements - Problems with selections.
Introduction to Python
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CS 100: Roadmap to Computing
Chapter 4 Mathematical Functions, Characters, and Strings
Today’s Objectives Review the important points of classes
Introduction to Programming
CS 100: Roadmap to Computing
Computer Science 210 Computer Organization
Analysis of Algorithms
Functions, Procedures, and Abstraction
Value returning Functions
CS 100: Roadmap to Computing
Learning to Program in Python
The C Programming Language
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Assignment Operators Topics Increment and Decrement Operators
Java Online documentation
Assignment Operators Topics Increment and Decrement Operators
The Assembly Language Level
Last Class We Covered What makes “good code” good Top down design
Introduction to Programming with Python
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Computer Simulation Techniques Generating Pseudo-Random Numbers
Class 6 using the math library
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Course Outcomes of Programming In C (PIC) (17212, C203):
Math Facts Single Digit Addition
Assignment Operators Topics Increment and Decrement Operators
Using Modules.
Presentation transcript:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu Fall 2017 1

THE ONLINE BOOK CHAPTER V MODULES

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

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

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

Where to find the right modules? On-line Python documentation https://docs.python.org/3/ Global Python Module Index https://docs.python.org/3/py-modindex.html Python books Googling "Python 3 random"

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

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

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

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

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

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)