Python programming Flashcards
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 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.
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)
int
float
int
int
str
lst
lst
str
dict
tuple
What is the word import in python?
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
Give examples of built in functions in Python
print()
len()
input()
What is the difference between tuples and lists?
tuples are unmutable and can contain different datatypes. Can be used to create dictionaries.
What are boolean values?
True or False
Difference between “/” “//”
/ gives result in float type
// integer division
Difference between integer counting and float counting?
integer counting is exact while floating point counting can give rounding errors.
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
True
True
False
False
False
True or false: If you know the index of a value in a list you can change it.
True
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 = 0 because the first index in the list should be 9
b = 1 because the second index in the list should be 8.
How can you recognize a class?
The first letter is capital
What is slicing? what can you do with it?
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
Give example of functions that take strings as parameters
Input()
split()
What is an argument and what is a parameter?
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.