Python Flashcards
(136 cards)
What is the Python interpreter and what’s its primary function?
The Python interpreter is a program that executes Python code. Its main function is to read, interpret, and run Python scripts and interactive commands. It works similarly to Node.js for JavaScript, providing an environment to execute your code.
What is the REPL in the context of the Python interpreter?
REPL stands for Read-Eval-Print Loop. It’s an interactive mode of the Python interpreter that reads user input, evaluates it, prints the result, and loops back to read more input. It’s useful for quick testing and experimentation.
How do you execute a Python script from the command line?
You can execute a Python script (e.g., my_script.py) by using the command python my_script.py in your terminal.
What is the significance of file encoding in Python, and what is the recommended encoding?
File encoding ensures that characters are correctly interpreted by the Python interpreter. The recommended encoding is UTF-8, which supports a wide range of characters, including Unicode.
How can you enter interactive mode after running a Python script?
Use the -i option when running the script: python -i my_script.py. This will execute the script and then drop you into the interactive interpreter.
What is PYTHONSTARTUP environment variable used for?
The PYTHONSTARTUP environment variable specifies a file that the Python interpreter will execute when it starts in interactive mode. This allows you to customize your Python environment. It is similar to .bashrc or Node.js initialization scripts.
How do you run optimized code in Python?
You can use the -O flag when executing your code. For example: python -O my_script.py.
What is Python?
Python is a high-level, versatile programming language that, like JavaScript, supports multiple programming paradigms but is particularly renowned for its simplicity, readability, and extensive use in backend web development, data science, and machine learning, offering a complementary skillset to JavaScript for full-stack development.
How does Python’s syntax compare to JavaScript’s?
Python emphasizes readability through indentation (instead of curly braces) and a more natural language-like syntax. While both are dynamically typed, Python’s syntax requires adapting to its unique style for loops, conditionals, and function definitions.
What are Python’s key data structures, and how do they relate to JavaScript? (4)
Python’s key data structures include lists (similar to JavaScript arrays), tuples (immutable lists), dictionaries (similar to JavaScript objects), and sets (collections of unique elements). Understanding these is crucial for efficient data manipulation in Python.
What are Python modules and packages?
Modules are files containing Python code that can be imported and used in other programs. Packages are collections of modules organized in a directory hierarchy. They allow you to organize and reuse code effectively.
What are Python virtual environments, and why are they important?
Virtual environments isolate project dependencies, preventing conflicts between different projects that might require different versions of the same library. This is essential for maintaining project stability.
What are some popular Python web frameworks, and what problems do they solve?
Popular frameworks include Flask (microframework, unopinionated) and Django (full-featured, batteries-included). They provide tools and structure for building web applications, handling routing, database interactions, and more, similar to how Next.js helps with React development.
What Are Python’s Basic Data Types?
Basic data types in Python include numbers (integers and floats), strings (immutable sequences of characters), and lists (mutable collections of items).
How Are Strings Manipulated in Python?
Strings in Python are immutable and can be enclosed in single or double quotes. They can be concatenated with +, repeated with *, and indexed/sliced.
What Are Lists in Python?
Lists are mutable collections of items enclosed in square brackets. They support indexing, slicing, concatenation, and modification through methods like append.
What Is the Difference Between / and // in Python?
The / operator performs floating-point division, while the // operator performs floor division, returning an integer result by discarding the fractional part.
What Is the Role of the % Operator?
The % operator calculates the remainder of an integer division operation in Python.
print(‘C:\some\name’)
vs
print(r’C:\some\name’)
print(‘C:\some\name’) # here \n means newline!
C:\some
ame
print(r’C:\some\name’) # note the r before the quote
C:\some\name
print(“””\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
“””)
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
3 * ‘un’ + ‘ium’
‘unununium’
How to get characters from position 2 (included) to 5 (excluded)
word[2:5]
How to get characters from position 4 (included) to the end
word[4:]
How to get characters from the second-last (included) to the end
‘on’
word[-2:]