Skip to content
Snippets Groups Projects
Commit 22606cb3 authored by guiwitz's avatar guiwitz
Browse files

remove sols

parent dde36591
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# 1. Python Basics solutions
%% Cell type:markdown id: tags:
1. Create a variable var1 which is of type int
%% Cell type:code id: tags:
``` python
var1 = 3
```
%% Cell type:markdown id: tags:
2. Create a variable var2 which is of type float
%% Cell type:code id: tags:
``` python
var2 = 5.4
```
%% Cell type:markdown id: tags:
3. Create a new variable var3 by multiplying those two variables
%% Cell type:code id: tags:
``` python
var3 = var1 * var2
```
%% Cell type:markdown id: tags:
4. What is the type of the new variable ?
%% Cell type:code id: tags:
``` python
print(var3)
```
%% Output
16.200000000000003
%% Cell type:code id: tags:
``` python
type(var3)
```
%% Output
float
%% Cell type:markdown id: tags:
5. Compare var1 and var2 with an operation of your choosing and store the result in a new variable var4
%% Cell type:code id: tags:
``` python
var4 = var2 > var3
print(var4)
```
%% Output
False
%% Cell type:markdown id: tags:
6. What is the type of var4 ?
%% Cell type:code id: tags:
``` python
type(var4)
```
%% Output
bool
%% Cell type:markdown id: tags:
7. Compare var3 and var2 with an opertion of your choosing and store the result in var5
%% Cell type:code id: tags:
``` python
var5 = var2 < var3
print(var5)
```
%% Output
True
%% Cell type:markdown id: tags:
8. Try different combinations of var4 and var5 using and/or/not
%% Cell type:code id: tags:
``` python
var4 and var5
```
%% Output
False
%% Cell type:code id: tags:
``` python
var4 or var5
```
%% Output
True
%% Cell type:markdown id: tags:
# 2. Numpy basics solutions
%% Cell type:markdown id: tags:
1. Import Numpy
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:markdown id: tags:
2. Create a 6 rows, 4 columns array of 1s called ```my_array```
%% Cell type:code id: tags:
``` python
my_array = np.ones((6,4))
```
%% Cell type:code id: tags:
``` python
my_array.shape
```
%% Output
(6, 4)
%% Cell type:markdown id: tags:
3. Modify the element at row = 3, column = 1 to be 50
%% Cell type:code id: tags:
``` python
my_array[3,1] = 50
```
%% Cell type:markdown id: tags:
4. Modify the array by multiplying all its values by 3
%% Cell type:code id: tags:
``` python
my_array = 3 * my_array
```
%% Cell type:markdown id: tags:
5. Using Google, find the Numpy function to take the square root and create a new array called ```my_array_root``` by applying it to your current ```my_array```.
%% Cell type:code id: tags:
``` python
my_array_root = np.sqrt(my_array)
```
%% Cell type:markdown id: tags:
6. Create a new array called ```my_array_sum``` by multiplying ```my_array``` and ```my_array_root```
%% Cell type:code id: tags:
``` python
my_array_sum = my_array + my_array_root
```
%% Cell type:code id: tags:
``` python
my_array_sum
```
%% Output
array([[ 4.73205081, 4.73205081, 4.73205081, 4.73205081],
[ 4.73205081, 4.73205081, 4.73205081, 4.73205081],
[ 4.73205081, 4.73205081, 4.73205081, 4.73205081],
[ 4.73205081, 162.24744871, 4.73205081, 4.73205081],
[ 4.73205081, 4.73205081, 4.73205081, 4.73205081],
[ 4.73205081, 4.73205081, 4.73205081, 4.73205081]])
%% Cell type:markdown id: tags:
7. Find the maximum value of your array by using either a methods or a function
%% Cell type:code id: tags:
``` python
np.max(my_array_sum)
```
%% Output
162.2474487139159
%% Cell type:code id: tags:
``` python
my_array_sum.max()
```
%% Output
162.2474487139159
%% Cell type:markdown id: tags:
8. Create a boolean array where only places in ```my_array_sum``` which are larger than 100 are True
%% Cell type:code id: tags:
``` python
my_boolean = my_array_sum > 100
```
%% Cell type:code id: tags:
``` python
my_boolean
```
%% Output
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, True, False, False],
[False, False, False, False],
[False, False, False, False]])
%% Cell type:code id: tags:
``` python
```
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
%% Cell type:markdown id: tags:
# 8. Loops and conditions solutions
%% Cell type:markdown id: tags:
1. Create list of 10 numbers with values spread between 0 and 100
%% Cell type:code id: tags:
``` python
my_list = [3, 80, 25, 56, 4, 32, 9, 29, 92, 49]
```
%% Cell type:markdown id: tags:
2. Create a for loop that goes through the list and prints each number
%% Cell type:code id: tags:
``` python
for elem in my_list:
print(elem)
```
%% Output
3
80
25
56
4
32
9
29
92
49
%% Cell type:markdown id: tags:
3. In the loop create a variable called ```squared_val``` that is the square of the current value of the list and print that instead of the original value
%% Cell type:code id: tags:
``` python
for elem in my_list:
squared_val = elem**2
print(squared_val)
```
%% Output
9
6400
625
3136
16
1024
81
841
8464
2401
%% Cell type:markdown id: tags:
4. Create an empty list before the for loop. In the loop append squared_val to that empty list. Verify now that your array contains indeed those squared values
%% Cell type:code id: tags:
``` python
empty_list = []
for elem in my_list:
squared_val = elem**2
empty_list.append(squared_val)
```
%% Cell type:code id: tags:
``` python
empty_list
```
%% Output
[9, 6400, 625, 3136, 16, 1024, 81, 841, 8464, 2401]
%% Cell type:markdown id: tags:
5. Create an ```if``` condition in order to only append square of numbers which are larger than 50
%% Cell type:code id: tags:
``` python
empty_list = []
for elem in my_list:
if elem > 50:
squared_val = elem**2
empty_list.append(squared_val)
```
%% Cell type:code id: tags:
``` python
empty_list
```
%% Output
[6400, 3136, 8464]
%% Cell type:markdown id: tags:
6. Create another empty list before the for loop. Add an ```else``` condition to your ```if``` statement and append the values smaller than 50 to that list.
%% Cell type:code id: tags:
``` python
empty_list = []
empty_list2 = []
for elem in my_list:
squared_val = elem**2
if elem > 50:
empty_list.append(squared_val)
else:
empty_list2.append(squared_val)
```
%% Cell type:code id: tags:
``` python
empty_list
```
%% Output
[6400, 3136, 8464]
%% Cell type:code id: tags:
``` python
empty_list2
```
%% Output
[9, 625, 16, 1024, 81, 841, 2401]
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment