Introduction :
Welcome back to my Python journey! Yesterday, I laid the foundation with builtins modules.
Today, I dove into command_line arguments, essential for any programming language. Let's explore what I learned!
Command Line Arguments
.The Argument which are passing at the time of execution are called Command Line Arguments
. Within the Python Program this Command Line Arguments are available in argv. Which is present in SYS Module.
. argv is not Array it is a List.
Example
:
To check type of argv from sys
import argv
print(type(argv))
Note:
argv[0] represents Name of Program. But not first Command Line Argument.
argv[1] represent First Command Line Argument
Example:
Write a Program to display Command Line Arguments
from sys import argv
print(“The Number of Command Line Arguments:”, len(argv))
print(“The List of Command Line Arguments:”, argv)
print(“Command Line Arguments one by one:”)
for x in argv:
print(x)
: Within the Python program command line arguments are available in the String form.
Based on our requirement, we can convert into corresponding type by using type casting methods.
Example
:
from sys import argv
print(argv[1], type (argv[1]))
Example :
Write a program to find sum of all arguments
from sys import argv
sum=0
args=argv[1:]
for x in args :
n=int(x)
sum=sum+n
print("The Sum:",sum)
challenges:
Understanding commandline argument.
Handling coding errors.
Resources:
Official Python Documentation: commandline argument
W3Schools' Python Tutorial: commandline argument
Scaler's Python Course: commandline argument
Conclusion:
Day 22 was a success!
commandline argument are now under my belt.
Connect with me:
LinkedIn : [ https://www.linkedin.com/in/archana-prusty-4aa0b827a/ ]
GitHub: [ https://github.com/p-archana1 ]
Join the conversation:
Share your own learning experiences or ask questions in the comments.
Next Post: Day 22: we'll learn File Handling.
Happy learning!
THANK YOU!!