General Python Flashcards

1
Q

What function allows you to check whether or not a variable is part of a specific class?

A

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

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

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).?

A

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

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

What process removes duplicates from a list?

A

Converting from a list to a set removes duplicate elements.

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

How do you convert a list to a set?

A

> > > animals = [‘tiger’, ‘tiger’, ‘lion’, ‘giraffe’, ‘lion’]
set(animals)
{‘tiger’, ‘lion’, ‘giraffe’}

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

Why would you convert a list into a tuple?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you convert a list into a tuple?

A

> > > animals = [‘tiger’, ‘lion’, ‘giraffe’, ‘lion’]
tuple(animals)
(‘tiger’, ‘lion’, ‘giraffe’, ‘lion’)

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

What are the key Set operations?

A

The key set operations are union, intersection and difference.

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

What does union do?

A

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2

> > {1, 2, 3, 4, 5}

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

What does intersection do?

A

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection = set1 & set2

> > {3}

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

What does difference do?

A

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference = set1 - set2

> > {1,2}

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

What are *args and **kwargs?

A

*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

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

What is the basic syntax of a List comprehension?

A

The basic syntax of a list comprehension is:

[expression for item in iterable if condition]

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

What does “python -m” do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you check if any python packages are outdated?

A

To check which packages in your virtual environment are outdated, you can run:

pip list –outdated

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

How do you update any out of date packages?

A

To update all outdated packages:

pip install –upgrade <package_name></package_name>

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

How do you create a local git repo?

A

Inside your project folder, run the following command to initialize a Git repository:
git init

17
Q

How do you add the files that you want git to track?

A

Add the files you want to track to Git using:
git add .

This stages all the files in your project for committing. You can also add specific files by replacing . with the file name.

18
Q

How do you commit the staged files in git?

A

Commit the staged files with a message describing the changes:

git commit -m “Initial commit”

19
Q

How do you push from your local repo to a remote repo?

A

If you want to back up your code to a platform like GitHub, you can create a remote repository and link it to your local one:

git remote add origin <repository_url></repository_url>

Afterward, you can push your code to the remote:
git push -u origin master

20
Q

How would you list all previous commits?

A

To view the commit history of your repository, use the following command:

git log

This command will show a detailed log of commits, starting from the most recent. Each commit will be listed with the following information:

Commit hash: A long alphanumeric string identifying the commit.
Author: The name of the person who made the commit.
Date: The date and time the commit was made.
Commit message: A description of what was changed.
For a more concise view, use:

git log –oneline

This will show the commits in a simplified format with just the commit hash and message, making it easier to read through the commit history.

21
Q

How would you rollback to a specific commit?

A

Soft Reset: Keeps your changes in the working directory, but resets the commit history.

git reset –soft <commit-hash></commit-hash>

This will undo commits but keep the changes staged, so you can re-commit them.

Mixed Reset (default): Keeps your changes in the working directory but unstages them.
git reset <commit-hash></commit-hash>

This will undo the commits and move the changes to your working directory without staging them.

Hard Reset: Resets the commit history and removes all changes in the working directory (use with caution).

git reset –hard <commit-hash></commit-hash>

22
Q
A