Python Flashcards
What is the difference between tuple and list?
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.
You can access values in tuple by using negative index. How?
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
What is numpy?
Numerical Python / Arrays, matrices, and a variety of mathematical operations that can effectively operate on these arrays.
Six common ways to create numpy array
Using np.array / np.zeros() / np.ones() / np.full() / np.arange() / np.linspace()
8 main features of numpy
Arrays
Efficiency
Mathematical Functions
Broadcasting
Integration with other libraries
Multi-dimensional arrays
Indexing and Slicing
Memory Management
What is an iterable?
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.
Explain this code : iterable = (a*a for a in range(8))
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.