Skip to content
Snippets Groups Projects
Verified Commit 4a2a61d3 authored by Eirik Lorgen Tanberg's avatar Eirik Lorgen Tanberg
Browse files

Legg til LF for ØF1

parent 8252b457
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Øvingsforelesning 1 - TDT4110
%% Cell type:markdown id: tags:
**Læremål**:
* Bli introdusert til hva programmering er
* Grunnleggende syntaks
* Utføre enkle utregninger i Python
%% Cell type:markdown id: tags:
## Oppgave 1
%% Cell type:markdown id: tags:
**Hva tilsvarer 85 grader celsius i Fahrenheit?**
$F = \frac{9}{5} \cdot Celsius + 32$
%% Cell type:code id: tags:
``` python
(9/5) * 85 + 32
```
%% Output
185.0
%% Cell type:markdown id: tags:
## Oppgave 2
%% Cell type:markdown id: tags:
**Regn ut følgende i Python ved hjelp av kode**
$4 \cdot -2-2\left(2+-5\right)$
$-2--2-2$
$5-2^{-1\cdot-1}$
$-(1 \cdot 1 \cdot 2 \cdot 3 \cdot 5 \cdot -8)$
%% Cell type:code id: tags:
``` python
4 * -2 - 2 * (2 + -5)
```
%% Output
-2
%% Cell type:code id: tags:
``` python
(-2) - (-2) - 2
```
%% Output
-2
%% Cell type:code id: tags:
``` python
5 - 2 ** (-1 * -1)
```
%% Output
3
%% Cell type:code id: tags:
``` python
-(1 * 1 * 2 * 3 * 5 * -8)
```
%% Output
240
%% Cell type:markdown id: tags:
## Oppgave 3
%% Cell type:markdown id: tags:
**Er 1000 000 000 større enn $2^{31}$?**
%% Cell type:code id: tags:
``` python
# Dersom dette gir et negativt tall, er 1 mrd mindre enn 2 ** 31
1000000000 - 2 ** 31
```
%% Output
-1147483648
%% Cell type:markdown id: tags:
## Oppgave 4
Hva blir resten når $2^7$ deles på $42$
%% Cell type:code id: tags:
``` python
2 ** 7 % 42
```
%% Output
2
%% Cell type:markdown id: tags:
## Oppgave 5: Input
**Lag et program som gjør følgende:**
1. Tar inn to tall fra bruker
2. Multipliserer dem
3. Skriver ut `(tall 1) * (tall 2) = (resultat)`
%% Cell type:code id: tags:
``` python
tall1 = int(input("Skriv inn et tall: "))
tall2 = int(input("Skriv inn et tall: "))
resultat = tall1 * tall2
print(f"{tall1} * {tall2} = {resultat}")
```
%% Output
10 * 12 = 120
%% Cell type:markdown id: tags:
## Oppgave 6: Innebygde funksjoner
Skriv et program som spør brukeren om to tall og printer absoluttverdien av differansen
<br>
Hint: Bruk den innebygde funksjonen **abs()**
%% Cell type:code id: tags:
``` python
tall1 = int(input("Skriv inn et tall: "))
tall2 = int(input("Skriv inn et tall: "))
resultat = abs(tall1 - tall2)
print(resultat)
```
%% Cell type:markdown id: tags:
## Oppgave 7: Feilretting og variabeltyper
Følgende program skal regne ut og printe summen av prisen på tre varer. Prisene er lagret som forskjellige variabeltyper. Undersøk feilmeldingene og rett koden slik at den skriver ut riktig totalsum.
%% Cell type:code id: tags:
``` python
banan_pris = 10.50
melk_pris = 23.50 # Ta vekk fnuttene for å gjøre dette om fra string til float
havregryn_pris = 15
total_sum = banan_pris + melk_pris + havregryn_pris
print('Totalsummen for varene er ' + str(total_sum)) # Gjør om til str slik at vi kan sette sammen
```
%% Output
Totalsummen for varene er 49.0
%% Cell type:markdown id: tags:
## Oppgave 8: Bruk av variabler
Regn ut volumet av en kjegle med radius 3 og høyde 7.
$V = \frac{\pi r^2 h}{3}$
%% Cell type:code id: tags:
``` python
import math
radius = 3
height = 7
volume = math.pi * radius ** 2 * height / 3
print(volume)
```
%% Output
65.97344572538566
%% Cell type:markdown id: tags:
## Oppgave 9: Variabler og input
1. Endre kjegleprogrammet fra oppgave 6 til å ta inn radius og høyde fra brukeren
2. Verdiene skal være av desimaltall (float)
%% Cell type:code id: tags:
``` python
radius = float(input("Skriv inn radius: "))
height = float(input("Skriv inn height: "))
volume = math.pi * radius ** 2 * height / 3
print(volume)
```
%% Cell type:markdown id: tags:
## Oppgave 10: Sammenligning
Ta inn to tall fra brukeren og skriv ut True om det første tallet er større enn det andre, False dersom ikke
%% Cell type:code id: tags:
``` python
tall1 = float(input("Skriv inn et tall: ")) # Kan velge mellom float og int her
tall2 = float(input("Skriv inn et tall: "))
is_1_larger_than_2 = tall1 > tall2
print(is_1_larger_than_2)
```
%% Cell type:markdown id: tags:
## Oppgave 11 (Ekstraoppgave): Lag en funksjon
Lag en funksjon som kan regne om fra Celsius til Fahrenheit
$F = \frac{9}{5} \cdot Celsius + 32$
%% Cell type:code id: tags:
``` python
def convert_celsius_to_fahrenheit(celsius):
return 9/5 * celsius + 32
```
%% Cell type:markdown id: tags:
## Oppgave 12 (Ekstraoppgave): Plotting
Plot Celsius på x-aksen og Fahrenheit på y-aksen, fra -100 til 100 Celsius (hint: np.linspace).
Bonus: Legg til labels på aksene og en "Legend"
%% Cell type:code id: tags:
``` python
import numpy as np
from matplotlib import pyplot as plt
x_axis = np.linspace(-100, 100)
plt.plot(x_axis, convert_celsius_to_fahrenheit(x_axis), label="Fra vår funksjon")
plt.ylabel("Fahrenheit")
plt.xlabel("Celsius")
plt.legend()
plt.show()
```
%% Output
%% Cell type:markdown id: tags:
## Fritt kodefelt (Du kan bruke dette for å teste egen kode)
%% Cell type:code id: tags:
``` python
# Skriv koden din her
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment