Week 3: Loops in Python Flashcards

1
Q

what are print() arguments?

A

print() arguments change the default ways that print() command behaves.

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

what is the end= argument

A

a print() argument that changes the default ending of the print() command.

  • , end=”” removes the default line break and replaces it with nothing.
  • we can replace the line break with whatever we put in the quotation marks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the sep= argument

A

a print() argument that changes the default separator (a single space)

  • sep=”” just removes the separator and replaces it with nothing
  • we can change the separator to be whatever we put in the quotation marks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is string interpolation?

A

when you read in values of variables or expressions into placeholders in the string

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

What are the 3 methods of doing string interpolation/string formatting

A
  1. f-string
  2. % Operator
  3. The .format() method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are f-strings, their syntax, what they are used for, how they work

A

f-strings are strings that start with an f before the quotation marks and values can be interpolated directly into the string by surrounding them with brackets {}

Expressions can also be embedded in f-strings and the result of the expression will be interpolated into the string

Example: Syntax of f-strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are format specifiers? do f-strings support format specifiers?

A

format specifiers give us more control of the output when formatting strings and interpolating values.
f-strings support many different format specifiers

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

what is the :.xf format specifier, when is it used? give an example

A

the :.xf format specifier is used in f-strings to round the value to x decimal places

Example: :.xf format specifier to round values in f strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the % operator do when used on strings, what is the syntax

A

when used on strings, the modulo operator allows for formatting by replacing placeholders with variable values - allows you to interpolate values into strings

reads in the values from left to right in the string in the order you type them

Example: % Operator for string interpolation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some common placeholders using the % operator

A
  • %s : for strings
  • %d : for integers
  • %f : for floating-point
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the .format() method? What is it used for, what is the syntax? What are the 3 different ways to do it?

A

The .format() method is an easy way to format and interpolate values into strings

This method supports 3 different ways to replace brackets {} with values.

  1. Empty replacement fields
  2. Replacement fields with zero-based indices
  3. Replacement fields with Named Arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain how the .format() method works with empty replacement fields. Give an example of the syntax

A

{} act as replacement fields in the string we are interpolating values into.
It reads the values into the string from left to right in the order you type them in the parentheses

Example: Syntax for .format() method with empty replacement fields
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain how the .format() method works with replacement fields with zero-based indices, give an example

A

the first value typed in the parentheses will go to index {0}, the next will go to index{1} and so on

Example: Syntax for .format() method with replacement fields with zero-based indices
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain how the .format() method works with replacement fields with named arguments, give an example

A

it does not matter what order you type the value of the variables in the parentheses because they will go to the replacement field of their name

Example: Syntax for .format() method with replacement fields with named arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Compare the 3 string interpolation methods based on syntax, readability, variable insertion, type safety, performance, expressions, templates/reuse, and legacy

A
  • f-strings are the most readable, it directly embeds expressions, can support full expressions directly, no explicit type required (can use without specifying type), fastest method, no built-in template support, newer (Python 3.6+)
  • % operator is less readable for complex cases, requires placeholders for variable insertion, requires correct types for placeholders, generally slower than f-strings, no expressions - only variables, limited and inflexible, old method but still used
  • .format() method is more readable than % operator, uses {} as placeholders, more forgiving with types, slower than f-strings, can use methods/formatting in {}, can create reusable templates, introduced in Python 2.7
Comparison of String Interpolation Methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly