Forelesning 1: Innledning Til Programmering I Python. Programmeringsmiljøer For Python: Spyder Og Jupyter Notebook

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Forelesning 1: Innledning til programmering i Python.

Programmeringsmiljøer for Python: Spyder og Jupyter Notebook.

Ctrl + : makes the texts bigger in the screen

“””: block comments

#: inline comments

- Difference in the big and small letters when it comes to the variable names.
- No difference in using the space in the command.
- F5: kjører koden i hele filen
- Omregne fra .txt til .py: in the browsing box to save the file, write .py and the whole name in
anførselstegn “”.
- To open a file with the given path in Python, Ctr O, paste the link in the Filename box

- Necessary to install a package and import it in the code when using it (e.g numpy,..)

- If a code is too long, then we can put the mouse right after the comma and enter -> the first
letter of the remaining arguments will go in vertical line with the previous arguments in the
code.

- In order to just see the results of a specific line, bold that line and F9, then you can see the
corresponding result in console.

- Å kommentere vekk kode bruk # -> kjøre kode uten å slette kode eller Ctr1

- # %%: markere kode celle med en separat ramme

- View >> Panes >> Outline

- Jupyter Notebook >> New >> Python 3: Write code cells. If want to write texts -> Cell >> Cell
Type >> Markdown

- To go back to writing codes -> Shift Enter

- To see if a package numpy is installed -> Anaconda prompt >> pip show numpy

- To see all the installed packages >> pip list

- To install a package pygame >> pip install pygame

- Syntax in Python is not capitalized.

Forelesning 2: Variabler og datatyper.


- Ctrl + L: delete all in Console
- Ctr+Enter: Run the marked codes
- ‘Reset’ in Console: delete all of the information we have stored in consoled so far
- Check type of a variable: type()
- 2**3 = 2^3
- Kan add texts together by +:
Text1 =’Kake er godt’
Text2=’synes jeg’
Text1+Text2
- List can contain any types of data, even another list. list index counting from position 0.
- How we can index List in list: print(list[4][1])
- Tuple cannot be replaced the values like list
- A list*2 -> double length but not double the element in magnitude
- Array*2-> double the elements in magnitude
- == (equal), !=(unequal)
- Ctrl + Enter: run the codes in a specific frame.

Floating point values have the f suffix. We can also specify the precision: the number of decimal
places. The precision is a value that goes right after the dot character.

The example prints a formatted floating point value.

$ python format_floats.py

12.30

12.30000

"\t" is a tab, "\n" is a newline.

Difference between list and array:

- An array data structure belongs to the "must-import" category. To use an array in Python,
you'll need to import this data structure from the NumPy package or the array module.
- An array is also a data structure that stores a collection of items. Like lists, arrays are
ordered, mutable, enclosed in square brackets, and able to store non-unique items. But
when it comes to the array's ability to store different data types, the answer is not as
straightforward. It depends on the kind of array used. To use arrays in Python, you need to
import either an array module or a NumPy package.

- Arrays need to be declared. Lists don't, since they are built into Python. Lists are created by
simply enclosing a sequence of elements into square brackets. Creating an array, on the
other hand, requires a specific function from either the array module (i.e., array.array()) or
NumPy package (i.e., numpy.array()). Because of this, lists are used more often than arrays.
- Arrays can store data very compactly and are more efficient for storing large amounts of
data.
- Arrays are great for numerical operations; lists cannot directly handle math operations. For
example, you can divide each element of an array by the same number with just one line of
code. If you try the same with a list, you'll get an error.

- It's possible to do a mathematical operation with a list, but it's much less efficient:
In, Python += adds another value with the variable's value and assigns the new value to
the variable

Forelesning 3: Plotting, funksjoner, testing av egen kode


Når du bruker plt.savefig()-funksjonen til å lagre en plott-figur som en grafikkfil, f.eks. en pdf-fil, må
plt.savefig() plasseres foran (ovenfor) plt.show() i programmet. Hvis plt.savefig() plasseres etter
plt.show(), blir den resulterende grafikkfilen tom.

A function can be called in another function.

Regular mistakes: wrong name of function, long codes without testing small portions first for easier
troubleshooting

Good practice: use print function in Funksjoner so that we know that variables are attached the right
values.

A variable defined inside a function is local to it. When the function ends, this variable is destroyed.

Variables defined outside a function are called global variables.


Inside functions, the “global” keyword can be used to change the value of a variable in the global
scope.

Using the global keyword is a bad practice. Should try to avoid

https://www.programiz.com/python-programming/global-local-nonlocal-variables

Forelesning 4: Betingelse (if-else), Itererte programløp med for- og


while-løkker

True, False in Python has to be with the capital beginning letter ( not true, false)

Løkker er nyttige i arrayer og lister for optimering og simulering av dynamiske systemer.

For-løkke: Antall iterasjoner er forhåndsbestemt.

(S += S) is (S = S + S)

While-løkke: Antall iterasjoner er ikke forhåndsbestemt. Iterasjoner gjentas så lenge fortsett-


betingelsen er True.

You might also like