Python Notes Flashcards

1
Q

Is python case sensitive and does spacing matter?

A

yes to both

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

What is Print used for?

A

To show a value

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

Arithmatic in python?

A

+, -. *, /

Addition, subtraction, multiplication, division

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

**?

A

Does number to the power of a other

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

what does % do?

A

Gives remainder from a division

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

//?

A

Divides one number by another but rounds down to nearest integer

(still rounds down even when negative)

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

how to assign closely related variables?

A

x, y, z = 2, 3, 4

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

Can you have spaces in variable names?

A

no, should use underscores and all lower case

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

what is += used for?

A

Telling a descriptive value it is being effected by new values

also -= when want to subtract values

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

Float?

A

Decimal answer (even if it .0

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

Int?

A

Integer (whole number)

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

How to find out if int or float?

A

Use type()

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

Interaction with an int and a float always =?

A

Float

Can also use int and float as functions to add or remove decimal points

x = int(4.7) # x is now an integer 4
y = float(4) # y is now a float of 4.0

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

What does bool (boolean) do?

A

Say if something is true or false

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

Comparison operators?

A

< less than
> greater than
<= less or equal than
>= greater or equal than
== equal to
!= not equal to

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

What is a string?

A

Data type for immutable ordered sequences of characters

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

How to print words?

A

Use Print (“…….”)

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

How to get rid of issue of using speech marks within a quote?

A

Use single for overall one, and use double for quote

If need more than one, use a / before the ‘ to let python know its part of the string

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

How to combine, or repeat strings or put a space?

A

+ for combine

  • for repeat

” “ for space

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

what does Len( ) do?

A

Returns the length of a string

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

How to convert values into a string value?

A

str()

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

How to convert values into a integer?

A

Int()

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

What is a method?

A

A function that belongs to an object

for example title() will turn the string into a title so capitals in each words

Is placed in the bracket and doesn’t need anything in its own brackets, acts on the string

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

What is the length of the string variable verse?
What is the index of the first occurrence of the word ‘and’ in verse?
What is the index of the last occurrence of the word ‘you’ in verse?
What is the count of occurrences of the word ‘you’ in the verse?

A

length = len(verse)
print(length)

index = verse.index(“and”)
print(index)
rindex = verse.rindex(“you”)
print(rindex)

count = verse.count(“you”)
print(count)

25
Q

Example of a list and how to access parts of the list?

A

months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]

print(months[0]) # January
print(months[1]) # February
print(months[7]) # August
print(months[-1]) # December
print(months[25]) # IndexError: list index out of range

26
Q

How would you get the Q3 months (slicing)

A

months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]

q3 = months[6:9]
print(q3) # [ ‘July’, ‘August’, ‘September’]

Note how the lower bound is inclusive, but the upper bound is exclusive

Note it starts from 0, not 1

27
Q

What do ‘in’ and ‘not in’ do?

A

evaluates if the object on the left hand side is in, or not in the object on the right eg.

eg . print( ‘Sunday’ in months, ‘Sunday’ not in months)

Gives False True

28
Q

Difference between strings and lists?

A

Mutability is whether an object can change its values once it has been created

Lists are mutable (can be changed)

Strings are immutable (can’t be changed)

29
Q

What is ordering and are lists and strings ordered?

A

When order of elements within a list matter

Yes both strings and lists are ordered

30
Q

Use list indexing to determine how many days are in a particular month based on the integer variable month, and store that value in the integer variable num_days. For example, if month is 8, num_days should be set to 31, since the eighth month, August, has 31 days.

A

month = 8
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]

num_days = days_in_month[month - 1]
print(num_days)

31
Q

what does max() do when used on a list or string?

A

Gives the largest value

Value that would appear last if ordered alphabetically

32
Q

What does min() do?

A

Opposite of max

33
Q

What does sorted() do?

A

Returns a copy of a list ordered from smallest to largest, leaving the original unchanged

can do the opposite argument by adding into the function ,reverse=true

34
Q

How to separate all the words out in a list on separate lines?

A

“/n”.join

if want hypothons between the words use “-“ .join

35
Q

Difference between data types and data structures?

A

A data type is just a type that classifies data. This can include primitive (basic) data types like integers, booleans, and strings, as well as data structures, such as lists.

Data structures are containers that organize and group data types together in different ways. For example, some of the elements that a list can contain are integers, strings, and even other lists!

36
Q

What is a tuple?

A

A data type for immutable ordered sequences of elements

Used when have values that are so closely related will always be used together, eg longitude and latitude

37
Q

Example of a tuple?

A

dimensions = 52, 40, 100
length, width, height = dimensions
print(“The dimensions are {} x {} x {}”.format(length, width, height))

38
Q

What are sets?

A

A data type for mutable unordered collections of unique elements

eg. amount of people visiting your website and you collect all the countries it has been from, there will be duplicates

39
Q

How do you remove duplicates?

A

Use the set() function

countries = [‘Angola’, ‘Maldives’, ‘India’, ‘United States’, ‘India’, ‘Denmark’, ‘Sweden’, ‘Ghana’, … 777 more countries not displayed]
print(len(countries)) # 785
print(countries[:5]) # [‘Angola’, ‘Maldives’, ‘India’, ‘United States’, ‘India’]
country_set = set(countries)
print(country_set) = 196

40
Q

What does the add() and pop() methods do?

A

You can add elements to sets using the add method, and remove elements using the pop method, similar to lists. Although, when you pop an element from a set, a random element is removed. Remember that sets, unlike lists, are unordered so there is no “last element”.

41
Q

Whats a dictionary?

A

A data type for mutable objects that store mappings of unique keys to values

eg. elements = {“hydrogen”: 1, “helium”: 2, “carbon”: 6}

42
Q

What does get() do?

A

Sees if value is in a dictionary, gives true or false

43
Q

Example of identity operators?

A

is evaluates if both sides have the same identity

is not - evaluates if both sides have different identities

44
Q

Example of a compound data structure, what does it add to the elements table?

A

elements = {“hydrogen”: {“number”: 1,
“weight”: 1.00794,
“symbol”: “H”},
“helium”: {“number”: 2,
“weight”: 4.002602,
“symbol”: “He”}}

45
Q

How do you do a lookup in a compound data structure?

A

helium = elements[“helium”] # get the helium dictionary
hydrogen_weight = elements[“hydrogen”][“weight”] # get hydrogen’s weight

46
Q

How do you add to a compound data structure?

A

oxygen = {“number”:8,”weight”:15.999,”symbol”:”O”} # create a new oxygen dictionary
elements[“oxygen”] = oxygen # assign ‘oxygen’ as a key to the elements dictionary
print(‘elements = ‘, elements)

47
Q

How do you split a verse into a list of words?

A

verse_split = verse.split()

48
Q

How do you turn a list of words into a data structure that stores unique elements?

A

verse_set = set(verse_list)

49
Q

How do you count the amount of words in a unique data structure?

A

num_unique = len(verse_set)

50
Q

How do you find if a certain word is in the dictionary?

A

contains_breathe = if ‘breathe’ in verse_dict:
print(“The word ‘breathe’ is in the dictionary.”)
else:
print(“The word ‘breathe’ is not in the dictionary.”)
print(contains_breathe)

51
Q

How do you find the unique number of keys in a dictionary?

A

num_keys = len(verse_dict.keys())
print(num_keys)

52
Q

How do you create and sort a list of dictionary keys?

A

sorted_keys = sorted(verse_dict.keys())

53
Q

How do you find the first element in the dictionary keys?

A

first_key = sorted_keys[0]
print(first_key)

54
Q

How do you find the element with the highest value in the list of keys?

A

key_with_highest_value = max(verse_dict, key=verse_dict.get)
print(key_with_highest_value)

55
Q

Features of a list?

A

Ordered

Mutable

[ ] or list() - are its constructors

eg. [5.7, 4, ‘yes’, 5.7]

56
Q

Features of a tuple?

A

Ordered

Not Mutable

( ) or tuple() are its constructors

eg. (5.7, 4, ‘yes’, 5.7)

57
Q

Features of a set?

A

Not ordered

Mutable

{}* or set() are its constructors

eg. {5.7, 4, ‘yes’}

58
Q

Features of a dictionary?

A

Not ordered

Not mutable

{ } or dict() are its constructors

eg. {‘Jun’: 75, ‘Jul’: 89}