Python basics Flashcards

1
Q

Syntax to import datetime from an external module

extra: & then set datetime to a current_time variable

A

from datetime import datetime

current_time = datetime.now()

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

What data type does random.choice() (from the module random) take as an argument? What does it return?

A

random.choice() takes a list as an argument.
It returns an number (from the list passed to it).

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

What does random.randint() take as arguments, and how many?
What does it return?

A

random.randint() takes two numbers as arguments.
It returns a random number between the two numbers passed as arguments.

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

Call a function from the random module to generate a random number between 1 and 100.

A

import random

random_number = random.randint(1, 100)

NB includes both limits.

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

What does random.sample(list, int) do?

e.g. here:
my_list = [“apple”, “kiwi”, “lime”]
random.sample(my_list, 2)

A

Takes a list (first argument) and number (second argument) and returns that number of random items from the list.

Returns 2 random items from the list my_list

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

Syntax to import a module library as an alias

A

import libraryname as localname

e.g. from matplotlib import pyplot as plt

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

Syntax to import a module library as an alias

A

import libraryname as alias_name

e.g. from matplotlib import pyplot as plt

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

Best practice place to keep all your imports?

A

At the top of your file, all together

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

Get unique elements from a list (below) using a set.

numbers = [1, 1, 1, 2, 3, 4]

A

numbers = [1, 1, 1, 2, 3, 4]

set(numbers)

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

Get unique elements from a list (below) using a for loop and returning a list.

numbers = [1, 1, 1, 2, 3, 4]

A

numbers = [1, 1, 1, 2, 3, 4]

def get_unique_numbers(numbers):
unique = [ ]
for number in numbers:
if number not in unique:
unique.append(number)
return unique

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

Get unique elements from a list (below) using a for loop and returning a list.

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

What method merges a given dictionary into an existing dictionary?

A

.update( )

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

Use .update() to merge a new dictionary into an existing one.

Is the original dictionary modified in-place or a new one created with this method?

A

existing_dict.update(new_dict)

The original dictionary is modified in place (so inside a function, you’d want to return the original dictionary after updating it, to output the merged dictionary).

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