Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python I/O and Module Imports: A Practical Guide

Mastering Python I/O and Module Imports

Learn how to use Python’s core I/O functions print() and input(), and discover best practices for importing modules to keep your code clean, reusable, and scalable.

Video: Python Take User Input

Python offers a suite of built‑in functions that simplify everyday tasks. Among them, print() and input() are essential for interacting with users and displaying results.

Printing Output with print()

The print() function sends text to the console (standard output). It accepts multiple arguments and supports several optional parameters that control spacing, line endings, and output destinations.

print('This sentence is output to the screen')

Output

This sentence is output to the screen
a = 5
print('The value of a is', a)

Output

The value of a is 5

The default separator between arguments is a single space, and each call ends with a newline. These defaults can be customized:

print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')

Output

1 2 3 4
1*2*3*4
1#2#3#4&

Formatting Output

Clear and professional output often requires formatting. Python offers several techniques:

Reading User Input with input()

Dynamic programs rely on runtime data. The input() function captures user input as a string:

num = input('Enter a number: ')

Result:

Enter a number: 10

The returned value is always a string. Convert it to the appropriate type with int(), float(), or, when needed, eval() (use cautiously):

int('10')   # 10
float('10') # 10.0
eval('2+3') # 5

Organizing Code with Modules

As projects grow, splitting functionality into separate modules improves maintainability and reusability. A module is a plain .py file containing definitions and statements.

To load a module, use import or from ... import:

import math
print(math.pi)   # 3.141592653589793

or import a specific symbol:

from math import pi
print(pi)        # 3.141592653589793

Python searches for modules along the paths listed in sys.path. You can inspect and modify these paths to include custom directories:

import sys
print(sys.path)

Typical output:

['',
 'C:\\Python33\\Lib\\idlelib',
 'C:\\Windows\\system32\\python33.zip',
 'C:\\Python33\\DLLs',
 'C:\\Python33\\lib',
 'C:\\Python33',
 'C:\\Python33\\lib\\site-packages']

Adding a new directory is straightforward:

sys.path.append('/my/custom/modules')

Following these conventions ensures your code remains modular, testable, and aligned with industry standards.

Python

  1. Input & Output Coupling Techniques for Amplifiers: Capacitive, Direct, and Transformer Methods
  2. C# Fundamentals: Input and Output Essentials
  3. Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
  4. Python Statements, Indentation, and Comments: A Clear Guide
  5. Python Namespaces & Variable Scope: Understanding Names, Bindings, and Scopes
  6. Python Modules: Creation, Importing, and Advanced Usage
  7. Master Java Input & Output: Print, Read, and Format Your Data
  8. Mastering D Latches: Design, Operation, and Key Differences
  9. Mastering Input and Output in C Programming
  10. Python Modules: Organizing Your Code for Clarity and Reusability