Wednesday, February 24, 2021

Python - Command line arguement

 Command line arguements
add.py
> python add.py 10 20
         argv[0] argv[1]   argv[2]

pre defined module sys module
in sys module has argv list

stores command line arguements.

The commmand line arguements passed to the python program are stored as strings
in a list called argv available in sys module.


Program:
Write a program to add two numbers using command line arguements.

you can also supply arguement through input function


Comments " """
"""
This program adds two  numbers using
command line arguements.
"""

# First import sys module
import sys
# index starts with 0
# type conversion
# x=argv[1]
# y=argv[2]
# you have to call like sys. becaause sys is a module
#x=sys.argv[1]
#x=sys.argv[2]
# but these values are string when they come in
# and we have to convert to integer
x=int(sys.argv[1])
y=int(sys.arg[2]
x=x+y
print('The Sum is = ',x)


now, save it 'add.py'

Now, go to command prompt and execute the program
> python add.py 100 200
you will see the result

WE are passing three arguements
filename first_value second_value
index0  index1 index3

What happens if you supply Four values
-> The last one will be ignored because we haven't define the 4th index.


Data Types in Python

Dynamimic type language
when you assign a value, python automatically detects the type of value. even you change it, it will automatically knows.
Python will automatically knows what type of data it it.
for eg,
x=10
x="John"

python will change the data type automatically.
for eg, we have x=10 - int
and we change to x="John" it changes to string

Some data types in python
1. int
2. Fload
3. Complex
4. str - string
5. bool - boolean - true/false
6. list
7. tuple
8. set
9. frozenset
10. dict - dictionary

Now, let us categorize them,
int, fload, complex => These are numeric type
str - text type
bool - boolean type (Can only store True or False)
list, tuple even sometime str - sequence types
set, frozenset -> set types
dict - mapping type

Note:
list,tuple, set, frozenset, dict are called collection type in python

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...