Today’s Objectives Review the important points of classes

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Modules and Objects Introduction to Computing Science and Programming I.
Introduction to Python
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Classes, Interfaces and Packages
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 20 – Classes and Modules Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Introduction to Computing Science and Programming I
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
COMPSCI 107 Computer Science Fundamentals
CMSC201 Computer Science I for Majors Lecture 04 – Expressions
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Topics Procedural and Object-Oriented Programming Classes
Software Development Java Classes and Methods
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CS-104 Final Exam Review Victor Norman.
ECS10 10/10
Computer Science 111 Fundamentals of Programming I
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Fundamentals of Programming I Managing the Namespace
Lecture VI Objects The OOP Concept Defining Classes Methods
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Creating and Deleting Instances Access to Attributes and Methods
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Random numbers Taken from notes by Dr. Neil Moore
Python Classes By Craig Pennell.
Inheritance Basics Programming with Inheritance
Teach A-Level Computer Science: Object-Oriented Programming in Python
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
MSIS 655 Advanced Business Applications Programming
Log onto a computer first then ….
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Computer Programming with JAVA
Variables and Expressions
Computer Science 111 Fundamentals of Programming I
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
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.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CMSC201 Computer Science I for Majors Final Exam Information
Object-Oriented PHP (1)
Prof. Katherine Gibson Prof. Jeremy Dixon
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
A Level Computer Science Topic 6: Introducing OOP
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
 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.
Functions that return a value
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 26 – Python and emacs Fun

Today’s Objectives Review the important points of classes Classes will be on the exam Learn some cool Python stuff Importing libraries to do tasks for you (Pseudo) random numbers Emacs shortcuts!

Review of Classes

In-Class Exercise Labelling the parts of a class! Partial list of answers: Class name Constructor Method Attribute Object the method is called on Keyword to create class

Built-In Functions Classes have two important built-in functions Have double underscores on either side of name __init__ Constructor for the class Initializes and creates attributes __str__ Creates the string representation of the object Used when we call print() with an instance

Abstraction and Encapsulation All programming languages provide some form of abstraction Hide the details of implementation from the user All the user needs to know is the name and basics Encapsulation is a form of information hiding and abstraction used in classes Data and functions that act on that data are located in the same place (inside a class)

The self Variable The self variable is how we refer to the current instance of the class In __init__, self refers to the object that is currently being created In other methods, self refers to the instance the method was called on def speak(self): print("\"" + str(self.species) + " noise\"")

Inheritance Inheritance is when one class is based upon another class (child inherits from parent) The child class inherits most or all of its features from the parent class it is based on Inherits both methods and attributes Child class can extend and override the methods from the parent class What do each of these mean?

Python Fun!

Importing Modules A module is a Python file that contains function definitions and other statements To import modules, use this command: import moduleName This imports the entire module of that name Every single thing in the file is now available This includes functions, data types, constants, etc.

Calendar Module Example import calendar exCal = calendar.TextCalendar() printCal = exCal.formatmonth(2017, 5) print(printCal) May 2017 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

import To use the things we’ve imported this way, we need to append the filename and a period to the front of its name (“moduleName.”) To access a function called function: moduleName.function()

“Random” Numbers

Random Numbers Random numbers are useful for many things Like what? Cryptography Games of chance Procedural generation Minecraft levels, snowflakes in Frozen Random numbers generated by computers can only be pseudo random

Pseudo Randomness “Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.” – John von Neumann Pseudorandom appears to be random, but isn’t Mathematically generated, so it can’t be Called a Random Number Generator (RNG)

Seeding for Randomness The RNG isn’t truly random The computer uses a “seed” in an attempt to be as random as possible By default, the seed is the system time Changes every time the program is run We can set our own seed Use the random.seed() function

Seeding for Randomness Same seed means same “random” numbers Good for testing, allow identical runs random.seed(7) random.seed("hello") 7 always gives .32, .15, .65, .07 “hello” always gives .35, .66, .54, .13

How Seeds Work “Resets” the random number generator each time it is seeded Should only seed once per program Seeding and calling gives the same number >>> random.seed(3) >>> random.random() 0.23796462709189137

Generating Random Floats random.random() Returns a random float from 0.0 up to (but not including) 1.0 >>> random.seed(201) >>> random.random() 0.06710225875940379 >>> random.random() 0.3255995543326774 >>> random.random() 0.0036753697681032316 >>> random.random() 0.28279809896785435

Generating Random Integers random.randrange() Works the same as normal range() Start, stop, and step >>> random.seed("dog") >>> random.randrange(2, 21, 4) 14 >>> random.randrange(2, 21, 4) 6 >>> random.randrange(2, 21, 4) 10 >>> random.randrange(6) 5 >>> random.randrange(6) 4

Generating Random Options random.choice() Takes in a list, returns one of the options at random >>> dogs = ["Yorkie", "Xolo", "Westie", "Vizsla"] >>> random.seed("yay, summer!") >>> random.choice(dogs) 'Yorkie' >>> random.choice(dogs) 'Xolo' >>> random.choice(dogs) 'Westie'

GL and emacs Shortcuts

Announcements Final is Friday, May 19th from 6 to 8 PM Start studying now! Review worksheet won’t come out until Saturday Final exam locations: Gibson (2, 3, 4, 5, 15, 16, 18) in ENGR 027 Wilson (9, 10, 11, 12, 20, 21, 22, 23) in MEYR 030 Project 3 due on May 12th @ 8:59:59 PM

What do you want to learn?

Common Tasks Moving around the file Copying, cutting, and pasting Searching and replacing Advanced commands (Un)comment region “Meta” (escape) GL vs bash