Python Refresher Flashcards

Review python from 1st year engineering

1
Q

How are statements printed in python?

How are results stored in python?

A
print ("hello world")
myresult = 5*6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Let’s say that we have a variable x = 10
Write code to detect whether x is postive, 0 or negative.

A
x = 10
if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the format for a for loop?

What is the format for a while loop?

A
Example of a for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Example of a while loop
count = 0
while count < 5:
    print(count)
    count += 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In python how can we initialize variables with the basic data types of integers, floats and strings.

How about composite data types? How do we access numbers in an array? How do we access members of a dictionary?

A
Integers: myVar = 5;
Floats: myVar = 6.3;
Strings: myVar = "Hello World"

many_numbers = [5, 42, 1, 10]
many_numbers[1] //will return 42

nums = {"best number":4, "still great": 8}
nums["best number"] // will return 4

Note that in python we use 0-based indexing

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

What is JSON?

A

JSON is a simple textual format derived from Javascript syntax

Files in JSON format can be read/written by Python code and can also be edited by jand if necessary.

{
    "name": "Lorenzo"
    "location": "Calgary"
    "numbers": [0,1,1,2,3,5]
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Assume that the contents of data.json is
{ "name" : "Lorenzo", "location" : "Calgary", "numbers" : [1,2,3,42] }

Write code so that we will print out name, and the number 42.

A
# First things first - import the json module
import json

Load the file
with open("data.json", "r") as inF:
    content = json.load(inF)

Print the content
print(content["name"]) #will print out Lorenzo
nums = content["numbers"] #assigns nums to the array
print(nums[3]) #prints out 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write code so that mydict = { "Temperature" : -36, "Location" : "Calgary", "Feeling" : "miserable", "numbers" : [0, 1, 1, 2, 3, 5, 8, 13] } is written to a file called output.json

A
mydict = {
    "Temperature" : -36,
    "Location" : "Calgary",
    "Feeling" : "miserable",
    "numbers" : [0, 1, 1, 2, 3, 5, 8, 13]
}

with open("output.json", "w") as fifi:
    json.dump(mydict, fifi)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the following print:
~~~
#Example of List Comprehension without filters
numbers = [1, 2, 3, 4]

numsq = [n*n for n in numbers]

print(numsq)
~~~

A
#Example of List Comprehension without filters
numbers = [1, 2, 3, 4]

numsq = [n*n for n in numbers]

print(numsq) #will print [1, 4, 9, 16]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will the following code do?
~~~
numbers2 = [1, 2, 3, 4, 5]

even_numbers = [x for x in numbers2 if x % 2 == 0]
print(even_numbers)

~~~

A

[2, 4]

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

What is the format for list comprehension with conditionals?

A
dst = [comp1(e) if cond(e) else comp2(e) for e in numbers]

numdst = [n*n if n<5 else n/2 for n in numbers]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a factorial function using recursion.

A
def factorial(n):
    if n == 1:
        return 1
    else:
        return n*factorial(n-1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do the following matplotlin functions do?
~~~
plt.plot(x, y)
plt.bar(x, y)
plt.hist(x)
~~~

A
plt.plot(x, y) #plots x, y using lines
plt.bar(x, y) #plots x, y data using bars
plt.hist(x) # plots the distribution (histogram) of data in x 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the following code do?
~~~
plt.savefig(“filename.png”)
~~~

A

Calling this after generating a plot will save the plot to a file.

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