Introduction Flashcards
How to use a particular function within a given library, for example, the square root function (sqrt) from the math library?
Use the from and import keywords.
What is the REPL?
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.
What are significant whitespaces in Python?
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 the use of whitespaces in Python differs from other languages in terms of demarking blocks of code?
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 to get more information on a particular library? Additionally, how can I list all the functions in a library?
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 to declare and run a for loop in the REPL?
- Enter the initial for loop statement, then press enter.
- 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.
- After the last statement, insert a blank line a press enter to run the loop.
How to close the REPL in both Windows and Mac?
Windows: CTRL-Z
Mac: CTRL-D
How to check the current version of Python installed in my OS?
>>> python –version
How does the Python interpreter work?
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.
What is the type from the output of the given operation?
>> 3.0 + 1
List the 4 scalar built-in types in Python
The four built-in scalar types are:
- int
- float
- None
- bool
Explain the None built-in type.
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 to test if a variable is of the None class?
Use the is operator
>> a = None
>> b = 52
>> a is None
>> True
>> b is None
>> False
Is it possible to create an int or a float from a string?
Yes, it can be done via either the int() or the float() functions.
>> int(“42”)
>> 42
>> float(“5.879”)
>>5.879
How to convert from other built-in types to bool?
Explain how the conversion works for:
- Collection type (e.g. strings or lists)
- Scalar type (e.g.int and floats)
- Special case: “True”and “False”strings
The conversion can be accomplished using the bool construct (see below)
>> bool(1.5)
>> True
Additionally,
- Any empty collection will be converted to false while non-empty collections, to true.
- Zero int or float will be converted to false, while non-zero values, to true.
- Both “True” and “False”strings will be converted to true since both are non-empty strings/collections.