Python and Real Numbers¶

When you use float you operate using 64 bits:

• 1 bit is the sign of a number;
• 52 bits are used for the mantissa (representing the digits of the number itself. 52 binary digits roughly equal 15-16 decimal digits of precision);
• 11 bits are used for the exponent (about from $2^{-1000}$ to $2^{1000}$).

M☉ = $(1.98847±0.00007)$ × $10^{30}$kg

mantissa: 198847, exponent: $10^{30}$

The point separating the fractional part from the integer part starts between the first and the second digit in this example. So, a number with exponent $10^{0}$ and mantissa 198847 would be 1.98847.

In [ ]:
solar_mass = 1e-8
print(solar_mass==1.98947*10**30)

In [ ]:
1e+308


Problems¶

In [ ]:
1.989E30 == 1.989*10**30

In [ ]:
(0.2+0.1) == (0.5-0.2)
print('{:.25}'.format(0.5-0.2))
print('{:.25}'.format(0.3))

In [ ]:
(0.2+0.1),(0.5-0.2)

In [ ]:
'{0:.50}'.format(0.1)

• Any real number in Python can be represented as a fraction where an integer is stored in the numerator, and in the denominator there is some power of two;
• If you can do without the use of real numbers you need to do this. Real numbers are problematic, slow and inaccurate .

Remember:

$(X+\epsilon)\cdot(Y+\epsilon) = XY +(X+Y)\cdot\epsilon+{\epsilon}^2$, where $\epsilon$ is an error of number storage and it is a negligible number.

An illustration of how fractions work in binary: https://en.wikipedia.org/wiki/Hexadecimal

In [ ]:
import math

In [ ]:
math.floor(0.2),math.ceil(0.2)

In [ ]:
math.pow(0.1,2)

In [ ]:
from math import pi,e,sin

In [ ]:
print("Pi number:",pi,"\nEuler's number:",e)

In [ ]:
math.cos(pi/2)

In [ ]:
sin(pi/2)

In [ ]:
from math import *

In [ ]:
print("Cosinus:",cos(0),"\nLog2:",log2(32),"\nSquare root:",sqrt(1024))

In [ ]:
from decimal import Decimal

In [ ]:
print(Decimal('0.2')+Decimal("0.1"))

In [ ]:
print(Decimal(0.2)+Decimal("0.1"))

In [ ]:
a = Decimal('0.2')

In [ ]:
a + a  # possible
a + 0.2  # unsupported

In [ ]:
print(Decimal('0.1000')+Decimal('0.20000002')) # number

In [ ]:
from decimal import getcontext

In [ ]:
getcontext()

In [ ]:
Decimal('0.154')+Decimal('0.14')

In [ ]:
(Decimal('0.154')+Decimal('0.14')).quantize(Decimal("1.00"))

In [ ]:
getcontext().prec = 2

In [ ]:
Decimal('0.155')+Decimal('0.14')

In [ ]:
from decimal import ROUND_CEILING,ROUND_DOWN, ROUND_HALF_EVEN

In [ ]:
getcontext().prec = 3

b = Decimal('0.154')+Decimal('0.14')
b

In [ ]:
b.quantize(Decimal('.00'), rounding=ROUND_DOWN)

In [ ]:
b.quantize(Decimal('.00'), rounding=ROUND_CEILING)

In [ ]:
b.quantize(Decimal('.00'), rounding=ROUND_HALF_EVEN)


Fractions package¶

https://docs.python.org/3/library/fractions.html

In [ ]:
from fractions import Fraction

In [ ]:
from math import sqrt

In [ ]:
?sqrt

In [ ]:
print(Fraction(1,3))

In [ ]:
print(Fraction(1,3) + Fraction(7,3))
print(Fraction(1,4) * Fraction(2,3))
print(Fraction(3,4) / 3)
print(Fraction(7,4) % 1)
print(Fraction(1,4) ** 2)

In [ ]:
print(Fraction(pi), 884279719003555/281474976710656, pi)

In [ ]:
print(Fraction(pi).limit_denominator(max_denominator=100), 311/99, pi)

In [ ]:
Fraction(Decimal('0.1')+Decimal('0.2'))

In [ ]:
from fractions import gcd

In [ ]:
gcd(9, 3)

In [ ]:
gcd(2, 3)

In [ ]:
gcd(10, 5)

In [ ]:
gcd(0, 0)


String Sections¶

In [ ]:
string_section = 'an_example_of_some_string'

In [ ]:
string_section[0],string_section[-5]

In [ ]:
string_section[:4],string_section[5:7],string_section[:]

In [ ]:
string_section[0:10:1],string_section[::2],string_section[6:2:-2],string_section[2:6:-2]

In [ ]:
print(string_section[0:10:1])
print('0123456789')

In [ ]:
string_section[::-1]

In [ ]:
string_methods = 'an_example_of_some_string_2'

In [ ]:
string_methods. # [press tab]

In [ ]:
string_methods.upper()

In [ ]:
string_methods.count("e"),string_methods.count("q")

In [ ]:
print(string_methods.find("s"),string_methods.rfind("s"),string_methods.find("qwerty"))
print(string_methods[14])

In [ ]:
print(string_methods.rfind("o", 1, 11))

In [ ]:
string_methods.replace('_','####')

In [ ]:
string_methods.split("_")

In [ ]:
inputs = "1 2 3 4"
inputs.split()


Example problem¶

You can join a Zoom conference in two ways: by a link and by the ID.

A conference link, however, contains the ID in a specific format.

Write a program that gets a link to a Zoom conference and outputs the conference ID.

In this example there are always 9 numbers in the ID and they are split into three sections of 3 numbers each.

Example:
https://us04web.zoom.us/j/123456789?pwd=bkN3djZmYlZ5MStFMWdqdmphSGFVdz09

Output data: ID
123 456 789

The link is always in the format https://us04web.zoom.us/j/XXXXXXXXX?pwd=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY, the only things that change are the ID and the password (including password's length). You don't have to process cases with incorrect links.

In [ ]:
# textlink = input()

# first_index =
# second_index =

# use methods "find" and "rfind" to find the indices of the start and the end of the ID in the string


Convert a link from the first format to the second one

In [ ]:
https://drive.google.com/file/d/1kazyfB4JHoZSmczN-FBVXB4C8qN5b46G/view?usp=sharing: