Python Refresher Flashcards
Review python from 1st year engineering
How are statements printed in python?
How are results stored in python?
print ("hello world") myresult = 5*6
Let’s say that we have a variable x = 10
Write code to detect whether x is postive, 0 or negative.
x = 10 if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")
What is the format for a for loop?
What is the format for a while loop?
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
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?
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
What is JSON?
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] }
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.
# 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
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
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)
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)
~~~
#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]
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)
~~~
[2, 4]
What is the format for list comprehension with conditionals?
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]
Write a factorial function using recursion.
def factorial(n): if n == 1: return 1 else: return n*factorial(n-1)
What do the following matplotlin functions do?
~~~
plt.plot(x, y)
plt.bar(x, y)
plt.hist(x)
~~~
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
What does the following code do?
~~~
plt.savefig(“filename.png”)
~~~
Calling this after generating a plot will save the plot to a file.