Python Basics Flashcards

1
Q

Syntax used to concatenate strings

A

‘a’ + ‘b’

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

Finding the type of a value

A

type([val])

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

Syntax to define a function

A
def [function_name](param1, param2):
     code
     return val1, val2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax to insert values into a string in order

A

“France is in {} and China is in {}”.format(“Europe”, “Asia”)

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

Insert values into a string by position

A

“{0} times {0} equals {1}”.format(3, 9)

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

Insert values into a string by name

A

“{name}’s population is {pop} million”.format(name=”Brazil, pop=209)

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

Format specification for 2 decimal places

A

“I own {:.2f}% of the company”.format(32.5548)

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

Format specification for 2 decimal places and comma separator

A

“Your bank balance is {:,.2f}”.format(12345.678)

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

Define an empty class

A

class class_name:

pass

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

Instantiate an object of a class

A

variable = class_name

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

Define an init function in a class to assign an attribute at instantiation

A

class class_name:
def_init_(self, param1):
self.attribute1 = param1
mc_2 = MyClass(“arg_1”)

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

Import datetime module

A

import datetime as dt

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

Create datetime.datetime string given a month, year and day

A

[var] = dt.datetime(1985, 3, 13)

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

Create datetime.datetime object from string

A

[var] = dt.datetime.strptime(“24/12/1984”, “%d/%m/%Y”)

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

Convert datetime.datetime object to string

A
dt_object = dt.datetime(1984, 12, 24)
dt_string = dt_object.strftime("%d/%m/%Y")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Instantiate a datetime.time object

A

[var] = datetime.time(hour=0, minute=0, second=0, microsecond=0)

17
Q

Retrieve part of a date stored in the datetime.datetime object

A

[var].day

18
Q

Create a date from a datetime.datetime object

A
d2_dt = dt.datetime(1946, 9, 10)
d2 = d2_dt.date()
19
Q

Create a datetime.date object from a string

A
d3_str = "17 February 1963"
d3_dt = dt.datetime.strptime(d3_str, "%d %B %Y")
d3 = d3_dt.date
20
Q

Instantiate a datetime.timedelta object

A

eg_4 = dt.timedelta(weeks=3)

21
Q

Add a time period to datetime.datetime object

A
d1 = dt.date(1963, 2, 26)
d1_plus_1wk = d1 + dt.timedelta(weeks=1)