Python Flashcards

1
Q

+

A

Addition

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

-

A

Subtraction

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

*

A

Multiplication

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

/

A

Division (standard); floating-point numbers

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

//

A

Division (integer); no decimals; rounds to whole num

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

**

A

Exponents

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

“Hi” + “ world!”

A

Text in quotes added together

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

“Hi” * 5

A

Concatenates copies of the string

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

[1,2] + [3,4]

A

Concatenates the lists into one list
Python is 0-indexed, so [1] is list item 0

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

not True

A

Negation

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

True or False

A

One OR the other must be True statements

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

True and False

A

Both must be true to be true

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

==

A

Equals to

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

!=

A

Not equal to

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

<

A

Less than

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

>

A

Greater than

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

<=

A

Less than or equal to

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

> =

A

Greater than or equal to

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

And

A

Both conditions must be true

20
Q

Or

A

One condition must be true

21
Q

if statements

A

conditional statement with the options if, else, elif

22
Q

elif

A

elif (else if) = multiple conditions with different responses

23
Q

name = “Averil”
if name == “Averil”:
response = “Hi Averil!”
else:
response = “Girl bye.”

A

Hi Averil!

24
Q

name = “Averil”
if name == “Averil”:
response = “Hi Averil!”
elif name = “Ava”:
response = “Hi Ava”
else:
response = “Girl bye.”

A

Hi Averil!

25
ways to use Python
1. From command-line prompt (run directly from prompts) 2. As a script 3. In a browser (Jupyter, Google Colab) -- Use mouse to edit texts -- Groups commands in same cell ***accessible across many platforms for all users
26
Why Python in comp bio?
Massive scientific community Tests/writes code quickly Easy to learn and use Compiles scientific libraries (ex/ numpy) VERSATILE
27
expressions
Python expressions simply calculations and text manipulations
28
variables
- provide flexible storage for diverse data types - can be put in variables (lists, numbers, strings, etc) Variable name criteria: -- One word only -- Numbers, letters, _ only -- Cannot begin with a number -- Case sensitive
29
flow control structures
Flow control structures guide Python’s decision-making -- if, elif, else statements
30
dictionaries
- Map keys to values for efficient data retrieval - Unordered mapping of a key (string, num) to a value (string, num, etc)
31
d = { } d["key"] = "value" d["key2"] = 5 d["key3"] = {"h":"I"} print(d["key2"])
5
32
lists
Lists store ordered data for sequential processing (doesn’t have to be the same data type)
33
d = [“hello”, 5] print (d)
“hello”, 5
34
d = [] d.append[5] d.append[6] d.append[7] print (d[1])
6 Creates an empty lists and then adds items to the 0-indexed list
35
for loops
For loops allow you to go through a list item by item (iterates through lists/sequences)
36
for i in [1,2,3]: print (i*i)
1 2 3
37
for x in [“dog”, “cat”]: print (x)
dog cat
38
definitions
Python functions or definitions organize reusable blocks of code def → defines a function return → determines the output
39
def add_numbers(a,b): return a + b add_numbers(5,6)
12
40
def add_nums_and_double(c,d): var1 = c+d var2 = 2 * var1 return var2 add_nums_and_double(5,7)
24
41
def name_person(name): return name + “ is fun!” name_person(“Averil”)
Averil is fun!
42
Libraries
- Extend Python capabilities Through pre-written code that can be used in your own program Ex/ Numpy: advanced math features (arrays + vectorization)
43
import numpy a = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) a*5
array ([[5,10,15], [20,25,30], [35,40,45]])
44
Python Common Errors
Definition MUST PRECEDE usage Consistent indentation is necessary == indicates equality = assigns a variable Mix only compatible variable types Remember to add return to a definition to produce an output
45
student = { "name": "Alice", "age": 20, "major": "Computer Science" } print(student["name"]) print(student["age"]) student["gpa"] = 3.8 Iterating through the dictionary for key, value in student.items(): print(key, ":", value)
Alice 20 name : Alice age : 20 major : Computer Science gpa : 3.8