Python 3 Flashcards

1
Q

How to comment in Python?

A

#

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

How to print in Python?

A

print(text here)

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

Float number

A

Number with a decimal

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

How to do exponents?

A

**
Example: For 62, you would write 6**2

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

How to make a multiline string?

A

””” or ‘’’

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

How to assign a variable to user input?

A

input(text here)

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

Boolean operators in Python

A

and, or, not

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

Else if statement

A

elif

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

Syntax Error

A

Error caused by not following the proper syntax of the language

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

Name Error

A

Error caused when an unknown variable is used

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

Type Error

A

Error caused when you do an operation on an incorrect type

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

how to concatanate Strings with ints or doubles

A

use a comma instead of a plus sign

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

How to create a list

A

variable = [stuff here, separated by commas]

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

True or False: Lists can contain any data type

A

True

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

How to add something to the end of a list?

A

list.append(“”)

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

list concatination

A

use the + symbol and store it in a new list

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

How to access the last element of a list

A

use -1 (-2 would access second to last, etc.)

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

How to add an element to a specific index in a list?

A

.insert(index, element)

19
Q

How to remove an element by index

A

.pop(index)
you can also do .pop() with no parameters and it will remove the last index

20
Q

what does range() do

A

range(10)
will create a range object with numbers 0 -> 9
range(2, 9)
will create a range object with numbers 2 -> 8
range(2, 9, 2)
will create a range object where each number is 2 greater than the previous
[2, 4, 6, 8]
note: because it is a range object, you need to convert it into a list using list(my_range)

21
Q

how to find the number of items in a list

A

len(my_list)

22
Q

how to slice a list

A

list_name[first index:last index]
note: it goes up to but not including last index

23
Q

what does list[:n] do?

A

selects the first n objects in a list (also works for negative, list[-n:] will select that last n objects in a list

24
Q

.count() method

A

will count the amount of times the parameter appears in a list (can also be used with 2d lists)

25
Q

.sort() method

A

will sort a list by numerical or alphabetical orders

26
Q

how to sort a list in reverse order

A

.sort(reverse=True)

27
Q

what is a tuple?

A

a data structure in python that allows us to store multiple pieces of data inside of it. similar to a list except that it is inmutable

28
Q

how to make a tuple

A

use () instead of []

29
Q

unpacking a tuple

A

you can use a tuple to set lots of variables with one line
example:
my_info = (‘mike’, 24, ‘programmer’
name, age, occupation = my_info

it will create three different variables, each assigned to the relative index of the tuple

30
Q

how to create a one element tuple

A

you MUST include a comma, or else it is just a regular data type
example:
WRONG -> one_element_tuple = (4)
RIGHT -> one_element_tuple = (4,)

31
Q

how to use the zip() function

A
  • use it to combine 1D lists into 2D lists
    names_and_heights = zip(names, heights)
    note: will create a zip object, need to cast as a list using list(names_and_heights) to print
  • also, inner lists will become tuples
32
Q

for loop strucure

A

for <temporary> in <list></list></temporary>

<action>
</action>

33
Q

elegant for loop

A

for ingredient in ingredients: print(ingredient)
(a one line loop)

34
Q

how to use range in for loops

A

Allows you to perform an action multiple times
(like loops in java)
for temp in range(6)

35
Q

continue statement

A

will stop the current iteration of the loop, move on to the next one

36
Q

list comprehensions

A

new_list = [<expression> for <element> in <collection>]
example: (will double each number in numbers)
numbers = [2, -1, 79, 33, -45]
doubled = [num * 2 for num in numbers]
print(doubled)</collection></element></expression>

37
Q

function definition

A

def function_name():

38
Q

Keyword Arguments

A

explicitly refer to what each argument is assigned to in the function call. example:
calculate_taxi_price(rate=0.5, discount=10, miles_to_travel=100)

39
Q

Positional Arguments

A

arguments that can be called by their position in the function definition

40
Q

Default argument

A

When a default value is provided. You can either choose to call the function without the value for the default (and the function will use the default value) or you can provide the value.
example:
def calculate_taxi_price(miles_to_travel, rate, discount = 10):

41
Q

max() & min() function

A

will get the maximum or minimum of all the parameters in it

42
Q

round() function

A

will round parameter to either:
- integer if only one parameter is used
- whichever decimal place you want to round it to if two parameters are use
example:
(to round 9.75 to one decimal place)
round(9.75, 1)

43
Q

multiple returns

A

you can return multiple things
example:
return first_day, second_day, third_day
then you assign it to three variable like this:
monday, tuesday, wednesday = threeday_weather_report(weather_data)

print(monday)
print(tuesday)
print(wednesday)

44
Q
A