Unit 3 Flashcards

Types

1
Q

What is a string?

A

an immutable sequence of characters stored in a variable

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

3.1 - String Basics

How do you find the length of a string?

A

len(string)

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

3.1 - String Basics

What is string concatenation?

A

combining string values in an output statement

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

3.2 - List Basics

What is a container?

A

a construct which groups related objects together

individual components are called elements

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

3.2 - List Basics

What is a list?

A

an ordered mutable container, uses square brackets []

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

3.2 - List Basics

How do you concatenate lists?

use ‘list1’ and ‘list2’ as list names

A

list1 + list2

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

3.2 - List Basics

What are the four commands you can do for lists?

format answers as code

A

list.append(v) adds new element
list.pop(i) removes an element at index (i)
list.remove(v) removes an element with value (v)
list.index(v) finds a value (v)’s index

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

3.3 - Tuple Basics

What is a tuple?

A

an ordered immutable container, uses ()

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

3.3 - Tuple Basics

What does a named tuple do?

A

allows a programmer to name each element

Car = namedtuple('Car', ['model', 'price']

blazer = Car('Chevrolet', 32000')

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

3.4 - Set Basics

What is a set?

A

a mutable, unordered container with unique elements; uses set() or {}

{} used for literals

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

3.4 - Set Basics

What are the four commands you can do for sets?

use ‘setA’, ‘setB, etc. for set examples

A

set.intersection(setA, setB) set w/ elements in common
set.union(setA, setB) set w/ unique elements
setA.difference(setB, setC) set w/ unique elements from setA
setA.symmetric_difference(setB) set w/ elements that appear only once in setA/setB

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

3.5 - Dictionary Basics

What is a dictionary?

A

a mutable key/value paired collection, uses {'a': 1}

‘a’: 1 is a sample element for a dictionary

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

3.5 - Dictionary Basics

What is the difference between a key and a value?

A

key: index, like a term
value: mutable value, like a definition

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

3.5 - Dictionary Basics

What are the three commands you can use for dictionaries?

dictionary name: numbers
key: ‘one’, ‘two’, etc.
value: 1, 2, etc.

A

numbers["one"] references value from key
numbers['three'] = 3 creates/updates entries
del numbers['two'] removes an entry

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

3.6 - Common Data Types

What are the 5 data types?

A

string: sequence type, text
list: sequence type, ordered elements, mutable
tuple: sequence type, ordered elements, immutable
set: set type, unordered/unique elements, mutable
dict: mapping type, key/value elements, mutable

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

3.8 - Type Conversions

What are the three ways to convert types?

A

int() for integers/floats/int strings
float() for integers/floats/fractions/int strings
str() for all types

17
Q

3.9 - Binary Numbers

What is binary?

A

a base 2 number system; the basis for machine language

ex. 10 = 0110

10 / 8 = 2
2 / 2 = 0

18
Q

3.10 - String Formatting

How do you use string formatting?

give an example code

A

`print(f’{value}’)

19
Q

3.10 - String Formatting

What are the different ways to represent value in text?

10 total options

A

:s string (default)
:d decimal (base 10)
:,d decimal w/ commas
:03d leading 0 notation*
:b binary
:x or :X hexadecimal (upper/lower)
:e exponent
:f fixed point, 6 places
:.2f fixed point, 2 places*
:,.2f see above, w/ commas

    • 3 is replacable with any number, ex. 004
    • 2 is replacable, ex. .04
20
Q

3.15 - zyLab

How do you get the 2 rightmost digits of a number?

123 -> 23

A

123 % 100

123 % 100 = 23

21
Q

3.15 - zyLab

How do you shift a number to the right 2 digits?

123 -> 1

A

123 // 100

123 // 100 = 1

22
Q

Extra

What does ** do?

A

exponent

ex. 3 ** 2 = 9

basically, ** can be treated as ^
ex. 3^2 = 9