General Python Flashcards
What function allows you to check whether or not a variable is part of a specific class?
isinstance() is the function to check if a variable to check the class.
> > > number = 23
isinstance(number, int)
True
isinstance(number, str)
False
isinstance(number, float)
False
What function can you use to find the highest value in an iterable such as a list, a tuple, or a dictionary (for which it returns the maximum key).?
The max() function returns the highest value.
> > > numbers = [1, 2, 3, 4]
max(numbers)
4
> > > numbers = (1, 2, 3, 4)
max(numbers)
4
> > > points = {34: ‘John’, 25: ‘Kate’, 70: ‘Jane’}
max(points)
70
What process removes duplicates from a list?
Converting from a list to a set removes duplicate elements.
How do you convert a list to a set?
> > > animals = [‘tiger’, ‘tiger’, ‘lion’, ‘giraffe’, ‘lion’]
set(animals)
{‘tiger’, ‘lion’, ‘giraffe’}
Why would you convert a list into a tuple?
Converting a list into a tuple can be useful when you need to create an immutable collection of elements from an existing list. Immutable means that the data structure cannot be modified.
How do you convert a list into a tuple?
> > > animals = [‘tiger’, ‘lion’, ‘giraffe’, ‘lion’]
tuple(animals)
(‘tiger’, ‘lion’, ‘giraffe’, ‘lion’)
What are the key Set operations?
The key set operations are union, intersection and difference.
What does union do?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2
> > {1, 2, 3, 4, 5}
What does intersection do?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection = set1 & set2
> > {3}
What does difference do?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference = set1 - set2
> > {1,2}
What are *args and **kwargs?
*args is used to pass positional arguments to a function via a tuple.
**kwargs is used to pass keyword arguments to a function via a dictionary.
Calling the function with different arguments
example_function(1, 2, 3, name=”Alice”, age=30)
Positional arguments (*args):
1
2
3
Keyword arguments (**kwargs):
name: Alice
age: 30
What is the basic syntax of a List comprehension?
The basic syntax of a list comprehension is:
[expression for item in iterable if condition]
What does “python -m” do?
The -m flag in Python allows you to run a module as a script. When you use python -m <module>, Python will locate the specified module and run it as if it were a standalone script, rather than simply importing it.</module>
How do you check if any python packages are outdated?
To check which packages in your virtual environment are outdated, you can run:
pip list –outdated
How do you update any out of date packages?
To update all outdated packages:
pip install –upgrade <package_name></package_name>