Python programming Flashcards

1
Q

From this code define:
a module
an instancevariable
a function
a method

import random
class Dice:
def __init__(self):
self.value = random.randint(1,6)

def roll(self):
self.value = random.randint(1,6)

def getValue(self):
return self.value

def __str__(self):
return ‘Dice = ‘ + self.value

t1 = Dice() print(t1.getValue())

A

A module from this code is random.

The instancevariable is the attribute which is self.value

a function is randint

a method is roll(), getvalue(). Methods are functions defined in a class.

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

Define the datatypes of the following expressions:

4%3
1.0 * 7
5*7
4>3
‘12’ + ‘13’
[ e for e in range(7)]
[True, False, True]
‘1’ + ‘2’
{‘abc’: 4}
(‘x’, 4)

A

int
float
int
int
str
lst
lst
str
dict
tuple

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

What is the word import in python?

A

A keyword.

“keywords” refer to reserved words that have special meanings and functions in the language.

If, while, else, def, return ect. are also keywords

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

Give examples of built in functions in Python

A

print()
len()
input()

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

What is the difference between tuples and lists?

A

tuples are unmutable and can contain different datatypes. Can be used to create dictionaries.

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

What are boolean values?

A

True or False

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

Difference between “/” “//”

A

/ gives result in float type
// integer division

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

Difference between integer counting and float counting?

A

integer counting is exact while floating point counting can give rounding errors.

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

What is true about the for loop?

  • you can write for loops that does not stop
  • You can have if statements in a for loop
  • You can have if-statements in a for loop
  • You can always replace a for loop with an if statement
  • clauses in a for loop always have to run at least once
  • With the word “continue” you leave a for loop
A

True
True
False
False
False

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

True or false: If you know the index of a value in a list you can change it.

A

True

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

Given the following code, what are the values of a and b to make print(y) give [9,8,3]?

x = [1, 2, 3]
y= [4, 5, 6]
y=x
x[a] = 9
y[b] = 8
print(y)

A

a = 0 because the first index in the list should be 9

b = 1 because the second index in the list should be 8.

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

How can you recognize a class?

A

The first letter is capital

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

What is slicing? what can you do with it?

A

manipulate lists.

You can create new lists with only parts of the old list.

Give the list (or parts of the list) in reverse.

Give a copy of the list

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

Give example of functions that take strings as parameters

A

Input()
split()

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

What is an argument and what is a parameter?

A

def g(a, b)
return a + b

x = 8
y = g(x, 14)

In this code a and b are parameters for the function g.

x and 14 are arguments passed to the function.

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