# Declare an integer
myint = 10
Hvadsomhelst = 5
myint
# Declare a float
myfloat = 1.5
myfloat
myint + 1 # addition
myint * 2 # multiplication by int
myint * 1.0 # multiplication by float
2**3 # raising to the power of 3
2**3.0 # raising to the power of 3.0
int(1.1) # make float into an integer
int(1.5)
int(1.9)
int(-2.5) # what do you think happens here?
# Strings
mystring = "Hvad sker der her?" # Use strings to store strings of letters
mystring
print("This is an int", myint)
print("This is a float", myfloat)
print(mystring)
myint
print("This is an int", myint, ". This is a float", myfloat)
string = f'This is an int {myint}. This is a float {myfloat}'
print(string)
string = f'This is an int {myint:2d}. This is a float {myfloat:7.3f}'
print(string)
for i in range(15):
j = i**(1/2)
print(f'Lagkage {j:7.5f}')
string_percentsign = "This is an int %2d, This is a float %7.3f" % (myint, myfloat)
string_format = "This is an int {0:2d}, This is a float {1:7.3f}".format(myint, myfloat)
string_f = f'This is an int {myint:2d}. This is a float {myfloat:7.3f}'
print(string_percentsign)
print(string_format)
print(string_f)
print("Line \n new line starts here \t and is tabulated")
print("") # Print an empty line
string = f'This is an int \t\t{myint}. \nThis is a float \t{myfloat}'
print(string)
# Skriv din kode for øvelsen nedenfor her i blokken...
x = 2
y = 8
print("2 opløftet i 8 er:", 2**8)
simplelist = [0, 1.0, "to", "3", "π"]
simplelist[0] = 1 # Assign the second entry to 42.0.
simplelist
simplelist.append(1.0)
simplelist.append(5.0)
simplelist.append("test")
simplelist
simplelist[-3]
alotofzeros = [0]*10
alotofzeros
import numpy as np
liste = np.array([-4., 21., 10., 4., 1/5.])
liste = np.append(liste, 128)
print(liste)
liste + 1
liste * 5
np.sqrt(liste)
np.arange(10)
np.linspace(0, 5, 10+1) # can you see why we need a +1 here?
np.zeros(10)
np.ones(10000)
# Skriv din kode for øvelsen nedenfor her i blokken...
# This is a simple loop
for i in range(10) :
# This is the block within the loop
print(f"Counting to ten: {i}")
for i in range(4, 10):
print(f"Counting from four to ten: {i}")
for i in range(4, 10, 2):
print(f"Counting every second number from four to ten: {i}")
for val in simplelist:
print(val)
for index, val in enumerate(simplelist):
print(f'index: {index}, value: {val}')
# Skriv din kode for øvelsen nedenfor her i blokken...
anint = 3
afloat = 14.0
astring = "string"
if anint == 3:
print("The integer value is three ")
if afloat > 1000.0:
print("The float value is greater than 1000")
elif 100.0 < afloat < 500.0:
print("The float value is somewhere between 100. and 500.")
else:
print("The float is neither > 1000 nor in the range [100,500]")
if astring == "string":
print(f"'astring' says {astring}")
if astring != "blah":
print("'astring' does not say 'blah'")
square_numbers = [] # make empty list
for i in range(10):
square_numbers.append(i**2)
square_numbers
# Skriv din kode for øvelsen nedenfor her i blokken...
def sqr(a):
return a**2 # or a*a
square_numbers = [] # make empty list
for i in range(10):
square_numbers.append(sqr(i))
square_numbers
numbers = np.arange(10)
square_numbers = sqr(numbers)
square_numbers
# Skriv din kode for øvelsen nedenfor her i blokken...
def sqrt(a):
return a**0.5