Introduction to Python Flashcards

(58 cards)

1
Q

Ask user data as “variable”

A

variable = input()

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

Convert to string

A

Str()

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

Convert to integer

A

int()

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

Convert to float

A

float()

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

How to combine “result is” and result in print

A

print(f”result is {result}”

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

How to calculate x square

A

x**2

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

How to print without line end

A

print(“”, end=” “)

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

Shorter for:
sum = sum + x

A

sum += x

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

Operator equal

A

==

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

Operator not equal

A

!=

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

Data type True/False

A

bool

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

How to calculate square root of x

A

sqrt(x)

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

How to end an if line

A

:

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

Check if “number” is odd

A

if number %2 == 0:

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

Three possibilities If expression

A

if
elif
else

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

X ist nicht kleiner als 1

A

if not x < 1

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

Nested if condition

A

if:
If:

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

How to create a loop

A

while:

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

How to check length of a string?

A

len()

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

Adress first letter of “word”

A

word[0]

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

last character of “word”

A

word[len(word)-1]

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

search for X in “word”

A

“X” in word

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

locate a substring in string

A

string.find(substring)

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

jump to start of loop

25
How to specify type of argument in a function. (int)
argument : int
26
Initiate list with values 0, 1
list = [0,1]
27
add to list
list.append()
28
Put a into position b in list
list.insert(b, a)
29
delete from list by index a
list.pop(a)
30
remove value a from list
list.remove(a)
31
redefine list from smallest to greatest
list.sort
32
new sorted list2 from list1
list2 =sorted(list1)
33
repeat for loop 3 times
for i in range(4)
34
print X decimals of argument number in f Print statement
{number.Xf}
35
print variable with X white spaces reserved
{variable: X}
36
slice a string
string[start:stop:step]
37
count the number of 1 in list
list.count(1)
38
replace a with b in string
string.replace("a", "b")
39
create a true copy of matrix
import copy copy_matrix = copy.deepcopy(matrix)
40
create an empty dictionary
dictionary= {}
41
delete key-value pair from dictionary
del dictionary["key"]
42
delete whole dictionary
dictionary.clear()
43
initiate a tuple
tuple = ()
44
How are tuples different from list
they are immutable
45
return tuple for each key-value pair in dictionary
dictionary.items()
46
traverse a dictionery
for X, Y in dictionary.items():
47
remove whitespace from start or end of string
string strip()
48
specify writing mode for file
with open("file", "w") as my_file:
49
specify append mode for file writing
with open("file", "a") as my_file
50
clear contacts of file
with open("file", "w") as my_file: pass OR open("file", "w"). close
51
What is a ValueError
The argument passed to function is invalid.
52
What is a Type error?
Value is the wrong type(int instead of str for len())
53
What is an Index error?
Referring to an index that doesn't exist.
54
random integer between a,b
from random import randint randint(a, b)
55
shuffle any data passed
from random import shuffle shuffle(data)
56
return a randomly picked item from data_structure
from random import choice choice(data_structure)
57
Draw random sample size n from data_structure
from random import sample sample(data_structure, n)
58
what is a method
object.method()