Introduction to Programming with Python Flashcards

1
Q

List three reasons why Python is a popular language among educators and learners.

A

Python is free, and a variety of supporting tools, modules, and libraries are available at no cost to aspiring developers.

Python’s syntax is concise compared to that of many other languages, which means you can do more with less, reducing the demand on the programmer.

Python code is also easier to read than code written in many other languages because one of the central concepts in the creation of Python was that the code should resemble everyday English.

Python has a very active developer community that creates resources for entry-level and expert Python developers.

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

List three reasons why Python is a popular language among professionals.

A

Despite being relatively easy to use and read, Python is extremely robust and powerful.

Because Python is so easy to use and such a concise language, it is a popular choice for quickly building working prototypes.

Python is the language of choice for data science and is heavily used in artificial intelligence.

Python is already extremely popular among educators, learners, and practitioners, and the trends show that this momentum is only increasing. That means more jobs with higher salaries are available to Python developers. Learning Python can help you pay the bills!

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

What is the Python interpreter?

A

A program that reads Python code, translates it into something called byte code, and then executes the code in a Python Virtual machine, returning the output to the user

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

Which of the following is a difference between interpreted languages and compiled languages? (select all that apply).

A

Compiled languages are processed into machine code and stored as separate executables for users to run.

Interpreted languages are parsed and executed without storing a separate machine code executable.

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

What is the relationship of Jupyter to IPython?

A

Jupyter evolved from IPython.

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

What is JupyterLab?

A

An integrated development environment that allows developers to organize and develop in multiple documents with code, Markdown, and text alongside terminals and other components.

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

NumPy:

A

An extremely efficient library used for the computation of large data sets and multi-dimensional arrays.

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

SciPy:

A

A library used for scientific computing including linear algebra, interpolation, and signal and image processing.

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

Pandas:

A

A library used for data manipulation in numerical tables and time series data.

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

What is Anaconda?

A

Anaconda is a distribution that includes Python and various other tools including SciPy, NumPy, and pandas.

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

When the interpreter gives you an error of “invalid syntax,” what is the most likely cause?

A

I tried to use a variable name that is not valid.

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

What type of value is 0o42?

A

octal

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

What type of value is 42?

A

int

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

What type of value is 4 + 2J?

A

complex

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

What type of value is 4.2?

A

float

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

What is the result of this equation: int(5.8 * 2)?

A

11

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

How would you put quotation marks into a string in Python?

A

my_string = “this has "quotes".”

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

How do you get the length of a string in Python?

A

len(my_string)

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

If there is a string “my_string” with the value “I love Python!” how do you substring to get just “Python!”?

A

my_string[7:]

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

If there is a string “my_string” with the value “I love Python!,” how do you substring to get just “love”?

A

my_string[2:6]

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

What is the purpose of an escape character?

A

To include special characters in a string like a backslash or quotation mark

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

What is the purpose of a collection in Python?

A

to simplify code

to let the programmer combine multiple pieces of data into one variable object

to allow you to organize data more effectively

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

What is a key difference between a dictionary and a set?

A

Dictionaries store elements in key/value pairs, and sets do not.

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

What is the proper way to add the number 24 as an element to the set that I’ve named “my_favorite_numbers”?

A

my_favorite_numbers.add(24)

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

What is the proper way to add the number 24 at index 3 of the list that I’ve named “my_favorite_numbers”?

A

my_favorite_numbers.insert(3, 24)

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

What is the command to open a file in Python?

A

open

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

What does “x” do when used with the file open method?

A

Creates a new file with the specified file name; if it already exists, you will get an error message

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

What does “r” do when used with the file open method?

A

opens the file in read mode

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

What is the fastest way to initialize four variables (a, b, c, and d) all equal to 5?

A

a = b = c = d = 5

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

Which of the following lines of code will print the output of all four variables (a, b, c, and d)?

A

print(a, b, c, d)

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

I have a variable called my_int that holds the integer value 42. I want to convert that value to a string data type and assign that value to a variable called my_str. Which of the following will do that?

A

my_str = str(my_int)

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

What is the difference between an expression and a statement in Python?

A

A statement is a line of code the interpreter can execute, an expression is a section of code that the interpreter evaluates to a value.

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

What is a StopIteration exception in Python?

A

An error code received from the interpreter when moving past the end of a list

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

I have a list called “my_numbers” that contains the values 4, 8, 15, 16, 23, and 42. I want to loop through that list using an iterator. Each time through the list, I want the variable “next_value” to contain the next value in the list. Which of the following lines of code will do that?

A

for next_value in my_numbers:

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

I have a list called “my_list” that contains four values (1, 2, 3, and 4). I created an iterator for my_list called “my_iterator.” What happens when I call next(my_iterator) five times in a row?

A

The values 1, 2, 3, and 4 will be printed, and then a StopIterator error will be shown.

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

I have a list called “my_list” that contains four values (1, 2, 3, and 4). I want to create a new list called “my_new_list” from my existing list “my_list.” I want my_new_list to contain only the even values from my_list, and I want each of those values to be multiplied by 100. Which of the following will accomplish that using a list comprehension?

A

my_new_list = [n*100 for n in my_list if n% 2 == 0]

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

What is scope?

A

Scope is the sections within code for which a variable or function is defined.

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

What is the result of the following code?
a = 1
b = 2
c = a + b
d = a + e

A

an error

39
Q

What is the result of the following code?
def my_function():
a = 5
b = 5

a = 1
b = 1
my_function()
print(a + b)

A

2

40
Q

What is the result of the following code?
a = 1
b = 1
add_them()
print(a + b)

def add_them():
a = 2
b = 2

A

an error

41
Q

I want to define a function called “two_step” that takes two arguments called arg1 and arg2. Which of the following will do that?

A

def two_step(arg1, arg2):

42
Q

I want to define a function called “two_step” that takes two arguments called arg1 and arg2. I want arg2 to have a default value of 12. Which of the following will do that?

A

def two_step(arg1, arg2 = 12):

43
Q

I have a function called “four_names” that takes four arguments a, b, c, and d. All four arguments are default arguments. Which of the following are valid function calls to four_names?

A

four_names(c=”Billy”)

four_names(“Bob”, “Billy”)

four_names()

four_names(“Bob”)

44
Q

I want my function to return the value stored in a variable called “result.” I use the line of code return result to do so. What will happen in my code?

A

After the value is returned, any code in the function block after the return call will not be executed.

The value stored in result will be returned to the point at which the function was called.

45
Q

What are two classifications of errors in Python?

A

syntax errors and exceptions

46
Q

What does an “invalid syntax” error message mean?

A

Something in your code does not conform to the Python programing syntax rules and cannot be interpreted properly.

47
Q

Which of the following will cause an exception in Python programming?

A

an error that terminates the program during program execution

48
Q

Dividing by zero will result in what type of error?

A

ZeroDivisionError

49
Q

I’m writing code where I’ll be accessing a file. It’s a likely place for a file input/output error to occur. What’s the best way to handle that?

A

Place all file I/O code in a try/except block and specify file-related errors in the except block. This will catch file errors we anticipate, but nothing else.

50
Q

What does the finally clause do within a try block?

A

It contains code that executes after a try block regardless of whether an exception occurs. The code in a finally block will always be executed after the try/except block.

51
Q

There are five levels of logging criticality. What are those levels in order from least to most critical?

A

debug, info, warning, error, critical

52
Q

If we want to use logging, we must use the statement: import logging. What does that statement do?

A

imports logging libraries that you can use in Python code

53
Q

Which line of code below will set the log level to Warning?

A

logging.basicConfig(level = logging.WARNING)

54
Q

Besides using whatever text you wish to describe the error, is there a way to further customize the log messages?

A

Yes, you can pass in attributes representing things such as time of the error, line number, process ID, etc.

55
Q

\

A

A single backslash character

56
Q

\’

A

A single quote

57
Q

\”

A

A double quote

58
Q

\n

A

An ASCII new line character

59
Q

\r

A

An ASCII carriage return character

60
Q

\t

A

An ASCII tab character

61
Q

Get the length of a string

A

len(“Hello, World!”)
13

62
Q

Get the index – i.e. the position - of a character within a string. Counting starts at 0.

A

my_string = “Hello, World!”
my_string.index(‘e’)
1

63
Q

Count the number of matching characters in a string

A

my_string = “Hello, World!”
my_string.count(‘o’)
2

64
Q

Convert all characters to lowercase

A

my_string = “Hello, World!”
my_string.lower()
‘hello, world!’

65
Q

Convert all characters to uppercase

A

my_string = “Hello, World!”
my_string.upper()
‘HELLO, WORLD!’

66
Q

Appends an element to the end of the list

A

append(element)

67
Q

Inserts an element at index i in the list

A

insert(i, element)

68
Q

Removes an element from the list

A

remove(element)

69
Q

Removes all elements from the list

A

clear( )

70
Q

Returns the number of times a particular element appears in the list

A

count(element)

71
Q

Gets the log entry creation date and time

A

asctime
%(asctime)s

72
Q

Gets the name of the file

A

filename
%(filename)s

73
Q

Gets the name of the function in which the log entry was created

A

funcName
%(funcName)s

74
Q

Gets the line of code where the log entry was created

A

lineno
%(lineno)d

75
Q

Gets the process ID that created the log entry

A

process
%(process)d

76
Q

Gets the process name that created the log entry

A

processName
%(processName)s

77
Q

What are some ways the interpreter helps you find the cause of a syntax error?

A

The phrase “syntax error” itself helps you understand that something in the code goes against Python programming rules.

The interpreter provides an arrow that always points at the exact location of the error.

78
Q

Python namespaces include the following:

A

built-in namespace: contains all built-in functions and exceptions.

global namespace: contains all the names of variables and functions you create in your program that exist outside of functions

local namespace: contains only names within a given function.

79
Q

Namespace definition

A

In Python, a namespace represents a way of outlining categories of names to reduce name reuse and overwriting.

80
Q

Built-in

A

Contains all built-in functions and exceptions
print

81
Q

Global

A

Contains all the names of variables and functions you create in your program that exist outside of functions
my_str
print_it_x_times
print_times

82
Q

Local

A

Local namespace: Contains only names within a given function
str
x
a

83
Q

What is the benefit of importing an entire library rather than just importing selected functions from that library?

A

Importing the entire library reduces the likelihood that a name conflict will exist in the global namespace by making use of the dot notation.

84
Q

What if we wanted to import the entire soccerstats library? Which line of code will import that library?

A

import soccerstats

85
Q

Morgan and Kyle developed a soccer statistics library for Python called “soccerstats.” We want to import a function from that library called “score_chance.” Which line of code will import only that function?

A

from soccerstats import score_chance

86
Q

What is the purpose of a namespace?

A

provides a way to make variable and function names unique and prevent their duplication across modules and packages

87
Q

I have documented a function called “foo” with a docstring. Which of the following will display my function and associated docstring?

A

help(foo)

88
Q

Which of the following is a single-line docstring in Python?

A

””” This is a docstring! “””

89
Q

Which of the following is a valid comment in Python?

A

This is a comment!

90
Q

Which library provides scientific functions for linear algebraic functions, interpolation, signal processing, and more?

A

SciPy

91
Q

Which library provides a wide range of data types and functions for programming data science applications in Python?

A

NumPy

92
Q

Which library provides the fast, efficient, and easy-to-use DataFrame data structure?

A

pandas

93
Q

Which library provides machine-learning algorithms for Python programmers?

A

scikit-learn

94
Q

Which library provides a wide range of graphing functions to visualize data?

A

Matplotlib