Importing C++ programs in Python - Formula Sheet
Key Syntaxes and Module Formulas
C++ Script Execution Command:
python filename.py -i cpp_filename
python: Command keyword to invoke the Python interpreter.filename.py: The name of the Python wrapper script being executed.-i: Input flag specifying that the next parameter is the input C++ file.cpp_filename: The name of the C++ source file (without the .cpp extension).
Module Function Access Formula:
module_name.function_name()
module_name: Name of the imported standard or user-defined module..(Dot Operator): The binding operator used to access functions within modules.function_name(): The function defined inside the imported module.
C++ Compilation Command Formula:
os.system('g++ ' + inp_file + ' -o ' + exe_file)
os.system(): The method that executes shell commands from Python.g++: The GNU C++ compiler command.inp_file: Variable holding the input C++ file name with the .cpp extension.-o: Output flag specifying that the following argument is the output executable name.exe_file: Variable holding the compiled executable file name with the .exe extension.
Command-Line Argument Parsing Formula:
opts, args = getopt.getopt(argv, options, [long_options])
opts: Returned list of option-value pairs (e.g., [('-i', 'path')]).args: Returned list of remaining arguments or errors (empty if parsing succeeds).argv: The list of arguments to be parsed, typically sys.argv[1:].options: String representing single-character options (e.g., 'i:'). A colon indicates the option requires an argument.long_options: List of long options (e.g., ['ifile=']). An equals sign indicates the option requires an argument.
Main Script Execution Conditional block:
if __name__ == '__main__': main(sys.argv[1:])
__name__: Built-in variable evaluating to the current module's name.'__main__': The value assigned to __name__ when the script is run directly.sys.argv[1:]: Slices command-line arguments to exclude the script name itself (sys.argv).
More for this chapter
Book Back Questions10 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Science
View all
1 Function
2 Data Abstraction
3 Scoping
4 Algorithmic Strategies
5 Python -Variables and Operators
6 Control Structures
7 Python functions
8 Strings and String manipulation
9 Lists, Tuples, Sets and Dictionary
10 Python Classes and objects
11 Database Concepts
12 Structured Query Language (SQL)
13 Python and CSV files
15 Data manipulation through SQL
16 Data visualization using pyplot: line chart, pie chart and bar chart