Part 4 Modules Flashcards

1
Q

Import a module to display current time

A

import datetime

x = datetime.datetime.now()

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

There is a method in the datetime module that outputs more human readable formats but requires DIRECTIVES. Use it, but first pass the year 2018, the month June and the day 1 to it.

A

x = datetime.datetime(2018, 6, 1)
x.strftime((‘%B’))

%B - this is the directive
strftime <- string format time

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

Show the absolute value of -17
show the lowest and highest values of the below list:
[5, 10, 48]

Calculate the value of 4 to the power of 3

A

print(abs(-7.25))

print(min(5, 10, 48))
print(max(5, 10, 48))
print(pow(4, 3)
This basically means 3 over for or (4 * 4 * 4)

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

Import a module to:
determine the square root of 64

round 1.4 to the upwards to the nearest integer and then downward to the

show the value of pi

A

import math
print(math.sqrt(64))

math.ceil(1.4)
math.floor(1.4)

math.pi

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

What does JSON stand for?
Import JSON and:
Create a variable that looks like JSON
Parse it
Print the results

A

Java Script Object Notation

import json

x = ‘{ ‘name’: ‘John’, ‘age’ : 30, ‘city’: ‘New York}’

y = json.loads(x)

print(y[‘age’])

parsing - divide something into parts to analyze individually
data parsing - Converting one data format to another

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

Convert the below python into json

x = { ‘name’: ‘John’, ‘age’: 30, ‘city’: ‘New York’}

A

import json
y = json.dumps(x)
print(y)

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

What are the json equivalents to the below
dict
list
tuple
str
int
float
True
False
None

A

Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null

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

The info is a bit hard to read after converting info into json, indent the code to make it more readable

A

json.dumps(x, indent=4)

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

JSON normally indents using a comma and a space to separate each object and a colon and a space to separate keys from values, how would you change this

Also, how would you sort the keys?

A

json.dumps(x, indent=4, separators=(‘. ‘, ‘=’)

json.dumps(x, indent=4, sort_keys=True

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

In JSON, what are dictionaries called?

A

Objects
{‘key’:’value’}

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

Import regular expessions
Search the below string to see if it starts with “The” and ends with ‘Spain’

A

import re

txt = ‘The rain in Spain’
x = re.search(‘^The.*Spain$’, txt)

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