In Python, there are several ways to take input from the user. Here are a few common methods:
- Using the input() function: The input() function is used to take input from the user. It reads a line of text entered by the user and returns it as a string. Here’s an example:
-
name = input("Enter your name: ") print("Hello, " + name + "!")
- Using command-line arguments: Command-line arguments are passed to a Python script when it is executed. The
sys
module provides access to these arguments through thesys.argv
list. Here’s an example:
- Using command-line arguments: Command-line arguments are passed to a Python script when it is executed. The
import sys arg1 = sys.argv[1] arg2 = sys.argv[2] print("Argument 1:", arg1) print("Argument 2:", arg2)
You can then run the script from the command line like this:
python script.py argument1 argument2
- Using a file: You can also take input from a file using the
open()
function. Here’s an example:
with open("input.txt") as f: lines = f.readlines() for line in lines: print(line.strip())
This reads the lines from the input.txt
file and prints them to the console.
How to check Python version?:
In order to check the version of Python installed on your system, you can use the following methods:
Method 1: Using the command line
Open a command prompt (Windows) or terminal (Mac/Linux) and type the following command:
python --version
This will output the version of Python installed on your system.
Method 2: Using Python code
Open the Python interpreter or create a new Python file and type the following code:
import sys print(sys.version)
This will output the version of Python installed on your system, along with additional information such as the platform and build details.
You can also use the platform
module to get additional information about the system, such as the operating system and architecture:
import platform print(platform.python_version()) print(platform.system()) print(platform.machine())
This will output the version of Python installed on your system, as well as the operating system and architecture.