## Rhestr o enghreifftiau bach o god ar gyfer sylfeini Python

Erbyn diwedd y cwrs fe ddylech chi allu:

• Darllen a disgrifio'r hyn maent yn eu gwneud.
• Ysgrifennu enghreifftiau eich hunain.
• Cyfuno'r cysyniadau er mwyn gwneud pethau mwy cymhleth.

Nodwch taw is-set fach o alluoedd Python yw hon

### Aseinio newidynnau a thriniaeth algebraidd

```import math

a = 3
b = a + 1
print(math.sqrt(a ** 2 + b ** 2))
```

Allbwn?

``````
``````
``````

In [1]:

import math
a = 3
b = a + 1
print(math.sqrt(a ** 2 + b ** 2))

``````
``````

5.0

``````

```import random

n = random.randint(0, 5)
if n == 4:
print(n)
else:
print(-1)
```

Allbwn?

``````
``````
``````

In [2]:

import random
n = random.randint(0, 5)
if n == 4:
print(n)
else:
print(-1)

``````
``````

-1

``````

```import random

n = random.randint(0, 5)
number_of_samples = 1
while n != 4:
number_of_samples += 1
n = random.randint(0, 5)
```

Allbwn?

``````
``````
``````

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)

``````
``````

2

``````

### Ffwythiannau a rhestrau

```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))

``````
``````

[0, 4, 1, 3, 1, 4, 4, 4, 5]

``````

### Iteru

```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)
```

Allbwn?

``````
``````
``````

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)

``````
``````

364

``````

```def fibonacci(n):
"""
Allbynnu'r nfed rhif fibonacci gan ddefnyddio
"""
if n in [0, 1]:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(5))
```

Allbwn?

``````
``````
``````

In [6]:

def fibonacci(n):
"""
Allbynnu'r nfed rhif fibonacci gan ddefnyddio
"""
if n in [0, 1]:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(5))

``````
``````

8

``````

```class RhifCymhlyg:
"""
Dosbarth ar gyfer rhifau cymhlyg o'r ffurf \$a + i b\$
"""
def __init__(self, a, b):
self.a = a
self.b = b

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

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)

``````
``````

51 + -102i

``````
``````

In [ ]:

``````