0% found this document useful (0 votes)
14 views

Py

Uploaded by

sassuologool
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Py

Uploaded by

sassuologool
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Python isn't smart enough to read my code and turn it into a nice English

description. However, when I write a function, I can provide a description in


what's called the docstring.

Docstrings
def least_difference(a, b, c):
"""Return the smallest difference between any two numbers
among a, b and c.

>>> least_difference(1, 5, -5)


4
"""
diff1 = abs(a - b)
diff2 = abs(b - c)
diff3 = abs(a - c)
return min(diff1, diff2, diff3)

When we called help(print), we saw that the print function has several optional
arguments. For example, we can specify a value for sep to put some special string
in between our printed arguments:

print(1, 2, 3, sep=' < ')


1 < 2 < 3
But if we don't specify a value, sep is treated as having a default value of ' ' (a
single space).

By default, max returns the largest of its arguments. But if we pass in a function
using the optional key argument, it returns the argument x that maximizes key(x)
(aka the 'argmax').

def mod_5(x):
"""Return the remainder of x after dividing by 5"""
return x % 5

print(
'Which number is biggest?',
max(100, 51, 14),
'Which number is the biggest modulo 5?',
max(100, 51, 14, key=mod_5),
sep='\n',
)
Which number is biggest?
100
Which number is the biggest modulo 5?
14
As you've seen, ndigits=-1 rounds to the nearest 10, ndigits=-2 rounds to the
nearest 100 and so on. Where might this be useful? Suppose we're dealing with large
numbers:

The area of Finland is 338,424 km²


The area of Greenland is 2,166,086 km²

We probably don't care whether it's really 338,424, or 338,425, or 338,177. All
those digits of accuracy are just distracting. We can chop them off by calling
round() with ndigits=-3:

The area of Finland is 338,000 km²


The area of Greenland is 2,166,000 km²

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus',


'Neptune']

What are the first three planets? We can answer this question using slicing:

planets[0:3]
['Mercury', 'Venus', 'Earth']

planets[:3]
['Mercury', 'Venus', 'Earth']

planets[3:]
['Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

# All the planets except the first and last


planets[1:-1]
['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus']

# The last 3 planets


planets[-3:]
['Saturn', 'Uranus', 'Neptune']

Let's compensate by shortening the names of the first 3 planets.

planets[:3] = ['Mur', 'Vee', 'Ur']


print(planets)
['Mur', 'Vee', 'Ur', 'Malacandra', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
len gives the length of a list

sorted returns a sorted version of a list:

# The planets sorted in alphabetical order


sorted(planets)
['Earth', 'Jupiter', 'Mars', 'Mercury', 'Neptune', 'Saturn', 'Uranus', 'Venus']

sum(primes)

max(primes)

list.append modifies a list by adding an item to the end

list.pop removes and returns the last element of a list

We can get its index using the list.index method.

we can use the in operator to determine whether a list contains a particular value:

# Is Earth a planet?
"Earth" in planets
True

Tuples are almost exactly the same as lists. They differ in just two ways.

1: The syntax for creating them uses parentheses instead of square brackets

t = (1, 2, 3)

2: They cannot be modified (they are immutable).

Tuples are often used for functions that have multiple return values.

For example, the as_integer_ratio() method of float objects returns a numerator and
a denominator in the form of a tuple:

x = 0.125
x.as_integer_ratio()
(1, 8)

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus',


'Neptune']
for planet in planets:
print(planet, end=' ') # print all on same line
Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune

We can fix this by "escaping" the single quote with a backslash.

'Pluto\'s a planet!'
"Pluto's a planet!"

In addition, Python's triple quote syntax for strings lets us include newlines
literally (i.e. by just hitting 'Enter' on our keyboard, rather than using the
special '\n' sequence). We've already seen this in the docstrings we use to
document our functions, but we can use them anywhere we want to define a string.

triplequoted_hello = """hello
world"""
print(triplequoted_hello)
triplequoted_hello == hello
hello
world
True

But a major way in which they differ from lists is that they are immutable. We
can't modify them.

planet[0] = 'B'
# planet.append doesn't work either
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_19/2683731249.py in <module>
----> 1 planet[0] = 'B'
2 # planet.append doesn't work either

TypeError: 'str' object does not support item assignment

# Searching for the first index of a substring


claim.index('plan')
11

claim = "Pluto is a planet!"


claim.startswith(planet)
True
# false because of missing exclamation mark
claim.endswith('planet')
False

datestr = '1956-01-31'
year, month, day = datestr.split('-')
str.join() takes us in the other direction, sewing a list of strings up into one
long string, using the string it was called on as a separator.

'/'.join([month, day, year])


'01/31/1956'

# Yes, we can put unicode characters right in our string literals :)


' 👏 '.join([word.upper() for word in words])
'PLUTO 👏 IS 👏 A 👏 PLANET!'

position = 9
"{}, you'll always be the {}th planet to me.".format(planet, position)
"Pluto, you'll always be the 9th planet to me."

pluto_mass = 1.303 * 10**22


earth_mass = 5.9722 * 10**24
population = 52910390
# 2 decimal points 3 decimal points, format as percent separate with
commas
"{} weighs about {:.2} kilograms ({:.3%} of Earth's mass). It is home to {:,}
Plutonians.".format(
planet, pluto_mass, pluto_mass / earth_mass, population,
)
"Pluto weighs about 1.3e+22 kilograms (0.218% of Earth's mass). It is home to
52,910,390 Plutonians."

# Referring to format() arguments by index, starting from 0


s = """Pluto's a {0}.
No, it's a {1}.
{0}!
{1}!""".format('planet', 'dwarf planet')
print(s)
Pluto's a planet.
No, it's a dwarf planet.
planet!
dwarf planet!

Dictionaries are a built-in Python data structure for mapping keys to values.
numbers = {'one':1, 'two':2, 'three':3}

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus',


'Neptune']
planet_to_initial = {planet: planet[0] for planet in planets}
planet_to_initial
{'Mercury': 'M',
'Venus': 'V',
'Earth': 'E',
'Mars': 'M',
'Jupiter': 'J',
'Saturn': 'S',
'Uranus': 'U',
'Neptune': 'N'}

We can access a collection of all the keys or all the values with dict.keys() and
dict.values(), respectively.

The very useful dict.items() method lets us iterate over the keys and values of a
dictionary simultaneously. (In Python jargon, an item refers to a key, value pair)

for planet, initial in planet_to_initial.items():


print("{} begins with \"{}\"".format(planet.rjust(10), initial))
Mercury begins with "M"
Venus begins with "V"
Earth begins with "E"
Mars begins with "M"
Jupiter begins with "J"
Saturn begins with "S"
Uranus begins with "U"
Neptune begins with "N"

The as simply renames the imported module

import math as mt
mt.pi

Wouldn't it be great if we could refer to all the variables in the math module by
themselves? i.e. if we could just refer to pi instead of math.pi or mt.pi? Good
news: we can do that.

from math import *


print(pi, log(32, 2))
ravel(...) method of numpy.ndarray instance
a.ravel([order])

Return a flattened array.

Refer to `numpy.ravel` for full documentation.

See Also
--------
numpy.ravel : equivalent function

ndarray.flat : a flat iterator on the array.

rolls + 10
array([13, 14, 13, 14, 15, 15, 12, 11, 13, 13])

# At which indices are the dice less than or equal to 3?


rolls <= 3
array([ True, False, True, False, False, False, True, True, True,
True])

When does 1 + 1 not equal 2?


Things can get weirder than this. You may have heard of (or even used) tensorflow,
a Python library popularly used for deep learning. It makes extensive use of
operator overloading.

import tensorflow as tf
# Create two constants, each with value 1
a = tf.constant(1)
b = tf.constant(1)
# Add them together to get...
a + b
<tf.Tensor: shape=(), dtype=int32, numpy=2>

a + b isn't 2, it is (to quote tensorflow's documentation)...

a symbolic handle to one of the outputs of an Operation. It does not hold the
values of that operation's output, but instead provides a means of computing those
values in a TensorFlow tf.Session.

It's important just to be aware of the fact that this sort of thing is possible and
that libraries will often use operator overloading in non-obvious or magical-
seeming ways.

Understanding how Python's operators work when applied to ints, strings, and lists
is no guarantee that you'll be able to immediately understand what they do when
applied to a tensorflow Tensor, or a numpy ndarray, or a pandas DataFrame.

Once you've had a little taste of DataFrames, for example, an expression like the
one below starts to look appealingly intuitive:

# Get the rows with population over 1m in South America


df[(df['population'] > 10**6) & (df['continent'] == 'South America')]
But why does it work? The example above features something like 5 different
overloaded operators. What's each of those operations doing? It can help to know
the answer when things start going wrong.

You might also like