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 cael_samplau(terfan_uchaf=5):
"""
Allbynnu'r hapsamplau o 0 i'r `terfan_uchaf` nes
samplir y `terfan_uchaf`.
"""
samplau = [random.randint(0, terfan_uchaf)]
while samplau[-1] != terfan_uchaf:
samplau.append(random.randint(0, terfan_uchaf))
return samplau
print(cael_samplau(terfan_uchaf=5))
Allbwn?
In [4]:
def cael_samplau(terfan_uchaf=5):
"""
Allbynnu'r hapsamplau o 0 i'r `terfan_uchaf` nes
samplir y `terfan_uchaf`.
"""
samplau = [random.randint(0, terfan_uchaf)]
while samplau[-1] != terfan_uchaf:
samplau.append(random.randint(0, terfan_uchaf))
return samplau
print(cael_samplau(terfan_uchaf=5))
In [5]:
nifer_o_ddyddiau = 12
nifer_o_anrhegion = 0
for dydd in range(nifer_o_ddyddiau + 1):
for anrhegion in range(dydd + 1):
nifer_o_anrhegion += anrhegion
print(nifer_o_anrhegion)
In [6]:
def fibonacci(n):
"""
Allbynnu'r nfed rhif fibonacci gan ddefnyddio
ffwythiant ailadroddol.
"""
if n in [0, 1]:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(5))
class RhifCymhlyg:
"""
Dosbarth ar gyfer rhifau cymhlyg o'r ffurf $a + i b$
"""
def __init__(self, a, b):
self.a = a
self.b = b
def __add__(self, llall):
return RhifCymhlyg(self.a + other.a, self.b + other.b)
def __mul__(self, llall):
return RhifCymhlyg(
self.a * llall.a - self.b * llall.b,
self.b * llall.a + self.a * llall.b
)
def __repr__(self):
return f"{self.a} + {self.b}i"
z1 = RhifCymhlyg(1, -2)
z2 = RhifCymhlyg(51, 0)
print(z1 * z2)
Allbwn?
In [7]:
class RhifCymhlyg:
"""
Dosbarth ar gyfer rhifau cymhlyg o'r ffurf $a + i b$
"""
def __init__(self, a, b):
self.a = a
self.b = b
def __add__(self, llall):
return RhifCymhlyg(self.a + other.a, self.b + other.b)
def __mul__(self, llall):
return RhifCymhlyg(
self.a * llall.a - self.b * llall.b,
self.b * llall.a + self.a * llall.b
)
def __repr__(self):
return f"{self.a} + {self.b}i"
z1 = RhifCymhlyg(1, -2)
z2 = RhifCymhlyg(51, 0)
print(z1 * z2)
In [ ]: