Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
talit:python_basics [2020-02-09 21:34] scatalit:python_basics [2020-02-09 21:42] (aktuell) sca
Zeile 57: Zeile 57:
 </code> </code>
  
-Note that you can also write a number as a string, e.g., `{s = '5'`.+Note that you can also write a number as a string, e.g., `s = '5'`.
 However, `s` will be treated as text and not as a number, therefore, you cannot do any calculations with it. However, `s` will be treated as text and not as a number, therefore, you cannot do any calculations with it.
 Also, you can use variables with more than one character, e.g., `mystring = "Hello World"`, but you are not allowed to use blanks in variable names. Also, you can use variables with more than one character, e.g., `mystring = "Hello World"`, but you are not allowed to use blanks in variable names.
Zeile 93: Zeile 93:
  
 1. Can you use numbers or special characters like `?,!,.,\%,-` or `_` in variable names? Give it a try! 1. Can you use numbers or special characters like `?,!,.,\%,-` or `_` in variable names? Give it a try!
-<!-- You can even use numbers in variable names, as long as they are not in the beginning, e.g., `mystring13 = "Hello World"` 
-But you can NOT use special characters in variable names 
---> 
 2. How can you add a line break to a string? 2. How can you add a line break to a string?
-<!-- % mystring = "Hello\nWorld" --> 
 3. Is Python case-sensitive? 3. Is Python case-sensitive?
 4. Given a (long enough) string `s`, what do the commands 4. Given a (long enough) string `s`, what do the commands
Zeile 106: Zeile 102:
  
 ## Integers & floats ## Integers & floats
-\label{sec integers and floats} 
  
 Next, let's have a look at numbers, in particular integers and floats. Next, let's have a look at numbers, in particular integers and floats.
Zeile 295: Zeile 290:
  
  
-# Conditions \& Booleans+# Conditions & Booleans
  
 We can check if one or multiple conditions are satisfied. We can check if one or multiple conditions are satisfied.
Zeile 334: Zeile 329:
 Some important conditions are: Some important conditions are:
  
-   {\codetxt a == b}: a and b are equal +   `a == b`: a and b are equal 
-   {\codetxt a > b}: a is larger b (similarly {\codetxt a < b}+   `a > b`: a is larger b (similarly `a < b`
-   {\codetxt a >= b}: a is larger equal b (similarly {\codetxt a <= b}+   `a >= b`: a is larger equal b (similarly `a <= b`
-   {\codetxt a != b}: a and b are not equal +   `a != b`: a and b are not equal 
-   {\codetxt a in A}: a is element of list A  +   `a in A`: a is element of list A  
-   {\codetxt a not in A}: a is not element of list A +   `a not in A`: a is not element of list A 
  
  
Zeile 346: Zeile 341:
 # If statements # If statements
  
-In an `ifstatement, the action depends on conditions.+In an `ifstatement, the action depends on conditions.
 Let's say we are given some number $a$ and want to determine its absolute value. It is given by $a$ if $a \geq 0$ and by $-a$ if $a < 0$. Let's say we are given some number $a$ and want to determine its absolute value. It is given by $a$ if $a \geq 0$ and by $-a$ if $a < 0$.
 We can realize this by We can realize this by
Zeile 359: Zeile 354:
 In the first line, we just choose some number. In the first line, we just choose some number.
 Feel free to play around with its value. Feel free to play around with its value.
-In line 3, the `ifstatement begins. +In line 3, the `ifstatement begins. 
-It starts with {\codetxt ifand is followed by a condition, here {\codetxt a < 0}, and a mandatory colon. +It starts with `ifand is followed by a condition, here `a < 0`, and a mandatory colon. 
-If the condition is {\codetxt True(satisfied), i.e., if $a < 0$, it runs the commands that follow, here {\codetxt print(-a)}+If the condition is `True(satisfied), i.e., if $a < 0$, it runs the commands that follow, here `print(-a)`
-If the condition is {\codetxt False(not satisfied), it jumps to {\codetxt else:and runs what is written there: {\codetxt print(a)}.+If the condition is `False(not satisfied), it jumps to `else:and runs what is written there: `print(a)`.
 Also, here it is important to do the indentation correctly. Also, here it is important to do the indentation correctly.
  
-If there are more than two possibilities, we can add {\codetxt elifstatements as in the following example:+If there are more than two possibilities, we can add `elifstatements as in the following example:
 <code python> <code python>
 a = 4. a = 4.
Zeile 377: Zeile 372:
 </code> </code>
 As you can see, a condition can consist of multiple sub-conditions. As you can see, a condition can consist of multiple sub-conditions.
-These can be connected, e.g., using {\codetxt andor {\codetxt or}:+These can be connected, e.g., using `andor `or`:
  
-   {\codetxt condition1 and condition2}: is true only if *both* conditions are true +   `condition1 and condition2`: is true only if *both* conditions are true 
-   {\codetxt condition1 or condition2}: is true if one or both conditions are true+   `condition1 or condition2`: is true if one or both conditions are true
  
  
Zeile 414: Zeile 409:
  
 Second, we discuss the **while-loop**. It repeats a piece of code as long as a condition is satisfied.  Second, we discuss the **while-loop**. It repeats a piece of code as long as a condition is satisfied. 
-Careful: If you have a bug in your code and the condition is always true, you have an `infinite loop': the code will never stop running and you will have to kill the process manually.+Careful: If you have a bug in your code and the condition is always true, you have an **infinite loop**: the code will never stop running and you will have to kill the process manually.
  
 A simple example of a while-loop is: A simple example of a while-loop is:
Zeile 425: Zeile 420:
 </code> </code>
 This piece of code has exactly the same output as the for-loop above. This piece of code has exactly the same output as the for-loop above.
-In the first line, we set {\codetxt i = 0}. %and we will use $i$ as a running index+In the first line, we set `i = 0`
-Then, in line 3, we say that we want to run the following code as long as the condition {\codetxt i < 4is true.+Then, in line 3, we say that we want to run the following code as long as the condition `i < 4is true.
 In line 4, we print the current value of the index. In line 4, we print the current value of the index.
 Very important is the last line: Here, we increase the value of $i$ by $1$ for the next run. Very important is the last line: Here, we increase the value of $i$ by $1$ for the next run.
Zeile 441: Zeile 436:
  
 There are many additional packages that can be imported. There are many additional packages that can be imported.
-For doing math, `numpyis particular useful.+For doing math, `numpyis particular useful.
 For example, it contains a lot of predefined functions like trigonometric functions. For example, it contains a lot of predefined functions like trigonometric functions.
 Let us do an example Let us do an example
Zeile 450: Zeile 445:
 print(np.pi) print(np.pi)
 </code> </code>
-In the first line, we import the `numpypackage and give it the short name `np'. Of course, you could use a different name than `npbut this is the common choice. +In the first line, we import the `numpypackage and give it the short name `np`. Of course, you could use a different name than `npbut this is the common choice. 
-Each time we call a function (or something else) from numpy, we have to start with `np.', in order for Python to know that it has to look inside the numpy package. This is exactly what we do on line 3: We call the sine function defined in numpy and evaluate it for the argument $2$. In the last line, we print the value of the constant $\pi$.+Each time we call a function (or something else) from numpy, we have to start with `np.`, in order for Python to know that it has to look inside the numpy package. This is exactly what we do on line 3: We call the sine function defined in numpy and evaluate it for the argument $2$. In the last line, we print the value of the constant $\pi$.
  
 **Questions:** **Questions:**
Zeile 457: Zeile 452:
    * From trigonometry, you (should) remember the relation $\sin^2x + \cos^2x = 1$, holding for any $x \el \R$. Show explicitly that this relation holds different values for $x$.    * From trigonometry, you (should) remember the relation $\sin^2x + \cos^2x = 1$, holding for any $x \el \R$. Show explicitly that this relation holds different values for $x$.
    * Two very useful functions within numpy are:\\    * Two very useful functions within numpy are:\\
-{\codetxt np.linspace(a,b,c)and {\codetxt np.arange(a,b,c)}\\+`np.linspace(a,b,c)and `np.arange(a,b,c)`\\
 Find out what they do. Find out what they do.
 \een \een
Zeile 463: Zeile 458:
  
 # Elapsed time # Elapsed time
-\label{sec elapsed time} 
  
 If you want to test the efficiency of your code, it is useful to measure the time needed for the calculation: If you want to test the efficiency of your code, it is useful to measure the time needed for the calculation:
  • talit/python_basics.1581284085.txt.gz
  • Zuletzt geändert: 2020-02-09 21:34
  • von sca