CMPT 120 Topic: Python Modules.

Slides:



Advertisements
Similar presentations
Introduction to R - Functions, Packages Andrew Jaffe 10/18/10.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Fundamentals of Python: From First Programs Through Data Structures
Control Structures FOR Statement Looping.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Lesson 06: Functions Class Participation: Class Chat:
Introduction to Computing Science and Programming I
Topic: Python Lists – Part 1
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
Exam #1 You will have exactly 30 Mins to complete the exam.
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topics Introduction to Functions Defining and Calling a Void Function
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
CMPT 120 Topic: Searching – Part 2
PYGAME.
Topic: Introduction to Computing Science and Programming + Algorithm
Lesson #6 Modular Programming and Functions.
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Topic: Python’s building blocks -> Variables, Values, and Types
Lesson #6 Modular Programming and Functions.
Topic: Conditional Statements – Part 2
CMPT 120 Topic: Searching – Part 1
Lecture: MATLAB Chapter 1 Introduction
CMPT 120 Topic:  Case Study.
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
Lecture 2 Introduction to Programming
Lesson #6 Modular Programming and Functions.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Assignment 4 For this assignment refer to the notes on MATLAB from an MIT course that you can find here (or at the original website, lectures 14, 15, 16).
Lesson 06: Functions Class Chat: Attendance: Participation
Last Class We Covered What makes “good code” good Top down design
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Lesson #6 Modular Programming and Functions.
PYTHON: BUILDING BLOCKS Sequencing & Selection
CISC101 Reminders Assignment 2 due this Friday.
CISC101 Reminders Quiz 1 marking underway.
CMPT 120 Lecture 2 - Introduction to Computing Science – Problem Solving, Algorithm and Programming.
Topic: Iterative Statements – Part 2 -> for loop
Functions, Procedures, and Abstraction
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Variables, Constants, Assign.
CMPT 120 Lecture 4 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Lecture 17 – Practice Exercises 3
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 37 – Practice Exercises 9
CMPT 120 Topic: Python strings.
Lecture 17 – Practice Exercise 3 SOLUTIONS
Lecture 37 – Practice Exercises 9
CMPT 225 Lecture 7 – Stack.
Presentation transcript:

CMPT 120 Topic: Python Modules

Last Few Lectures Functions

Learning outcomes At the end of this course, a student is expected to: Create (design) small to medium size programs using Python: Use the core features of Python to design programs to solve problems: variables, expressions, terminal input and output, type conversion, conditionals, iteration, functions, standard library modules Identify and use built-in objects (from predefined classes), and objects defined in modules

Today’s Menu Modules What are they Where to find them How to use them Demo: Turtle module

Remember our Guessing Game Problem Statement: Write a guessing game using Python We pick a number and the user tries to guess our number Our program displays an appropriate message: "Wow! You got it!" OR "Sorry! You missed! The number was <#>. # List all modules used in this program import random # Get a number from 1 to 10 for the user to guess number = random.randint(1,10) # For debugging purposes: print("number to guess is: ", number) Python module

What is a module? Python comes with a huge library of pre-written code (functions) that we can use in our Python program This pre-written code is organised into modules Each module has a theme For example: random, math, calendar and turtle This makes it easy to search for functionality (i.e., functions) throughout all Python modules

Python modules Question: How do we learn about all the Python modules? Answer: The same way we learn about all the Python built-in functions and methods -> we look them up on web sites Here is one web site describing Python modules: https://docs.python.org/3/py-modindex.html Question: How do we know which function to use from a particular module (for example, from the random module)? Answer: We search the web site describing the desired module For example, here is the link to the random module web site: https://docs.python.org/3/library/random.html By the way, here is a super useful web site https://docs.python.org/3/library/

How do we use a module? To use the functions of a Python module in our program, we must first import the module Syntax: import <moduleName> Example: import random Important: We can use the functions of a module as soon as we have imported the module, therefore, we place the import statement at the top of our Python program so that its functions can be called at any point in our program To use a particular function of a Python module in our Python program, we use the following dot notation syntax: Syntax: <moduleName>.<functionName>( ) Example: aRandomInt = random.randint(1,10)

How do we use a module? Other syntax: Syntax: import <moduleName> as <shortNameForModule> Example: import turtle as t import turtle as t2 t.pencolor("red") t2.pencolor("blue") Advantage of this syntax: you can create > 1 turtle Syntax: from <moduleName> import <functionName> Example: from math import sqrt, pi circleArea = pi * sqrt(radius) Advantage of this syntax: you only import what you need out of the module

Module Demo Let’s illustrate how to use a module by looking at the Turtle module

Summary Modules What are they Where to find them How to use them Demo: Turtle module

Next Lecture Case study: creating a program with functions