Python - Data Types Flashcards

Review Major Concepts of Data Type in the Python Programming Language

1
Q

Research: what is a Data Type in the Python Programming Language.

A

Data values in Python are known as objects; each object, aka value, has a type.

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

List and Describe: each Data Type in the Python Programming Language.

A

Numbers
Sequences
Sets
Dictionaries
None
Ellipsis (…)
Callables
Boolean Values

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

What is the ‘numbers’ data types in the Python Programming Language?

A

The ‘numbers’ data types are built-in numeric types such as integers, floating-point numbers and complex numbers.

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

What is a ‘sequence’ data type in the Python Programming Language?

A

A sequence is a container of ordered of items, indexed by integers.

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

What is a ‘set’ data type in the Python Programming Language?

A

A ‘set’ is a container of an arbitrarily ordered collection of unique items that can be of different types.

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

What is a ‘dictionary’ data type in the Python Programming Language?

A

A ‘dictionary’ is a container of mappings of an arbitrary collection of objects indexed by arbitrary values. An array of key-value pairs.

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

What is the ‘none’ data type in the Python Programming Language?

A

The ‘none’ data type denotes a null object.

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

Describe ‘callable’ data types in the Python Programming Language?

A

A ‘callable’ data type supports the function call operation. Examples are function, generators and methods.

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

What is the ‘boolean’ data type in the Python Programming Language?

A

A ‘boolean’ data type evaluates to either True of False. Any data value in python can be used as a truth value.

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

What are the three built-in types that are numerical data types?

A

The built-in numeric types in Python include integers, floating-point numbers, and complex numbers.

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

List and Describe: the four Integers literals of the numerical data types.

A

Decimal
A decimal literal is a sequence of digits in which the first digit is nonzero.

Binary
A binary literal is 0b followed by a sequence of binary digits (0 or 1).

Octal
An octal literal is 0o followed by a sequence of octal digits (0 to 7).

Hexadecimal
hexadecimal literal is 0x followed by a sequence of hexadecimal digits (0 to 9 and A to F, in either upper- or lowercase).

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

Describe: floating-point literals of the numerical data types.

A

A floating-point literal is a sequence of decimal digits that includes a decimal point (.), an exponent suffix (e or E, optionally followed by + or -, followed by one or more digits), or both.

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

Describe: Complex Number of the numerical data types.

A

A complex number is made up of two floating-point values, one each for the real and imaginary parts.

You can access the parts of a complex object z as read-only attributes z.real and z.imag.

Examples: 0j, 0.j, 0.0j, .0j, 1j, 1.j, 1.0j, 1e0j, 1.e0j, 1.0e0j

You can specify an imaginary literal as any floating-point or integer decimal literal followed by a j or J. The j at the end of the literal indicates the square root of -1, as commonly used in electrical engineering (some other disciplines use i for this purpose, but Python uses j).

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

Name three types of sequences in the Python Programming Language

A

Python has built-in sequence types known as strings (bytes or str), tuples, and lists.

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

Research: the term ‘iterate’, ‘iterable’ and ‘iterator’.

A

An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.

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

What is the difference between a bounded iterable and an unbounded iterable in the Python Programming Language?

A

A bounded iterable is an iterable that eventually stops yielding items.
An unbounded iterable is an iterable that has no limit.

17
Q

What are ‘strings’ in the Python Programming Language?

A

‘Strings’ are classified a ‘Sequences’.
‘Strings’ are objects that contain either a sequence of characters or a sequence of bytes.

18
Q

Research the term ‘immutable’.

A

Wikipedia: an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

19
Q

Are strings ‘mutable’ or ‘immutable’?

A

Strings of both types in Python are immutable: when you perform an operation on strings, you always produce a new string object of the same type, rather than mutating an existing string.

20
Q

What is the difference between a single quoted string, a double quoted string and a triple quoted string.

A

All quoted strings create string literals, but in a triple quoted string, line breaks act as newline characters.

21
Q

What are ‘raw string literals’ in the Python Programming Language?

A

‘Raw string literals’ do not evaluate escape sequences. They are quoted and triple quoted strings preceded with an ‘r’ or ‘R’.

22
Q

What are ‘formatted string literals’?

A

Formatted string literals (commonly called f-strings) let you inject formatted expressions into your string “literals

23
Q

How do you concatenate multiple string literals into one?

A

Use parentheses:

marypop = (‘supercali’ # ‘(‘ begins logical line,
‘fragilistic’ # indentation is ignored
‘expialidocious’) # until closing ‘)’

24
Q

What is a ‘bytes’ object?

A

A bytes object is an immutable ordered sequence of ints from 0 to 255.

25
Q

How do you create a ‘bytes string literal’?

A

A bytes literal has the same syntax as a str literal, prefixed with ‘b’: b’abc’

Also: bytes([97, 98, 99])

Also a ‘raw byte literal’: rb’\ = solidus’

26
Q

What is a ‘bytearray’?

A

A bytearray is a mutable ordered sequence of ints from 0 to 255:
ba = bytearray([97, 98, 99])
ba[1] = 97
print(ba.decode())

27
Q

What is the sequence called ‘tuple’?

A

A tuple is an immutable ordered sequence of items. The items of a tuple are arbitrary objects and may be of different types.

28
Q

What are four ways of creating a tuple?

A

tuple: 100, 200, 300 # Tuple with three items
one-item tuple: (3.14,) # Tuple with one item, needs trailing comma
empty tuple: ()
tuple built-in type: tuple(‘wow’) #creates (‘w’, ‘o’, ‘w’)

29
Q

What is the sequence called ‘list’?

A

A list is a mutable ordered sequence of items. The items of a list are arbitrary objects and may be of different types

30
Q

What are four ways of creating a list?

A

[42, 3.14, ‘hello’] # List with three items
[100] # List with one item
[] # Empty list
list(‘wow’) #creates [‘w’, ‘o’, ‘w’]

31
Q

What is a hash table and what does it mean to be hashable?

A

An object is hashable if it has a hash value that never changes during its lifetime.

32
Q

What are the two types of built-in Sets and what is the difference?

A

‘set’ is mutable and ‘frozenset’ is immutable.

33
Q

What are the two ways to create a set?

A

To denote a (nonfrozen, nonempty) set, use a series of expressions (the items of the set) separated by commas (,) within braces ({}):
{42, 3.14, ‘hello’}

or use the built-in type: set()

34
Q

What are the three ways to create a dictionary?

A

{1:’za’, ‘br’:23} # Dictionary with different key types
{} # Empty dictionary
dict(x=42, y=3.14, z=7) #built-in type
dict([(1,’za’), (‘br’,23)]

35
Q

What code is used to unpack a ‘dict’ into another ‘dict’?

A

unpack a dict’s contents into another dict using the ** operator

d1 = {‘a’:1, ‘x’: 0}
d2 = {‘c’: 2, ‘x’: 5}
d3 = {**d1, **d2}

or use the | operator (python 3.9+)

d4 = d1 | d2

36
Q

How do you create a dictionary using the ‘dict.fromkeys’ method?

A

The first argument is an iterable whose items become the keys of the dictionary; the second argument is the value that corresponds to each and every key (all keys initially map to the same value). If you omit the second argument, it defaults to None.

dict.fromkeys(‘hello’, 2) # Same as {‘h’:2, ‘e’:2, ‘l’:2, ‘o’:2}
dict.fromkeys([1, 2, 3]) # Same as {1:None, 2:None, 3:None}

37
Q

What does the ellipsis (‘…’) mean in python?

A

an indicator of “no value supplied, not even None.” Ellipsis is hashable and so can be used as a dict key

38
Q

How are data values used to indicate True or False?

A

Any nonzero number or nonempty container (e.g., string, tuple, list, set, or dictionary) is true.

Zero (0, of any numeric type), None, and empty containers are false.