Python_prograaming_notes_nTWBec
Python_prograaming_notes_nTWBec
books…
name = “PYHTON”
version = 3.6
price = 2000
C = 5+4i
name = “PYHTON”
MULTIPLE ASSIGNEMENT version = 3.6
flag=1
sum=0 sum=0
flag=0
a=0 name, version, flag, sum=“PYTHON”,3.6,1,0
b=0
sum=flag=a=b=0
List
Lists are used to store multiple items in a single
variable.
A list can contain different data types:
What is the data type of a list?
Range of Indexes
# List Concatenation using + operator
Each element in the set must be unique, and the sets remove the
duplicate elements.
Python Keywords
Keywords are predefined, reserved words used in Python programming that have
special meanings to the compiler.
We cannot use a keyword as a variable name, function name, or any other identifier.
Keywords are used to define the syntax of the coding. Keywords are Keywords
are used to define the syntax of the coding. T
All the keywords in python are written in lowercase except True and False. There
are 35 keywords in Python. coding.
Python Identifiers
UNIT 2
30
Scope of the variables
A Python module is a file containing Python definitions and statements
Inside the void function, we have taken two numbers as inputs from the user.
Then, we have casted into int and stored them into two variables named num1
and num2, respectively.
When we have called the void function voidOperation() from inside another
function main_fn(), the voidOperation() function performs the operation to
calculate the sum of two numbers and prints the result on the console.
The first one -- sys.argv[0] -- is the name of Python
script. The other list elements are sys.argv[1] to
sys.argv[n]- are the command line arguments
It stores command-line arguments into a list; we can access it
using sys.argv. This is very useful and a simple way to read
command-line arguments as String.
The getopt module is part of the Python Standard Library, so it comes pre-installed
with Python, and there’s no need to install any external packages to use it
The Python getopt module It allows both short, and long options including a value
assignment.
This module requires the use of the sys module to process input data properly.
It is useful in parsing command line arguments where we want the user to enter some
options.
use getopt module, it is required to remove the first element from the list of command-
line arguments.
Parsing the arguments using getopt.getopt()
Long options are specified with two hyphens (--) before the option name.
If an option requires an argument, it is followed by an equal sign (=). For example, ["help",
"input=", "output="]
specifies that the script should recognize the --help option, the --input option followed by
an argument, and the --output option followed by an argument.
Short options are single-character options that are specified with a single hyphen (-) before
the option letter.
Python argparse module is the most preferred way to parse command-line arguments as
it provides a lot of options such as positional arguments, the default value for arguments,
help message, etc.
add_argument()
# returns "Hello, World!"
we cannot combine strings and numbers
The format() method takes unlimited number of arguments, and are placed into the
respective placeholders:
Python Lambda
This function accepts any count of inputs but only evaluates and returns one
expression.
That means it takes many inputs but returns only one output.
The power of lambda is better shown when you use them as an anonymous function
inside another function.
use the same function definition to make both
functions, in the same program:
Python Recursion
Function calling itself is Recursion.
A some finite number of steps it will be terminated.
The Array is an idea of storing multiple items of the same type together, making it easier to
calculate the position of each element by simply adding an offset to the base value.
Print each item in the cars array:
Class
•Open a file
The files can be accessed using various modes like read, write, or append.
we have passed filename as a first argument and opened file in read mode as we
mentioned r as the second argument.
The fileptr holds the file object and if the file is opened successfully, it will execute the
print statement
located in the same folder as Python:
If the file is located in a different location, you will have to specify the file path,
like this:
The close() method
demo
demo
application of python in linguistics