Python Experiment No: 2 Command Line Parameters
AIM:- To write a program in Python to sort a given list of integers in ascending order.
THEORY:-
Python Basics:
Literal Constants
An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string like 'This is a string' or "It's a string!” It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.
Numbers
Numbers in Python are of three types - integers, floating point and complex numbers.
• An example of an integer is 2 which is just a whole number.
• Examples of floating point numbers (or floats for short) are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10-4.
• Examples of complex numbers are (-5+4j) and (2.3 - 4.6j)
Strings in double quotes work exactly the same way as strings in single quotes. An example is
You can specify multi-line strings using triple quotes - (""" or '''). You can use single quotes and double quotes freely within the triple quotes. An example is:
This is a multi-line string. This is the first line.
This is the second line.
" What's your name?," I asked.
He said "Bond, James Bond."
Escape Sequences
Suppose, you want to have a string which contains a single quote ('), you can specify
the string as ' What\'s your name? '
And
"This is the first sentence.\
This is the second sentence."
is equivalent to "This is the first sentence. This is the second sentence.”
Raw Strings
If you need to specify some strings where no special processing such as escape sequences are
handled, then what you need is to specify a raw string by prefixing r or R to the string.
An example is "Newlines are indicated by \n".
Strings Are Immutable
This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.
String Literal Concatenation
If you place two string literals side by side, they are automatically concatenated by Python.
For example, 'What\'s ' 'your name?' is automatically converted in to "What's your name?”
The format Method
Sometimes we may want to construct strings from other information. This is where the format() method is useful.
age = 25
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
Output:
Swaroop is 25 years old
Why is Swaroop playing with that python?
Variables
Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.
Python API Used:
sys — System-specific parameters and functions This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
sys.argv
The list of command line arguments passed to a Python script. argv[0] is the script name.
range
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
len
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection.
Q1) Explain Data Types available with python language.
Q2) Explain Collection Data Type in detail.
Q3) Write different operators available with python language.
Q4) Explain different methods available list and tuple.
THEORY:-
Python Basics:
Literal Constants
An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string like 'This is a string' or "It's a string!” It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.
Numbers
Numbers in Python are of three types - integers, floating point and complex numbers.
• An example of an integer is 2 which is just a whole number.
• Examples of floating point numbers (or floats for short) are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10-4.
• Examples of complex numbers are (-5+4j) and (2.3 - 4.6j)
Single Quotes
You can specify strings using single quotes such as 'Quote me on this'. All white space i.e.
You can specify strings using single quotes such as 'Quote me on this'. All white space i.e.
- spaces and tabs are preserved as-is.
Strings in double quotes work exactly the same way as strings in single quotes. An example is
- "What's your name?"
You can specify multi-line strings using triple quotes - (""" or '''). You can use single quotes and double quotes freely within the triple quotes. An example is:
This is a multi-line string. This is the first line.
This is the second line.
" What's your name?," I asked.
He said "Bond, James Bond."
Escape Sequences
Suppose, you want to have a string which contains a single quote ('), you can specify
the string as ' What\'s your name? '
And
"This is the first sentence.\
This is the second sentence."
is equivalent to "This is the first sentence. This is the second sentence.”
Raw Strings
If you need to specify some strings where no special processing such as escape sequences are
handled, then what you need is to specify a raw string by prefixing r or R to the string.
An example is "Newlines are indicated by \n".
Strings Are Immutable
This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.
String Literal Concatenation
If you place two string literals side by side, they are automatically concatenated by Python.
For example, 'What\'s ' 'your name?' is automatically converted in to "What's your name?”
The format Method
Sometimes we may want to construct strings from other information. This is where the format() method is useful.
age = 25
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
Output:
Swaroop is 25 years old
Why is Swaroop playing with that python?
Variables
Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.
Python API Used:
sys — System-specific parameters and functions This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
sys.argv
The list of command line arguments passed to a Python script. argv[0] is the script name.
range
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
len
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection.
Q1) Explain Data Types available with python language.
Q2) Explain Collection Data Type in detail.
Q3) Write different operators available with python language.
Q4) Explain different methods available list and tuple.
Comments
Post a Comment