Python Basics Flashcards

1
Q

What is running file in Jupyter?

A

A file is considered a running file until you shut it down

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Jupyter?

A

Python allows you to communicate with the computer with the help of a specific software or application: Jupyter

It is a server-client application that allows you to edit your code through a web browser

It’s not a text editor that opens a new window everytime with different codes bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Access and leave a cell

A

Enter + Escape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Execute line to output field

A

Ctrl + Enter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can output line be modified?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Execute line to output field + new cell

A

Shift + Enter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Copy/Paste cells

A

Select cells (so no cursor appears in input)

With C, X and V you can copy, cut and paste

With arrow keys you can move up and down

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Asterisk sign in input cell?

A

Can appear in the input cell –> Meaning the code takes too long

Can be stopped with the stop symbol in the viewport

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Insert markdown cell
&
Convert cell from markdown to code cell and vice versa

A

M
Y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When using a capital in variable, for example Y instead of y, what will happen?

A

Will lead to an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Assign values to x and y

A

x,y = (1,2)
Parentheses are not obligatory but is for readability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When you just want to print an output

A

print()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Number functions. Name 2

A

Int() –> Integer –> Will round of to integer value

float() –> Float –> Convert to decimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

With this function you can ask what type the variable is

A

type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How are booleans written?

A

Booleans need to be written in CAPITAL LETTERS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String variables how to write?

A

Use single or double quotes

When printing a string it will give the output without quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Convert number into text

A

str() function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Print number + text

A

print( str(x) + ‘Dollars’ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Do you need to specify a variable type?

A

As opposed to other programming languages, Python can automatically guess the type of data you are entering –> It always knows the type of variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When to use double quotes?

A

Best for working with strings since you can have single quote in your text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Two ways to add strings together

A

Plus sign

Trailing comma: print(‘Red’,’car’) > Will add a space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A way to include a quote in string

A

Backslash before the quote

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What to do when 16/3 leads to ‘5’

A

Try:
float(16) / 3
or:
16.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What to do when not having enough space in the line?

A

In that case you can use backslash \ to continue the line onto the next line without breaking the code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Difference = and ==

A

With = you can set a value

With == you can check if that value is true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is returned: “Friday”[4]

A

‘a’
In Python we start from the 0 character and then start counting upwards

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What to do when lines are part of a function?

A

When lines are part of a function you must apply indentation to it

Same goes vice versa

Indent goes with using tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Give the order of operators for AND, NOT, OR

A

Order of operators

NOT
AND
OR

Example:

True and not True –> not True will be processed first

False or not True and True –> Leads to False

True and not True or True –> Leads to True because right part is True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What can be written instead of == and !=

A

is, and is not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is used as elseif statement?

A

elif statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Write a function steps

A

Start with ‘def’

Add name of function

Then parentheses with optional parameters

End with a colon

Indent and write body on next line

25
Q

Call function

A

Name of function with parentheses and possible arguments

26
Q

Use of return in function

A

A function always needs something in return –> Write this in the body

Can be used only once in a function!

27
Q

Variable in function

A

A function can have a local variable that can be defined in the code block:
Write the name of the function followed by = sign
Define the variable after the = sign

Variable can then be used in the return command

28
Q

How to deal with nested functions?

A

You can call a function within another function

In the return section of the second function you call the first function

29
Q

Are parameters order sensitive?

A

The parameters are order-sensitive. If you do write them in a different order then give them a value in the arguments: (b=3, a=10, c=4)

30
Q

Returns nr of elements (length) in a word for example

A

len()

31
Q

Returns float of x, rounded to y decimal points. When y is not defined that rounds to 0

A

round(x,y)

32
Q

To the power of with base of x to the power of y

A

pow(x,y)

33
Q

Creating a list?

A

Like creating an array –> Use straight brackets

Use quotation marks ‘ ‘ around elements

34
Q

Retrieve from list

A

print (Participants[1])

Use the index number and it will retrieve that entry

35
Q

List: Find last entry and before last entry

A

Last entry –> [-1]

Before last –> [-2]

36
Q

List: Delete entry

A

del Participants[2]

37
Q

How to invoke a certain method?

A

object.method()

Use dot operator to call on/invoke a certain method

38
Q

How to add something to a list?

A

Append
Participants.append(“name”)

39
Q

How to extend a list with multiple list entries?

A

It takes one argument so of it’s multiple list entries, put it in a list first

Participants.extend([‘Name1’, ‘Name2’])

40
Q

How to use variables in string?

A

You can write the variable without quotation marks like Participants[0]

len(Participants) –> leads to amount of entries in the list

41
Q

How does slicing work?

A

Take out the 2nd and 3rd entry from a list

Participants[1:3] –> So from 1 til 3

Participants[:2] –> Get the first two elements –> 0 til 2

Participants[4:] –> From the fourth til the end

Participants[-2:] –> From the for last onward

42
Q

How to find the index number?

A

Index

Participants.index(“Maria”) –> Gives the index number

Best put in variable before printing

43
Q

How to nest a list?

A

Nested lists

Bigger_List = [List1, List2]

44
Q

How to sort numbers and strings?

A

Sort()

Numbers = [1,2,3,4,5]

Numbers.sort()

–> Will put it in order

45
Q

Sort in reverse?

A

Sort in reverse order:

Numbers.sort(reverse=True)

46
Q

Tuples vs List

A

Tuples cannot be changed or modified
You cannot append or delete elements

You can place a tuple in a list and then each tuple becomes an element in the list

47
Q

Create tuple?

A

x = (40, 41, 41)

Values are packing into a tuple

48
Q

Tuple assignment

A

a,b,c = 1, 4, 6

49
Q

(age, years_of_school) = “30,17”.split(‘ , ‘)

Results in?

A

age = 30

years_of_school = 17

50
Q

Tuples as return in a function

A

New variables from function can be returned one after the other in the form of a tuple: (X,Y)

51
Q

Return from tuple?

A

x[0] –> Return first entry in tuple

52
Q

What is a dictionary? How to declare it? How to retrieve info?

A

Dictionary
Another way of storing data –> Like object in JS
Used with curly braces {}

Declaring dictionary
dict = {‘k1’:”cat”, ‘k2’:”dog”}

Retrieve –> Single or double quotes
dict[‘k1’]
dict[“k3”]

53
Q

Replace value in dictionary?

A

Replacing value
dict[‘k2’] = ‘squirrel’

54
Q

Fill dictionary after declaring?

A

Start with name1 = {}

Fill in the rest with name1[‘key1] = ‘Value1’

Fill in the rest with name1[‘key2] = ‘Value2’

Etc

55
Q

get method()
What if key is not in dictionary?

A

Name1.get(‘key1’) –> Returns value

When key is not in the dictionary –> Python will return None

56
Q

For loop syntax

A

for n in listname1:

print (n)

57
Q

How to order loop in single line:

A

Order loop in single line:

print (n, end = “ “)

Will result in a comma and empty space after each entry

58
Q

While loop syntax?

A

While loop

x = 0
while x <= 20:
print (x, end = “ “)
x = x + 2 –> Incrementing

Note when not applying a counter it will result in an infinite loop!

59
Q

Explain range function

A

range(start, stop, step)

start –> First number in the list
stop –> Last value +1
step –> Distance between each two consecutive values

Stop value is required
Start, step values are optional!
When start value not given it will be replaced with 0
And step value will be equal to 1 when not given

60
Q

Use range in a list

A

list(range(10)) –> Will output all integers from 0 to 9 in a list

With step value –> list(range(1,20,2))

61
Q

Use range in loop

A

for x in range(20):

x then goes over every entry in the range

62
Q

Iterate over dictionay

A

You can iterate over a dictionary starting from the first key to the last

You can retrieve the values with dict[i] for example

63
Q

Describe object & class

A

Every object belongs to some class

The class defines the rules for creating that object

Example: A bike is an object of the bike-makers class

64
Q

Other names for object and attributes

A

Object is sometimes called instance

Attributes are sometimes called properties

65
Q

Is a List an object?

A

The list that we created earlier is an object belonging to the list class

The attributes are the data types: floats

Methods that can be applied to these floats are .extend() or .index()

66
Q

What is a module?

A

Prewritten code containing functions and classes

Goal of module is to not having to rewrite all the code

A package consists of various modules

Sometimes people call a package a library

67
Q

How to import the sqrt function?

A

from math import sqrt as s
call –> s(36)
Results in 6.0

68
Q

Renaming imported modules?

A

import math as m
m.sqrt(49)
Result: 7.0

69
Q
A
70
Q
A