Lecture 2 Flashcards

1
Q

In terms of strings, what is slicing?

A

String is an ordered sequence of characters, and therefore we can retrieve the elements of this sequence by referring to their indices.

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

How to slice a string?

A

Use the name of string to be sliced followed by square bracktes with the index of the position we want to return.
~~~
»> str1 = ‘home’
»> str1[1]
o
~~~

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

How to get last element from a string without knowing how long it is?

A

Use index -1 when slicing

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

What happens if I slice using an index that is greater than the length of string minus 1?

A

I will get an IndexError (see below)

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

Is it possible to slice more than one character from a string?

A

string_name[start:stop:step]

Remember: Python will include start index but will stop at stop-1 index.

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

What is the output from the prompt below?

>>> s = 'abcdefgh'
>>> s[3:6:2]
A

‘df’

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

What is the output from the prompt below?

>>> s = 'abcdefgh'
>>> s[::-1]
A

‘hgfedcba’

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

What is the output from the prompt below?

>>> s = 'abcdefgh'
>>> s[4:1:-2]
A

‘ec’

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

What is the output from the prompt below?

>>> s = 'ABC d3f ghi'
>>> s[6:3]
A

'' - empty string

  • Start position is f.
  • Step is not specified so it is set to 1 by default.
  • Stopping position is the first space.
    As we are moving to the right (step=1) but stopping point is to the left, there are no characters to return.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Are strings mutable or immutable? What does it mean?

A

Strings are immutable, meaning I cannot change one of its element by value assignment after it has been created.

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

What is Operator Overloading? Explain it through an example.

A

Operator overloading is when an operator behaves differently depending on the object to which it is applied.

For example, the + operator will add two numbers together but it will concatenate strings instead of throwing an error.
~~~
»> “a” + “a”
aa
»> 1 + 2
3
~~~

The * operator is also overloaded, since it is multiplication for number but repetition for strings.

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

What is the outcome from typing the command below in the interpreter?
~~~
»> new_id
~~~
Moreover, explain how Python handles this command.

A

It results in a NameError.
NameError: name 'new_id' is not defined

new_id is not a literal from any built-in type. It is then assumed to be a name, but since it is no bound to any object, Python throws an error since names can only exist bound to an object.

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