Unit 3 Flashcards
Types
What is a string?
an immutable sequence of characters stored in a variable
3.1 - String Basics
How do you find the length of a string?
len(string)
3.1 - String Basics
What is string concatenation?
combining string values in an output statement
3.2 - List Basics
What is a container?
a construct which groups related objects together
individual components are called elements
3.2 - List Basics
What is a list?
an ordered mutable container, uses square brackets []
3.2 - List Basics
How do you concatenate lists?
use ‘list1’ and ‘list2’ as list names
list1 + list2
3.2 - List Basics
What are the four commands you can do for lists?
format answers as code
list.append(v)
adds new elementlist.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
3.3 - Tuple Basics
What is a tuple?
an ordered immutable container, uses ()
3.3 - Tuple Basics
What does a named tuple do?
allows a programmer to name each element
Car = namedtuple('Car', ['model', 'price']
blazer = Car('Chevrolet', 32000')
3.4 - Set Basics
What is a set?
a mutable, unordered container with unique elements; uses set()
or {}
{} used for literals
3.4 - Set Basics
What are the four commands you can do for sets?
use ‘setA’, ‘setB, etc. for set examples
set.intersection(setA, setB)
set w/ elements in commonset.union(setA, setB)
set w/ unique elementssetA.difference(setB, setC)
set w/ unique elements from setAsetA.symmetric_difference(setB)
set w/ elements that appear only once in setA/setB
3.5 - Dictionary Basics
What is a dictionary?
a mutable key/value paired collection, uses {'a': 1}
‘a’: 1 is a sample element for a dictionary
3.5 - Dictionary Basics
What is the difference between a key and a value?
key: index, like a term
value: mutable value, like a definition
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.
numbers["one"]
references value from keynumbers['three'] = 3
creates/updates entriesdel numbers['two']
removes an entry
3.6 - Common Data Types
What are the 5 data types?
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
3.8 - Type Conversions
What are the three ways to convert types?
int()
for integers/floats/int stringsfloat()
for integers/floats/fractions/int stringsstr()
for all types
3.9 - Binary Numbers
What is binary?
a base 2 number system; the basis for machine language
ex. 10 = 0110
10 / 8 = 2
2 / 2 = 0
3.10 - String Formatting
How do you use string formatting?
give an example code
`print(f’{value}’)
3.10 - String Formatting
What are the different ways to represent value in text?
10 total options
: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
3.15 - zyLab
How do you get the 2 rightmost digits of a number?
123 -> 23
123 % 100
123 % 100 = 23
3.15 - zyLab
How do you shift a number to the right 2 digits?
123 -> 1
123 // 100
123 // 100 = 1
Extra
What does **
do?
exponent
ex. 3 ** 2 = 9
basically, ** can be treated as ^
ex. 3^2 = 9