Python Flashcards

1
Q

Sign to use for comments.

A

#

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

Function that can help you identify a variable’s data type.

A

print(type()) which can give back <class ‘float’/‘str’/‘bool’>
ex.:x=42
print(type(x))
<class ‘int‘>

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

How to write “2^3”?

A

print(2**3)

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

What does % do?

A

It calculates the remainder after left/right (exemple: print(5%2) will make 1 while print(2%4) will make 2 because 4 is bigger than 2.

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

How to use calculator mode?

A

print()

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

Storing a value in a variable.

A

Declaration

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

Whole number

A

Integer

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

Numbers with decimals

A

Floats

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

Non-numerical data

A

strings

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

“True” and “False” variation

A

boolean

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

money_in=100
print(money_in)

A

100

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

money_in=100
money_out=50
profit=money_in-money_out
print(profit)

A

50

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

How can you write: “I completed 3 courses!“

A

print(‘I completed‘+str(3)+‘courses!‘)

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

How can you convert a string into an integer?

A

You use int()
ex.:print(int(‘2‘)+2)
4

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

How can you convert a string into a float?

A

You use float()
ex.:print(float(‘2‘)+2.0)
4.0

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

How can you convert objects to a boolean value?

A

You use bool()
ex.:print(bool(2), bool(0), bool(‘hello‘))
True False True

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

How can you print “alright alright alright“?

A

print(‘alright‘*3)

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

Adding strings together using the ‘+‘ operator.

A

Concatenation
ex.:‘Data‘+‘Camp‘
DataCamp

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

What is the first element of a zero-indexing?

A

0

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

How can you count backwards when selecting elements near the end of a list?

A

You use negative indexes
ex.: x=[‘h‘, ‘b‘, ‘i‘, ‘k‘]
print(x[-1])
print(x[-3])
k
b

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

How can you make a list?

A

You use ‘[]‘.
l=[‘a‘, ‘b‘, ‘c‘]
print(l[0])
print(l[1])
a
b

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

How can you select multiple elements from a list?

A

Slicing: l[start:end]

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

What is excluded when you make a list?

A

The end index
ex.:l=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
print(l[1:3])
print(l[2:3])
[‘b‘, ‘c‘]
[‘c‘]

24
Q

What if I wanna take the first 2, do I need to include the index?

A

No
ex.: l=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
print (l[:2])
print (l[2:])
[‘a’,’b’]
[‘c’, ‘d’, ‘e’]

25
Q

What is a list?

A

A collection of elements

26
Q

l=[2, 4, 6]
print(type(l))

A

<class ‘list’>

27
Q

a=1
b=a
a=2
print(b)

A

1

28
Q

a=[1, 2]
b=a
a[0]=2
print(b)

A

[2, 2]

29
Q

a=1
b=a
a=2
print(b)

A

1

30
Q

a=[1,2]
b=a[:]
c=list(a)
a[0]=2
print(a,b,c)

A

[2,2] [1,2] [1,2]

31
Q

l=[[7, 6], [8, 9], [10, 11]]
print (l[1])

A

[8, 9]

32
Q

l[[7, 6], [8,9], [10, 11]]
print(l[1][0])

A

8

33
Q

l=[[7,6], [8, 9], [10, 11]]
print(l[-1][1:])

A

11

34
Q

l=[5, 6, 4, 2]
l[2]=100
print(l)

A

[5, 6, 100, 2]

35
Q

l=[5, 6, 4, 2]
l[0:2]=[1,2]
print(l)

A

[1, 2, 4, 2]

36
Q

How to delete elements from a list?

A

You use “del()”
ex.: l=[2, 3, 6, 8, 10]
del(l[3])
print(l)
[2, 3, 6, 10]
print(l[3])
10

37
Q

How can you combine two lists?

A

Use the “+”operators.
ex.: l=[5, 8]
print(l+[1, 2])
[5, 8, 1, 2]
a=[1, 3, 5]
b=[2, 4, 6]
print(a+b)
[1, 3, 5, 2, 4, 6]

38
Q

How can you combine two lists?

A

You use “+”.
ex.: l=[5, 8]
print(l+ [1, 2])
[5, 8, 1, 2]
a=[1, 3, 5]
b=[2, 4, 6]
print(a+b)
[1, 3, 5, 2 , 4, 6]

39
Q

How can you delete elements from a list?

A

You use the “del()”function.
l=[2, 3, 6, 8, 10]
del(l[3])
print(l)
[2, 3, 6, 8, 10]
print(l[3])
10

40
Q

How can you round a float?

A

You use the “round”function.
Ex.: x= round(3.1415, 2)
print (x)
3.14

41
Q

l=[2, 3, 6, 8, 10]
del(l[3])
print(l)

A

[2, 3, 6, 10]

42
Q

How can you round a number?

A

You use the “round()”function.

43
Q

x=round(3.1415, 2)
print(x)

A

3.14

44
Q

How can you capitalize a word?

A

You use the “capitalize()”function.

45
Q

greet= “hello”
print(greet. capitalize())
print(greet)

A

Hello
hello

46
Q

How can you add an element to a list?

A

You use “.append()”function.

47
Q

l=[‘cat’, ‘rat’, ‘mat’]
l.append(‘bat’)
print(l)

A

[‘cat’, ‘rat’, ‘mat’, ‘bat’]

48
Q

How can you use a package?

A

You import it using the format “import package”.

49
Q

The “numpy”package contains code for what?

A

Array computing

50
Q

What does “array”mean in computer science?

A

Data structure consisting of a collection of elements (values or variables), of same memory size, each identified by at least one array index or key.

51
Q

How can you import numpy?

A

You write “import numby”.

52
Q

n=numpy.array([1, 2, 3])
print(n)

A

[1, 2, 3]

53
Q

How to add a name to a function?

A

You use the function “as”.

54
Q

import numpy as np
n=np.array([4, 5, 6])
print (n)

A

[4, 5, 6]

55
Q

x=round(1.356)
print(x)

A

1

56
Q

How can you sort the elements of a list in ascending order?

A

You use the “sorted()”.

57
Q

a=sorted([4, 7, 2])
print(a)

A

[2, 4, 7]