Skip to main content

iostream

Project description

Python-iostream

Python-iostream is a Python library that provides C++ style input/output streams (cin and cout), making it easier for C++ programmers to transition to Python or for anyone who prefers the C++ I/O syntax.

Features

  • C++ style I/O operations: Use cout << for output and cin >> for input
  • Formatting manipulators: Support for endl, hex, dec, oct similar to C++
  • Type conversion: Automatic type conversion for input values
  • Convenient input variables: Create variables that can be directly used with cin >>
  • Operator overloading: Full support for arithmetic operations with input variables

Installation

You can install python-iostream using pip:

pip install python-iostream

Quick Start

Here's a simple example showing how to use the library:

from iostream import cout, cin, endl, hex, dec, oct, var

# Output examples
cout << "Hello, World!" << endl
cout << "Integer: " << 42 << endl
cout << "Hexadecimal: " << hex << 42 << dec << endl  # Hexadecimal output
cout << "Octal: " << oct << 42 << dec << endl        # Octal output

# Input examples
name = var("")
age = var(0)

cout << "Enter your name: "
cin >> name  # No assignment needed, just like C++
cout << "Hello, " << name << "!" << endl

cout << "Enter your age: "
cin >> age
cout << "You are " << age << " years old."
cout << " In 10 years, you will be " << (age + 10) << " years old." << endl

# Reading multiple values
numbers = [0, 0, 0]
cout << "Enter three numbers: "
cin >> numbers
cout << "You entered: " << numbers[0] << ", " << numbers[1] << ", " << numbers[2] << endl

Detailed Usage

Output with cout

The cout object provides a C++ style output stream:

cout << "Text" << endl  # Output text followed by a newline
cout << 42 << " is the answer" << endl  # Mix different types
cout << hex << 255 << dec  # Output in hexadecimal (then switch back to decimal)

Input with cin and var()

Use the var() function to create variables that can be directly used with cin >>:

# Create input variables with default values
name = var("")  # String variable
age = var(0)    # Integer variable
pi = var(0.0)   # Float variable

# Read input into variables
cin >> name
cin >> age
cin >> pi

Formatting Manipulators

The library provides several formatting manipulators similar to C++:

  • endl: Output a newline and flush the buffer
  • hex: Switch to hexadecimal output for integers
  • dec: Switch to decimal output for integers (default)
  • oct: Switch to octal output for integers
cout << "Decimal: " << 42 << endl
cout << "Hexadecimal: " << hex << 42 << dec << endl
cout << "Octal: " << oct << 42 << dec << endl

Reading Multiple Values

You can read multiple values into a list:

values = [0, 0, 0]  # Pre-allocate a list of 3 integers
cout << "Enter three numbers: "
cin >> values  # Will read up to 3 values from input

Arithmetic Operations with Input Variables

Input variables created with var() support standard arithmetic operations:

num1 = var(0)
num2 = var(0)

cin >> num1
cin >> num2

cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl
cout << num1 << " - " << num2 << " = " << (num1 - num2) << endl
cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl
cout << num1 << " / " << num2 << " = " << (num1 / num2) << endl

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cppio-1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cppio-1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file cppio-1.1.tar.gz.

File metadata

  • Download URL: cppio-1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for cppio-1.1.tar.gz
Algorithm Hash digest
SHA256 14847f28345cbe8defb664962eb427a1d6b23bd9fbf7a133f07323406117a262
MD5 c8d4896fa2593a3a6cc2a0c0eb06f3f5
BLAKE2b-256 3289509a925fb51ab4fd3cc531e9f9dadf2b3e7fe3be5067fa9766395ed4be6f

See more details on using hashes here.

File details

Details for the file cppio-1.1-py3-none-any.whl.

File metadata

  • Download URL: cppio-1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for cppio-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26a294885c84b770ae9222ce91d2af3babe37be734b70de0e5aa47dab8ad99d8
MD5 bfe2cbad4eac1f49b85a660f1b025b33
BLAKE2b-256 eb3a2130d60a258dead59acd785a4dd9d851603274d80db56b3e22de8f95327b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page