Flashcards
Python & Numpy

Cards in this deck (39)
- 1Which of the following is true? ?A You can override functions you import to change them. ?B You can store functions in variables. ?C Every function returns something or raises an exception. ?D All of the above.D
- 2—


- 3—


- 4Optional Parameter --> z3
6
- 5—NoneType
int
function
- 6—? When you convert an object from one type to another that is called casting.
int --> #str to int
set --> #ist to set
1--> #float to int, but information loss
- 7What are iterables?Objects you can iterate over are called Iterables --> list, tuple, set and dictionary
e.g. for element in [1, 2, 3]: - 8—True
[1]![True [1]](https://examtimeassets.s3.amazonaws.com/uploads/media/image/27491680/desktop_e585a073-c8cb-4c1b-9ea2-19f67277c80a.png)
- 9Eigenschaften eines Tupels?--> tuples are immutable
--> use them if the order of the elements is important
True
- 10Eigenschaften Set?--> A set can contain only unique values
-->? You can cast a list or tuple to set to get all the unique values
set([1, 2, 3])
- 11Eigenschaften dictionary?--> A dictionary maps keys to values
? --> You can only use immutable objects as keys
? --> A tuple would work as a key, a list would not
True
"world"
- 12Was ist Slicing? Worauf kann ich es anwenden?--> With slicing you can get parts of sequences
-->You can use it on list, tuple, set, str
--> sequence[start:end:step]
--> ”start” is inclusive, ”end” is exclusive
--> No ”start”: 0 is used
--> No ”end”: Go until end
[0, 2]
[0, 1, 2]
[2, 3, 4, 5]
- 13—[3, 4]
[5, 3, 1]
[1, 2, 3, 4]![[3, 4] [5, 3, 1] [1, 2, 3, 4]](https://examtimeassets.s3.amazonaws.com/uploads/media/image/27492634/desktop_ddb93410-ce13-44a6-a65d-33662dbc63c7.png)
- 14Was ist numpy? Wie kann ich es aufrufen?--> NumPy (Numerical Python) is a Python library
--> Has many nice mathematical features such as matrices and arrays
import numpy as np - 15Was ist ein array?--> A container for values (like a list!)
--> All values must be of the same type
array([1, 2, 3])
array([[ 7, 8, 9], [10, 11, 12]])
- 16—--> Arrays have a certain number of rows and columns
--> You can find out how many rows and columns it has with .shape
--> The first number is the number of rows
--> The second number is the number of columns
(2, 3)
- 17n = np.arrange(0, 30, 2) print(n)--> Can be used to create an array with values in a range
--> start at 0 count up by 2, stop before 30
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]) - 18reshape


- 19Resize- Difference to reshape?--> Works like np.reshape
--> Difference: Performs operation in-place
--> This means it doesn’t return a value, it changes the array directly

- 20o = np.linspace(0, 4, 9) print(o)Creates an array of evenly spaced numbers over an interval: return 9 evenly spaced values from 0 to 4
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ]) - 21—Returns an array of specified shape and type of 1s or 0s


- 22print(np.eye(3))Returns an NxN sized array where the diagonal is all 1s

- 23Addition/ Subtraction/ Multiplication/ DivisionShapes must be compatible
This is generally only the case
if they have the same shape
[5 7 9]
[-3 -3 -3]
- 24x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) print(x.dot(y)-->Dotproduct
--> 1*4 + 2*5 + 3*6
32 - 25—--> NumPy supports different sizes of its types, for example int64
--> This means the integers use 64 bits
--> This is also the default value for int arrays
dtype('int64')
dtype('float64')
- 26What are lambda functions?--> Lambda functions are functions
--> You need to specify arguments that the lambda function takes and what it returns
--> You should not store lambda functions in variables (though you can): The only reason to do this would be to re-use the lambda function, but in that case a normal function is more appropriate
-->no return needed, the return value is whatever comes after the colon
--> The intention is to use them when you need a throwaway function that is used only once and then not again - 27—[('P1', 44), ('P2', 22), ('P3', 33)]
![[('P1', 44), ('P2', 22), ('P3', 33)]](https://examtimeassets.s3.amazonaws.com/uploads/media/image/27514324/desktop_dc4458d9-ff90-4e6e-87f7-fddc28fb1885.png)
- 28—[('P2', 22), ('P3', 33), ('P1', 44)]
-->key specifies what to sort!![[('P2', 22), ('P3', 33), ('P1', 44)] -->key specifies what to sort!](https://examtimeassets.s3.amazonaws.com/uploads/media/image/27514337/desktop_37ce4145-e810-4911-8c56-37ccd4376daf.png)
- 29—[0, 2, 4]
--> works like a for loop![[0, 2, 4] --> works like a for loop](https://examtimeassets.s3.amazonaws.com/uploads/media/image/27514357/desktop_359c2ab2-d677-461f-9822-379ff976582e.png)
- 30Structure of list comprehension?[(expression) for (variable) in (iterable) (optional if)]
--> by putting square brackets around it you convert the entire thing to a list - 31—at 0x2ab4d7cbd408>

- 32—


- 33—'World'

- 34—D)

- 35—B)

- 36—--> map takes a function and a number of iterables
--> It then applies function to every item of the iterables and returns an iterator
--> If more than one iterable is passed then function must take that many arguments
[0, 1, 4, 9, 16]
- 37—C)

- 38—--> filter takes a function and one iterable
--> It then applies function to every item of the iterable and returns an iterator containing only those elements for which the function returns True
[0, 2, 4]
- 39—--> reduce takes a function, one iterable and an optional argument initializer
--> It then applies function cumulatively to every item of the iterable, to reduce them to a single value
--> If an initializer is passed it is used as the first value before using the values

Information
- Description
- Flashcards on Python & Numpy, created by Jojo wi on 03/06/2020.
- Created by
- Jojo wi
- Views
- 3
- Rating