Introduction Flashcards

1
Q

How to use a particular function within a given library, for example, the square root function (sqrt) from the math library?

A

Use the from and import keywords.

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

What is the REPL?

A

It is the interactive Python shell that allows me to run statements one at a time in a command-line-like interface. REPL stands for Read, Evaluate, Print and Loop.

It is very useful for debugging and “prototyping” code without having to save to a file and run the file.

The REPL is one of the ways of running a python program.

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

What are significant whitespaces in Python?

A

Significant whitespaces are the way of demarking specific blocks of code in Python. Such blocks of code can be used to control program flow or define objects.

Both tabs and four spaces are accepted valid options for significant whitespaces in Python, with the latter being the preferred way.

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

How the use of whitespaces in Python differs from other languages in terms of demarking blocks of code?

A

In other programming languages, braces are used to demark particular blocks of codes instead of whitespaces. For example, curly braces are used in C#.

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

How to get more information on a particular library? Additionally, how can I list all the functions in a library?

A

To get more information, use the help function. Passing only the name of the library to the help function will list all of its functions.

>>> help( math )

After importing the function, I can pass the function’s name to get details on the function

  • >>> import math*
  • >>> help(math.sqrt)*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to declare and run a for loop in the REPL?

A
  1. Enter the initial for loop statement, then press enter.
  2. Use four spaces (preferred) or tab to indent the block of code inside the for loop, after each line of code inside the loop, press enter.
  3. After the last statement, insert a blank line a press enter to run the loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to close the REPL in both Windows and Mac?

A

Windows: CTRL-Z

Mac: CTRL-D

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

How to check the current version of Python installed in my OS?

A

>>> python –version

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

How does the Python interpreter work?

A

The interpreter is a program that will convert my human-readable code into a ‘pseudo’ machine code. The interpreter will then execute this intermediate code.

The original code I wrote is saved into a .py format. The interpreter will create a new file with a .pyc format and this is what is going to be executed. If I re-run without making any changes, the interpreter will spot that no changes were made and will execute the existing .pyc file again, no need to create a new .pyc file.

The interpreter will handle all the validation checks to confirm the code I wrote has been written correctly.

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

What is the type from the output of the given operation?

>> 3.0 + 1

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

List the 4 scalar built-in types in Python

A

The four built-in scalar types are:

  1. int
  2. float
  3. None
  4. bool
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the None built-in type.

A

The None type is often used to represent the absence of a value, therefore passing None as input to the REPL will not return anything.

>> None

>>

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

How to test if a variable is of the None class?

A

Use the is operator

>> a = None

>> b = 52

>> a is None

>> True

>> b is None

>> False

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

Is it possible to create an int or a float from a string?

A

Yes, it can be done via either the int() or the float() functions.

>> int(“42”)

>> 42

>> float(“5.879”)

>>5.879

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

How to convert from other built-in types to bool?

Explain how the conversion works for:

  1. Collection type (e.g. strings or lists)
  2. Scalar type (e.g.int and floats)
  3. Special case: “True”and “False”strings
A

The conversion can be accomplished using the bool construct (see below)

>> bool(1.5)

>> True

Additionally,

  1. Any empty collection will be converted to false while non-empty collections, to true.
  2. Zero int or float will be converted to false, while non-zero values, to true.
  3. Both “True” and “False”strings will be converted to true since both are non-empty strings/collections.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are relational operators? List some basic examples of relational operators.

A

Relational operators are used to evaluate the relationship between two objects.

>> g = 20

>> g == 20

>> True

17
Q

How to terminate an infinite loop?

A

Press CTRL + C

18
Q

How does the REPL display the None type?

A

None is the absence of value and therefore the REPL does not display anything when the None type is returned. The REPL will simply show the >>>, signaling that is waiting for new input.

19
Q

When a function has no explicit return statement what will it return after it has been called?

A

The function will return the None type

20
Q

What does the launch_missiles function return?

A

The function returns None as there is no explicit return statement. The message printed to the terminal is actually a side effect from the function, not to be considered as a value returned from the call to the function.

By assigning an arbitrary variable to the function call and testing for None, we see that the function indeed does not return any value.

21
Q

How to pronounce the following?

__main__

A

dunder main

Dunder = double underscore

22
Q

What will happen if I ran the words.py file in the terminal?

$ words.py

A

The Terminal will not return anything since the module does not have any explicit return statements or side effects (e.g. print statements).

23
Q

What will happen if I import the words module in the REPL?

  • >>> python3*
  • >>> import words*
A

The REPL will not return anything since the module does not have any explicit return statements or side effects (e.g. print statements).

24
Q

What will happen if I ran the words.py file in the terminal?

>>> python3 words.py

A

The Terminal will automatically print each and every word in story_words.

25
Q

What will happen if I import the words module in the REPL?

>>> python3

>>> import words

A

The REPL will automatically print each and every word in story_words.

26
Q

Why does importing everything from a module like shown below is not usually recommended?

>> from module_name import *

What would be safer and better alternatives?

A

Since there is no alias, this procedure could potentially create name conflicts in two scenarios

  1. Creates conflicts with names previously defined
  2. Will create conflicts in cases a function with the same name is created in the future.

Instead, I could do either of the following. The second option will import everything from the module, but now under a particular alias, forcing me to use fully qualified names.

  1. >>> from module_name import (function1, function2, function3, etc.)
  2. import module_name as alias
27
Q

Explain what is the shebang and its purpose

A

Shebang is a special COMMENT added as the FIRST LINE OF CODE of a script file. It is a comment since it uses the # symbol.

The purpose of the shebang is to tell the program loader, for example, the terminal on macOS, which interpreter to use to run the script. If the shebang is not added, I need to invoke the Python interpreter myself before running a python script file.

Example:

#! /usr/bin env python 3

28
Q

What are the steps to correctly use the shebang in a script file?

A
  1. Add the shebang as the first line of code from the script.
  2. Then, I need to mark the script as executable by running the following command on the Terminal (macOS)
    * chmod +x [filename]*

The script can now be run directly as in

/usr/python-project/python-script.py

29
Q

What is the meaning behind the shebang?

A

represent the “she”

! represents the “bang”

30
Q

What are Docstrings and which things they can be added to in Python?

A

Docstrings are special COMMENTS that provide an explanation about a given object or module, how it behaves, and its arguments.

Docstrings can be added to modules, functions and classes.

31
Q

How to add Docstrings to:

  1. Functions and classes
  2. Modules
A

Docstrings are added by using triple quotes “”” and adding them to the top of the block of code.

  1. Add it as the top line of a module or function
  2. In case there is no shebang, then the Docstring will be the first line of the code. Otherwise, it will be the second line.
32
Q

How to access the Docstrings from an object?

A

Use the help( ) function.

33
Q

Is the output from the square( ) function, which is 25 for this example, considered a returned value? If not, what is it?

A

Yes, the definition of this function explicitly defines a value to return.

34
Q

Explain how is the __name__ built-in attribute set differently depending on whether a Python file was run as a script or imported as a module.

A

In case the file has been ran as a script, __name__ will be set to “__main__”. This would happen, for example, if I executed the following command on the Terminal:

$ python3 file_name.py

On the other side, if a Python file is imported as a module into another file, then __name__ will be set to the name of the module itself.

35
Q

Explain the output from running the file_one.py as a script in the Terminal

A

Since file_one.py was run as a script, the interpreter will set its __name__ attribute to ‘__main__’.

It will then import file_two.py as a module for file_one.py, therefore it will set __name__ for file_two to ‘file_two’.

It will then print the message shown on line 3 of file_two.py. Since the __name__ of file_two is set to the file’s name, the if condition will evaluate to false and the else statement will be executed.

Then, the interpreter will go back to file_one.py and print the message on line 5 of it. Finally, the if statement will evaluate to True, thus executing its statement.

Program executions ends.

36
Q

How to import a limited number of functions from a given module?

A

After the import keyword, add round brackets and pass the desired function names separated by a comma.

$ from module_name import (function1, function2, function3)

37
Q

What is the purpose of importing the sys module and assigning the url variable to sys.argv[1]?

A

The sys module provides access to the variables used and maintained by the Python interpreter, this includes the ones created from the user’s input.

argv is the list of arguments passed to the script. Since it is a list, its elements can be accessed using square brackets and indices.

argv[0] is the name of the script

argv[1] is the first argument passed to the script.

38
Q

What happens if I would import the words35.py file into the REPL and then call the main( ) function?

A

An error would be raised since sys.argv[1] does not contain any value in the REPL. As the file was not run as a script, no value had been passed to the module.