<jupyter_start><jupyter_text>Python Objects, Map, Lambda, and List Comprehensions<NAME>, PhD6/26/2018  ObjectsExample of a class in python:<jupyter_code>class Person:
    company = 'nVidia' #a class variable

    def set_name(self, new_name): #a method
        self.name = new_name
    def set_location(self, new_location):
        self.location = new_location
person = Person()
person.set_name('<NAME>')
person.set_location('2701 San Tomas Expressway Santa Clara, CA 95050')
print('{} works for {} located at {}'.format(person.name, person.company, person.company))<jupyter_output><NAME> works for nVidia located at nVidia<jupyter_text>Question- Define an object Student with one field `name` with a getter and setter for student name.- Set the student's name to 'Einstein'.- Get the student's name and format as shown in the solution cell.<jupyter_code># Your answer here:

from IPython.core.display import HTML
class Student:

    def set_name(self, new_name): #a method
        self.name = new_name
    def get_name(self):
        return self.name

student = Student()
student.set_name('Einstein')
print('Student name: {}'.format(student.get_name()) )

HTML('''<script>
code_show=true; 
function code_toggle() {
    if (code_show){
        $('div.cell.code_cell.rendered.selected div.input').hide();
    } else {
        $('div.cell.code_cell.rendered.selected div.input').show();
    }
    code_show = !code_show
} 

$( document ).ready(code_toggle);
</script>

To show/hide the solution cell, click <a href="javascript:code_toggle()">here</a>.''')<jupyter_output>Student name: Einstein<jupyter_text>MapMaps return an iterator that applies a function to every item of one or more iterables.Example of `mapping` the `min` function between two lists.<jupyter_code># iterables, e.g., lists
store1 = [10.00, 11.00, 12.34, 2.34]
store2 = [9.00, 11.10, 12.34, 2.01]

# map function min() to each item of iterables
cheapest = map(min, store1, store2)

# map returns a map iterator
type(cheapest)<jupyter_output><empty_output><jupyter_text>Now literate through the map object to see the values.<jupyter_code>for item in cheapest:
    print(item)<jupyter_output>9.0
11.0
12.34
2.01<jupyter_text>Lambda Lambda operator or lambda function is used for creating small, one-time anonymous function objects in Python. Works well with filter, and map.Simple function.<jupyter_code>def add_first_two(a, b, c):
    return a+b

add_first_two(1, 2, 3)<jupyter_output><empty_output><jupyter_text>Same function as lambda function.<jupyter_code>my_function = lambda a, b, c : a + b
my_function(1, 2, 3)<jupyter_output><empty_output><jupyter_text>QuestionImplement the following function as a lambda function.<jupyter_code>def mod_it(x, mod_value):
    if x % mod_value == 0:
        return x
    else:
        return None
    
print(mod_it(7, 2))
print(mod_it(6, 2))
# Your work here

lambda_mod_it = lambda x, mod_value: x if x % mod_value == 0 else None
        
print('lambda_mod_it(7, 2)', lambda_mod_it(7, 2))
print('lambda_mod_it(6, 2)', lambda_mod_it(6, 2))
lambda_mod_it = lambda x, mod_value: x if x % mod_value == 0 else None

print('lambda_mod_it(7, 2)', lambda_mod_it(7, 2))
print('lambda_mod_it(6, 2)', lambda_mod_it(6, 2))

HTML('''<script>
code_show=true; 
function code_toggle() {
    if (code_show){
        $('div.cell.code_cell.rendered.selected div.input').hide();
    } else {
        $('div.cell.code_cell.rendered.selected div.input').show();
    }
    code_show = !code_show
} 

$( document ).ready(code_toggle);
</script>

To show/hide the solution cell, click <a href="javascript:code_toggle()">here</a>.''')<jupyter_output>lambda_mod_it(7, 2) None
lambda_mod_it(6, 2) 6<jupyter_text>List comprehensionList comprehensions provide a concise way to create lists. They take the following form:  list_variable = [x for x in iterable] Simple function to iterate from 0 to 999 and return the even numbers.<jupyter_code>my_list = []
for number in range(0, 1000):
    if number % 2 == 0:
        my_list.append(number)
my_list<jupyter_output><empty_output><jupyter_text>Now the same thing but with list comprehension.<jupyter_code>my_list = [number for number in range(0, 1000) if number % 2 == 0]
        
        
        
        
        
my_list = [number for number in range(0,1000) if number % 2 == 0]
my_list<jupyter_output><empty_output><jupyter_text>Performance of list comprehensions<jupyter_code># standard function with list append
def square_even(arr):
    even_squares = []
    for i in arr:
        if i%2==0:
            even_squares.append(i)

%timeit square_even(range(1,11))
# same function with list comprehension
def square_even_lc(arr):
    return [i**2 for i in arr if i%2==0]
%timeit square_even_lc(range(1,11))<jupyter_output>2.76 µs ± 65.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
{'fraction_non_alphanumeric': 0.12059676750932449, 'fraction_numerical': 0.029838375466224618, 'mean_word_length': 4.4296962879640045, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '250265', 'license': 'MIT', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1634, 'n_words': 474, 'path': 'notebooks/3 - Python Objects Map Lambda List Comprehensions.ipynb', 'repo_name': 'larsonma/DataScienceIntro', 'size': '4846'}