In [1]:
import math
a = 3
b = a + 1
print(math.sqrt(a ** 2 + b ** 2))
In [2]:
import random
n = random.randint(0, 5)
if n == 4:
print(n)
else:
print(-1)
In [3]:
import random
n = random.randint(0, 5)
number_of_samples = 1
while n != 5:
number_of_samples += 1
n = random.randint(0, 5)
print(number_of_samples)
import random
def obtain_samples(upper_limit=5):
"""
Returns the random samples from 0 till
`upper_limit` until the `upper_limit` is sampled
"""
samples = [random.randint(0, upper_limit)]
while samples[-1] != upper_limit:
samples.append(random.randint(0, upper_limit))
return samples
print(obtain_samples(upper_limit=5))
Output?
In [4]:
def obtain_samples(upper_limit=5):
"""
Returns the random samples from 0 till
`upper_limit` until the `upper_limit` is sampled
"""
samples = [random.randint(0, upper_limit)]
while samples[-1] != upper_limit:
samples.append(random.randint(0, upper_limit))
return samples
print(obtain_samples(upper_limit=5))
In [5]:
number_of_days = 12
number_of_gifts = 0
for day in range(number_of_days + 1):
for gifts in range(day + 1):
number_of_gifts += gifts
print(number_of_gifts)
In [6]:
def fibonacci(n):
"""
Returns the nth fibonacci number using recursion.
"""
if n in [0, 1]:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(5))
class ImaginaryNumber:
"""
A class for imaginary numbers of the form $a + i b$
"""
def __init__(self, a, b):
self.a = a
self.b = b
def __add__(self, other):
return ImaginaryNumber(self.a + other.a, self.b + other.b)
def __mul__(self, other):
return ImaginaryNumber(
self.a * other.a - self.b * other.b,
self.b * other.a + self.a * other.b
)
def __repr__(self):
return f"{self.a} + {self.b}i"
z1 = ImaginaryNumber(1, -2)
z2 = ImaginaryNumber(51, 0)
print(z1 * z2)
Output?
In [7]:
class ImaginaryNumber:
"""
A class for imaginary numbers of the form $a + i b$
"""
def __init__(self, a, b):
self.a = a
self.b = b
def __add__(self, other):
return ImaginaryNumber(self.a + other.a, self.b + other.b)
def __mul__(self, other):
return ImaginaryNumber(
self.a * other.a - self.b * other.b,
self.b * other.a + self.a * other.b
)
def __repr__(self):
return f"{self.a} + {self.b}i"
z1 = ImaginaryNumber(1, -2)
z2 = ImaginaryNumber(51, 0)
print(z1 * z2)