Python Training at VMWare Pune - Day 1

Oct 16-18, 2017 Vikrant Patil

These notes are available online at http://notes.pipal.in/2017/vmware-oct-python

© Pipal Academy LLP

Day 1 | Day 2 | Day 3

Quick introduction to python

In [1]:
0 #python has basic datatypes... integers
Out[1]:
0
In [2]:
0 + 2
Out[2]:
2
In [3]:
1.2 # it has floats
Out[3]:
1.2
In [4]:
x = 3
In [5]:
print(x)
3
In [6]:
name = "krishna"
In [7]:
name
Out[7]:
'krishna'
In [8]:
print(name)
krishna

Basic data types

In [15]:
2 + 2 # addition
Out[15]:
4
In [16]:
2 * 2 #multiplication
Out[16]:
4
In [17]:
2 ** 3 # power
Out[17]:
8
In [29]:
13 % 10 # divmod
Out[29]:
3
In [19]:
2 ** 100
Out[19]:
1267650600228229401496703205376
In [21]:
2 ** 1000 # it supports really big numbers
Out[21]:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
In [24]:
5 / 2 # division
Out[24]:
2.5
In [25]:
5 //2  # integer division
Out[25]:
2
In [26]:
0.001 + 1
Out[26]:
1.001
In [31]:
1.1 - 1.0
Out[31]:
0.10000000000000009

Python has strings with lots of useful features

In [32]:
first = "Rupali"
second = "Rupak"
In [33]:
first
Out[33]:
'Rupali'
In [34]:
second
Out[34]:
'Rupak'
In [35]:
first + second
Out[35]:
'RupaliRupak'
In [36]:
print(first, second)
Rupali Rupak
In [37]:
fullname = first + " " + second
In [38]:
print(fullname)
Rupali Rupak
In [40]:
s = 'string with single quote'
In [41]:
print(s)
string with single quote
In [43]:
double = 'string with " double quote'
In [44]:
print(double)
string with " double quote
In [45]:
single = "string with ' single quote"
In [46]:
print(single)
string with ' single quote
In [47]:
fullname = "Rupali" "Rupak"
In [48]:
print(fullname)
RupaliRupak
In [49]:
five = str(5)
In [50]:
print(five)
5
In [51]:
five
Out[51]:
'5'
In [52]:
star = "*"
In [53]:
5*star
Out[53]:
'*****'
In [54]:
"*"*20
Out[54]:
'********************'

string supports escape characters

In [57]:
twolines = "first line \nsecond line"
In [58]:
print(twolines)
first line 
second line
In [59]:
multiline = """
this is
a multiline
string
which can have
any number
of lines
"""
In [60]:
print(multiline)
this is
a multiline
string
which can have
any number
of lines

In [62]:
regional = "आ ञ"
In [63]:
len(regional)
Out[63]:
3
In [64]:
print(regional)
आ ञ
In [65]:
print("\u0c85\u0c86")
ಅಆ

Python has special type bytes to represent binary data

In [66]:
binary = b'binary'
In [67]:
print(binary)
b'binary'
In [68]:
hex(ord('b'))
Out[68]:
'0x62'
In [69]:
hex(145)
Out[69]:
'0x91'
In [70]:
ord("b")
Out[70]:
98
In [71]:
hex(98)
Out[71]:
'0x62'
In [72]:
binary = b'\x62\x69\x6e\x61\x72\x79'
In [73]:
print(binary)
b'binary'

There is very useful high level data type called list

In [74]:
letters = ['x','y','z']
In [75]:
letters[0]
Out[75]:
'x'
In [76]:
letters[1]
Out[76]:
'y'
In [77]:
letters[2]
Out[77]:
'z'
In [78]:
len(letters)
Out[78]:
3
In [79]:
ones = [1,1,1,1]
In [80]:
moreones = ones + ones
In [81]:
moreones
Out[81]:
[1, 1, 1, 1, 1, 1, 1, 1]
In [82]:
stillmoreones = ones * 5
In [83]:
stillmoreones
Out[83]:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
In [84]:
letters[2]
Out[84]:
'z'
In [85]:
letters[3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-85-4f2a4695a7b3> in <module>()
----> 1 letters[3]

IndexError: list index out of range
In [86]:
mixedtypes = [1, 1, 2, 'x', 'y']
In [87]:
print(mixedtypes)
[1, 1, 2, 'x', 'y']
In [88]:
mixedtypes[0] = "zero"
In [89]:
mixedtypes
Out[89]:
['zero', 1, 2, 'x', 'y']
In [90]:
len(mixedtypes)
Out[90]:
5

List objects have special indexing mechanism by which you can access elements very easily

In [91]:
topten = [0,1,2,3,4,5,6,7,8,9]
In [94]:
topten[-1] # last element
Out[94]:
9
In [95]:
topten[-2] #second last element
Out[95]:
8
In [96]:
topten[:2] # take first two elements
Out[96]:
[0, 1]
In [97]:
topten[2:] #drop first two elements
Out[97]:
[2, 3, 4, 5, 6, 7, 8, 9]
In [99]:
topten[3:7] # from 3rd index(included) till 7th index(excluded)
Out[99]:
[3, 4, 5, 6]
In [100]:
topten[0:5]
Out[100]:
[0, 1, 2, 3, 4]
In [101]:
topten[1:8]
Out[101]:
[1, 2, 3, 4, 5, 6, 7]
In [102]:
matrix = [[1,2,3], [4,5,6], [7,8,9]]
In [103]:
matrix[0] # 0th row
Out[103]:
[1, 2, 3]
In [104]:
matrix[-1] # last row
Out[104]:
[7, 8, 9]
In [105]:
matrix[0][0]
Out[105]:
1
In [106]:
matrix[0][2]
Out[106]:
3

There is another datatype similar to lists is tuple

In [107]:
point = (1.1, 2.0)
In [108]:
point[0]
Out[108]:
1.1
In [109]:
point[-1]
Out[109]:
2.0
In [110]:
triangle = ((0,0), (0,1), (1,1))
In [111]:
triangle[0]
Out[111]:
(0, 0)
In [112]:
triangle[0] = (1,2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-112-6c4c2ede0506> in <module>()
----> 1 triangle[0] = (1,2)

TypeError: 'tuple' object does not support item assignment
In [113]:
ones = (1, 1, 1)
In [114]:
ones  + ones
Out[114]:
(1, 1, 1, 1, 1, 1)
In [115]:
ones*5
Out[115]:
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

Do it yourself

  • What is the difference in output of interpreter and output of function print
    s = "Just to make sure that \n you undestrand new line"
    s
    print(s)
  • Will this work?
    pre = "Isac"
    post = "Asimov"
    name = pre post
  • What will be output of following sequence of commands in python interpreter? observe the special variable _
    2 ** 2
    _
    a = 5*6
    a
    _
    _ = 67
    a/6
    _
    31//6
    _

Dictionaries are named collection of objects.

In [116]:
person = {'name':"Alice", "email":"alice@wonder.land", "age":15}
In [117]:
person['name']
Out[117]:
'Alice'
In [118]:
person['email']
Out[118]:
'alice@wonder.land'

Python also has set

In [119]:
paragraph = """
lets write some dummy python fiction
to create set. and try to find out how 
many alphabets are used in 
this
"""
In [120]:
alphabets = set(paragraph)
In [121]:
alphabets
Out[121]:
{'\n',
 ' ',
 '.',
 'a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'h',
 'i',
 'l',
 'm',
 'n',
 'o',
 'p',
 'r',
 's',
 't',
 'u',
 'w',
 'y'}

And offcourse you talk about True and False in python

In [122]:
sure = True
arrr = False
In [123]:
print(sure, arrr)
True False

There is special object for nothing!

In [124]:
nothing = None

Functions

In [127]:
len("python") # len works on string
Out[127]:
6
In [128]:
len([1,2,3,4]) # len works on lists
Out[128]:
4
In [130]:
len((2,3,45,4)) # len works on tuple
Out[130]:
4
In [131]:
len({'a':1, 'b':2})
Out[131]:
2
In [133]:
4 + "2"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-133-2957f6c14a52> in <module>()
----> 1 4 + "2"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [134]:
4 + int("2")
Out[134]:
6
In [135]:
str(4) + "2"
Out[135]:
'42'

problem: Using the functions above , is it possible to find number of digits in a given number? lets say 2**1000

In [138]:
digits = str(2**1000)
In [139]:
len(digits)
Out[139]:
302

Custom functions

In [142]:
def square(x):
    return x*x

def say_hello(name):
    print("hello", name)
In [143]:
square(5)
Out[143]:
25
In [145]:
say_hello("HARI")
hello HARI
In [146]:
value = square(5)
In [147]:
value
Out[147]:
25
In [148]:
value = say_hello("Hari")
hello Hari
In [149]:
value
In [150]:
print(value)
None

Problem : Write a function count_digits that counts number of digits of a given number

count_digits(100)
3
count_digits(1000)
4

Problem: Before you try out following code , guess what will be output?

def twice(x):
    return 2*x

def twice1(x):
    print(2*x)

twice(twice(2))
twice1(twice1(2))

More about functions

Functions in python are just like other data types. They are ordinary in that sense. But that makes them powewrful! lets see how?

In [151]:
def f(x):
    return 2*x
In [152]:
f
Out[152]:
<function __main__.f>
In [153]:
type(f)
Out[153]:
function
In [154]:
f(2)
Out[154]:
4
In [155]:
twotimes = f
In [156]:
twotimes
Out[156]:
<function __main__.f>
In [157]:
type(twotimes)
Out[157]:
function
In [158]:
twotimes(3)
Out[158]:
6

What is the advantage of having this functionality... able to alias functions as variables

In [159]:
def summation(x,y):
    return x+y
In [160]:
def square(x):
    return x*x
In [161]:
def sumofsquares(x,y):
    return square(x) + square(y)
In [162]:
def cube(x):
    return x*x*x
In [163]:
def sumofcubes(x,y):
    return cube(x) + cube(y)
In [164]:
def sumof(f, x, y):
    return f(x) + f(y)
In [165]:
sumof(square, 3, 4)
Out[165]:
25
In [166]:
sumof(cube, 2, 3)
Out[166]:
35

Some in built functions in python take functions as argument.

In [167]:
max(0, 42)
Out[167]:
42
In [168]:
max([1,2,3,4,5])
Out[168]:
5
In [169]:
max(["one", "two", "three", "four"]) 
Out[169]:
'two'
In [171]:
max(["one", "two", "three", "four"], key=len) # longest word
Out[171]:
'three'
In [172]:
min(["one", "two", "three", "four"], key=len) # shortest word
Out[172]:
'one'

We have a record of scores of students saved as list of tuples. every tuple stores names and score. we wish to find a record with maximum score

In [176]:
scoresheet = [('vijay', 8.8),
              ('vinay', 8.9),
              ('vijaya', 9.0),
              ('vilas', 7.8),
              ('vishakha', 6.0)]
In [177]:
def get_score(record):
    return record[1]
In [178]:
max(scoresheet, key=get_score)
Out[178]:
('vijaya', 9.0)
In [179]:
min(scoresheet, key=get_score)
Out[179]:
('vishakha', 6.0)
In [181]:
x, y = (1,2)
In [182]:
x
Out[182]:
1
In [183]:
y
Out[183]:
2
In [184]:
def get_score(record):
    name, score = record
    return score
In [185]:
max(scoresheet, key=get_score)
Out[185]:
('vijaya', 9.0)

Methods from objects

built in objects have usefull functions built in as part of object. Such functions are called methods

In [186]:
book = "Alice in wonderland"
In [187]:
book.lower()
Out[187]:
'alice in wonderland'
In [188]:
book.upper()
Out[188]:
'ALICE IN WONDERLAND'
In [190]:
book.count("n")
Out[190]:
3
In [191]:
book.split(" ")
Out[191]:
['Alice', 'in', 'wonderland']
In [192]:
book.replace(" ", "_")
Out[192]:
'Alice_in_wonderland'
In [193]:
"_".join(["Alice", "in","wonderland"])
Out[193]:
'Alice_in_wonderland'
In [194]:
book.startswith("Alice")
Out[194]:
True
In [196]:
book.center(50)
Out[196]:
'               Alice in wonderland                '

Problem: write a function which will count number of zeros in given number

count_zeros(100)
2
count_zeros(1000)
3
In [197]:
def count_zeros(x):
    digits = str(x)
    return digits.count("0")
In [200]:
count_zeros(1000)
Out[200]:
3

lists

In [201]:
numbers = list(range(5, 25, 2))
In [202]:
numbers
Out[202]:
[5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
In [203]:
numbers.count(5)
Out[203]:
1
In [204]:
numbers.reverse()
In [205]:
numbers
Out[205]:
[23, 21, 19, 17, 15, 13, 11, 9, 7, 5]
In [206]:
numbers.append(3)
In [207]:
numbers
Out[207]:
[23, 21, 19, 17, 15, 13, 11, 9, 7, 5, 3]
In [208]:
numbers.pop()
Out[208]:
3
In [209]:
numbers
Out[209]:
[23, 21, 19, 17, 15, 13, 11, 9, 7, 5]
In [210]:
numbers.pop(0)
Out[210]:
23
In [211]:
numbers.sort()
In [212]:
numbers
Out[212]:
[5, 7, 9, 11, 13, 15, 17, 19, 21]
In [213]:
numbers.sort(reverse=True)
In [214]:
numbers
Out[214]:
[21, 19, 17, 15, 13, 11, 9, 7, 5]
In [215]:
numbers[0] = 23
In [216]:
numbers
Out[216]:
[23, 19, 17, 15, 13, 11, 9, 7, 5]
In [217]:
numbers[3] = 21
In [218]:
numbers
Out[218]:
[23, 19, 17, 21, 13, 11, 9, 7, 5]
In [220]:
names = ["bhairavi", "durga", "asawary", "kalashree", "bhupali", "tilak kamod"]
In [221]:
names.sort()
In [222]:
names
Out[222]:
['asawary', 'bhairavi', 'bhupali', 'durga', 'kalashree', 'tilak kamod']
In [223]:
names.sort(key=len)
In [224]:
names
Out[224]:
['durga', 'asawary', 'bhupali', 'bhairavi', 'kalashree', 'tilak kamod']
In [225]:
names.extend(["yaman", "malkauns"])
In [226]:
names
Out[226]:
['durga',
 'asawary',
 'bhupali',
 'bhairavi',
 'kalashree',
 'tilak kamod',
 'yaman',
 'malkauns']

tuples

In [231]:
numbers = (1,1,2,3,4)
In [232]:
numbers.count(1)
Out[232]:
2
In [233]:
numbers.index(1)
Out[233]:
0

problem:

  • Write a function head that takes a list of words and returns first n words by dictionary order
    >>> words = ['python', 'lisp', 'haskell', 'ark', 'java', 'ocamel']
    >>> head(words, 3)
    ['ark', 'haskell', 'java']
  • Write a function count_words that takes a sentence or a string as an argument and returns number of words it has.
    >>> count_words("life is 42")
    3
In [2]:
words = ['python', 'lisp', 'haskell', 'ark', 'java', 'ocamel']
In [3]:
words.sort() # this sorts list in place
In [4]:
sorted(words) # this returns a new sorted list
Out[4]:
['ark', 'haskell', 'java', 'lisp', 'ocamel', 'python']
In [237]:
def head(words, n):
    words_sorted = sorted(words)
    return words_sorted[:n]
In [238]:
def head1(words, n):
    words.sort()
    return words[:n]
In [240]:
head(words, 4)
Out[240]:
['ark', 'haskell', 'java', 'lisp']
In [241]:
head1(words, 3)
Out[241]:
['ark', 'haskell', 'java']
In [242]:
def count_words(sentence):
    words = sentence.split(" ")
    return len(words)
In [243]:
count_words("Count words from this sentence")
Out[243]:
5

modules

For convinience related functions are organised inside a module. python's built in modules can be imported and used as given below

In [244]:
import math
In [245]:
def area_circle(radius):
    return math.pi *radius * radius
In [246]:
def polar_to_rectangular(radius, theta):
    return radius*math.cos(theta), radius*math.sin(theta)
In [247]:
area_circle(1)
Out[247]:
3.141592653589793
In [248]:
polar_to_rectangular(1, math.pi/2)
Out[248]:
(6.123233995736766e-17, 1.0)
In [250]:
x , y = polar_to_rectangular(1, math.pi)
In [251]:
print(x, y)
-1.0 1.2246467991473532e-16
In [252]:
import os
In [253]:
os.getcwd() # current working directory
Out[253]:
'/home/vikrant/trainings/2017/vmware-oct-python'
In [255]:
os.getenv("HOME")
Out[255]:
'/home/vikrant'
In [259]:
os.mkdir("/tmp/test")
In [260]:
!ls /tmp/t*
/tmp/t:

/tmp/test:
In [262]:
os.listdir(os.getcwd())
Out[262]:
['.ipynb_checkpoints',
 'push',
 'day1.ipynb',
 'day2.ipynb',
 'day2.html',
 'Untitled.html',
 'day3.ipynb',
 'day3.html',
 'Makefile',
 'day1.html']
In [265]:
os.path.exists("/tmp/test")
Out[265]:
True
In [266]:
os.path.getsize("day2.html")
Out[266]:
249917

problem:

  • Write a function countfiles which takes directory path as argument and returns number of files in that directory
  • Write a function biggestfile to find biggest file in a given directory
In [267]:
import os
def countfiles(dirpath):
    return len(os.listdir(dirpath))
In [268]:
def biggestfile(dirpath):
    files = os.listdir(dirpath)
    return max(files, key=os.path.getsize)
In [269]:
countfiles(os.getcwd())
Out[269]:
10
In [270]:
biggestfile(os.getcwd())
Out[270]:
'day1.html'
In [271]:
import math as m
In [272]:
m.pi
Out[272]:
3.141592653589793
In [273]:
m.sin(3.14)
Out[273]:
0.0015926529164868282
In [274]:
from math import sin
In [275]:
sin(3.14)
Out[275]:
0.0015926529164868282
In [276]:
from os.path import getsize
In [278]:
getsize("./day1.html")
Out[278]:
398679

Writing our own custom modules

In [279]:
%%file mymodule.py
import sys

def say_hello(name):
    print("Hello", name)
Writing mymodule.py
In [280]:
import mymodule
In [281]:
mymodule.say_hello("python")
Hello python
In [282]:
%%file mymodule1.py
import sys

print(sys.argv)
Writing mymodule1.py
In [283]:
!python mymodule1.py 
['mymodule1.py']
In [284]:
!python mymodule1.py argument1 argument2 hello 2 3 4
['mymodule1.py', 'argument1', 'argument2', 'hello', '2', '3', '4']

problem:

  • Write a python script square.py that takes a numbers as commandline argument and prints square os that number
In [5]:
%%file square.py
import sys

def square(x):
    return x*x

v = int(sys.argv[1])
print(square(v))
Overwriting square.py
In [6]:
!python square.py 2
4
In [8]:
import os
In [9]:
help(os.path.getsize)
Help on function getsize in module genericpath:

getsize(filename)
    Return the size of a file, reported by os.stat().

In [303]:
import square
In [304]:
square.square(4)
Out[304]:
16
In [305]:
%%file arguments.py
import sys

def print_arguments():
    """
    prints system arguments
    """
    print(sys.argv)

    
Writing arguments.py
In [306]:
import arguments
In [307]:
help(arguments)
Help on module arguments:

NAME
    arguments

FUNCTIONS
    print_arguments()
        prints system arguments

FILE
    /home/vikrant/trainings/2017/vmware-oct-python/arguments.py


In [308]:
%%file main.py

def hello():
    print("Hello")
    
hello()
Writing main.py
In [309]:
import main
Hello
In [310]:
def func(x):
    print("Hello")
    return x*x
In [311]:
%%file main1.py

def hello():
    print("Hello")
    
print(__name__) 
Writing main1.py
In [312]:
import main1
main1
In [313]:
!python main1.py
__main__
In [314]:
%%file main2.py

def hello():
    print("Hello")
    
if __name__ == "__main__":
    hello()
Writing main2.py
In [315]:
import main2
In [316]:
!python main2.py
Hello
In [317]:
import random
In [318]:
random.choice(['python', "java", "haskell", "lisp"])
Out[318]:
'haskell'

How do I pick up a random word from a sentence

In [319]:
sentence = "some random statement with random words"
In [321]:
random.choice(sentence.split())
Out[321]:
'with'

problem:

  • Write a python module add.py which takes two numbers from commandline and prints addition on standard output.
  • Write a python script echo.py equivalent to unix command echo which echoes text
  • can you add docstring to your functions so that help works on you module
In [323]:
%%file add.py
import sys

def add(x, y):
    """
    docstring of add ... just adds two numbers
    """
    return x+y

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    print(add(a, b))
    
Overwriting add.py
In [324]:
!python add.py 3 4
7
In [326]:
import add
In [327]:
help(add)
Help on module add:

NAME
    add

FUNCTIONS
    add(x, y)
        docstring of add ... just adds two numbers

FILE
    /home/vikrant/trainings/2017/vmware-oct-python/add.py


In [328]:
%%file echo.py
import sys

def echo(x):
    print(x)
    
if __name__ == "__main__":
    s = " ".join(sys.argv[1:])
    echo(s)
Writing echo.py
In [329]:
!python echo.py echo command equivalent
echo command equivalent

Conditional Expressions

In [330]:
1 > 2
Out[330]:
False
In [331]:
1.99 <= 2.0
Out[331]:
True
In [332]:
2 != 3
Out[332]:
True
In [333]:
2 == 2
Out[333]:
True
In [334]:
2 <= 2
Out[334]:
True
In [335]:
"python" > "c++"
Out[335]:
True
In [336]:
"alice" in "alice in wonderland"
Out[336]:
True
In [337]:
"alice" not in "alice in wonderland"
Out[337]:
False
In [338]:
book = "alice in wonderland"
In [339]:
book.endswith("land")
Out[339]:
True
In [340]:
book.startswith("alice")
Out[340]:
True
In [341]:
def is_python_script(filepath):
    return filepath.endswith(".py")
In [342]:
is_python_script("hello.py")
Out[342]:
True
In [343]:
is_python_script("hello.java")
Out[343]:
False
In [344]:
python = [3.4, "if"]
if "if" in python:
    print("Obviously it has if")
elif python[0] > 3:
    print("Python version is 3")
elif True:
    pass
else:
    print("If none of the if or elifs got executed..it will come here")
Obviously it has if
In [345]:
def even(n):
    if n%2 == 0:
        return True
    else:
        return False
In [346]:
even(2)
Out[346]:
True

The conditional after if will be True for

  • nonzero integer value
  • any object that is not None
  • nonempty sequeces like strings, lists, tuples, dictionaries
In [347]:
def even(n):
    return not n%2
In [348]:
even(3)
Out[348]:
False
In [349]:
def odd(n):
    return not even(n)
In [350]:
odd(3)
Out[350]:
True

problem:

  • write a function filetype which takes filename as argument and returns file type.
    type    extension
    python  .py
    text    .txt
    c       .c
    java    .java
>>> filetype("square.py")
python
>>> filetype("hello.txt")
text
  • Write a function minimum (without using built in min) to find minimum from two numbers
    >>> minimum(3,4)
    3
  • Write a function minimum3 to find minimum from given three numbers
    >>> minimum3(1,2,3)
    1
In [351]:
def minimum(x,y):
    if x < y:
        return x
    else:
        return y
In [352]:
minimum(4,5)
Out[352]:
4
In [353]:
def minimum3(x,y,z):
    return minimum(minimum(x,y),z) 
In [354]:
minimum3(4,5,7)
Out[354]:
4
In [355]:
primes = [2,3,5,7,11,13,17]
In [356]:
5 in primes
Out[356]:
True
In [357]:
primes2 = primes
In [358]:
primes is primes2
Out[358]:
True
In [359]:
primes == primes2
Out[359]:
True
In [360]:
primes3 = list(primes)
In [363]:
primes3 == primes # this checks values
Out[363]:
True
In [364]:
primes3 is primes # this checks reference
Out[364]:
False
In [365]:
person = {"name":"robinson",
          "books":["Element", "Finding your element"],
          "TED":"How schools kill creativity"
         }
In [366]:
"name" in person
Out[366]:
True
In [367]:
"robinson" in person
Out[367]:
False
In [368]:
"robinson" in person.values()
Out[368]:
True
In [ ]: