GithubHelp home page GithubHelp logo

mkosaka1 / python_basics Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 18 KB

Explaining the basics of Python concepts -- Objects and Classes, and Generators

Jupyter Notebook 100.00%
python-objects python-classes python-generators

python_basics's Introduction

Basics of Python - Introduction

The primary purpose of this project is to serve as a resource for myself and others with understanding different concepts in Python with the use of examples.

In computer programming, classes are a great way to organize attributes (variables) and methods (functions) so that they are easy to reuse and extend later. In this notebook, we walk through how to build a basic class in python. Specifically, we discuss the example of implementing a class that represents company employees.

In this simple example from our Objects and Classes notebook, we have created an Employee class, then used the __init__() method to define attributes name and income automatically as we instantiate our employee1 and employee2 objects.

class Employee:
    def __init__(self, name, income):
        self.name=name
        self.income=income

employee1=Employee('Matt',50000)
print(employee1.name)
print(employee1.income)

employee2=Employee('Penelope',90000)
print(employee2.name)
print(employee2.income)

# OUTPUT
Matt
50000
Penelope
90000

Python generators are a simple way of creating iterators. All the work we mentioned above are automatically handled by generators in Python. Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).

In this simple example from our Generators notebook, here we create a generator function to produce odd numbers

def get_odds_generator():
    n=1
    
    n+=2
    yield n
    
    n+=2
    yield n 
    
    n+=2
    yield n
    
numbers=get_odds_generator()
print(next(numbers))
print(next(numbers))
print(next(numbers)) 

In comparison to a simple class-based iterator:

class get_odds:
    def __init__(self, max):
        self.n=3
        self.max=max
    def __iter__(self):
        return self
    def __next__(self):
        if self.n <= self.max:
            result = self.n
            self.n += 2
            return result
        else:
            raise StopIteration

numbers = get_odds(10)
print(next(numbers))
print(next(numbers))
print(next(numbers))

Generator functions are much easier and simpler to understand!

python_basics's People

Contributors

mkosaka1 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.