Python Flashcards

1
Q

What is the difference between tuple and list?

A

Tuple is immutable, list is mutable.
Like Lists, tuples are ordered and we can access their elements using their index values
We cannot update items to a tuple once it is created.
Tuples cannot be appended or extended.
We cannot remove items from a tuple once it is created.

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

You can access values in tuple by using negative index. How?

A

t = (10, 5, 20)

print(“Value in t[-1] = “, t[-1])
print(“Value in t[-2] = “, t[-2])
print(“Value in t[-3] = “, t[-3])

Value in t[-1] = 20
Value in t[-2] = 5
Value in t[-3] = 10

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

What is numpy?

A

Numerical Python / Arrays, matrices, and a variety of mathematical operations that can effectively operate on these arrays.

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

Six common ways to create numpy array

A

Using np.array / np.zeros() / np.ones() / np.full() / np.arange() / np.linspace()

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

8 main features of numpy

A

Arrays
Efficiency
Mathematical Functions
Broadcasting
Integration with other libraries
Multi-dimensional arrays
Indexing and Slicing
Memory Management

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

What is an iterable?

A

any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop. Familiar examples of iterables include lists, tuples, and strings - any such sequence can be iterated over in a for-loop. We will also encounter important non-sequential collections, like dictionaries and sets; these are iterables as well. It is also possible to have an iterable that “generates” each one of its members upon iteration - meaning that it doesn’t ever store all of its members in memory at once.

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

Explain this code : iterable = (a*a for a in range(8))

A

Here’s what’s happening:

range(8): This creates a sequence of numbers from 0 to 7 (8 numbers in total).

(a*a for a in range(8)): This is a generator expression. It’s similar to a list comprehension, but instead of creating a list, it creates a generator object.

The expression a*a is applied to each value a in the range.

The result is assigned to the variable iterable.

What this code does:

It creates a generator that will yield the squares of numbers from 0 to 7.

The squares are not computed immediately, but only when the generator is iterated over.

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