Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python_prograaming_notes_nTWBec

Uploaded by

muralidhar2705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python_prograaming_notes_nTWBec

Uploaded by

muralidhar2705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 316

Refer the above text books and reference

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

# List repetition using * operator


Change the second value by replacing it
with two new values:
Python Tuples
A Python set is the collection of the unordered items.

Sets are written with curly brackets.

Each element in the set must be unique, and the sets remove the
duplicate elements.

* Note: Set items are unchangeable, but you can remove


items and add new items
Python Keywords and Identifiers

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

Creating python programs


Control flow statements:
If you try to call the function with 1 or 3 arguments, you will get an error:
Arbitrary Arguments, *args

If you do not know how many arguments that will be passed


into your function, add a * before the parameter name in the
function definition.
This way the function will receive a tuple of arguments, and can access the
items accordingly:
The return statement, which is supplied as output when a
particular job or function is finished, might take the form of
an argument, a statement, or a value.

A declared function will return a None object if no return


statement is written.
Python abs() Function
Local and Global variables
20

30
Scope of the variables
A Python module is a file containing Python definitions and statements

A document with definitions of functions and various statements written


in Python is called a Python module.

A file containing a set of functions you want to include in your


application.

A module is a file containing Python code, definitions of functions,


statements, or classes. An example_module.py file is a module.

A module can define functions, classes, and variables.


Save this code in a file named mymodule.py
We can import the functions, and classes defined in a module to
another module using the import statement in some other Python
source file.
Save this code in a file named mymodule.py
['DEV_NULL', '_UNIXCONFDIR', 'WIN32_CLIENT_RELEASES', 'WIN32_SERVER_RELEASES',
'__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__',
'__package __', '__spec__', '__version__', '_default_architecture', '_dist_try_harder',
'_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser',
'_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml',
'_node', '_norm_version', '_perse_release_file', '_platform', '_platform_cache',
'_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists',
'_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname',
'_syscmd_ver', '_uname_cache', '_ver_output', 'architecture', 'collections', 'dist', 'java_ver',
'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen',
'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation',
'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys',
'system', 'system_aliases', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']
Python’s from statement lets you import specific attributes or specific names from
a module without importing the module as a whole.
Void function in Python is a function that performs an action but does
not return any computed or final value.

It can accept any number of parameters and perform operation inside


the function. A void function may or may not return a value.

If we do not declare return statement to return a value, Python will


send a value None.
we have declared a void function named voidOperation(), which does not return
any computed value but performs basic operation inside it.

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()

Arguments with corresponding values are stored in two


variables named as opts and args
getopt() takes an optional third argument, longopts, which is a list of long
options that the script should recognize.

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.

From that termination it will be returned to the


previous functions.
Python Arrays
Arrays are used to store multiple values in one single variable:

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

The class can be defined as a collection of objects.

It is a logical entity that has some specific attributes and


methods.

For example: if you have an employee class, then it should


contain an attribute and method, i.e. an email id, name, age,
salary, etc.
What is Math Module in Python?
Python has a built-in math module.

It is a standard module, so we do not need to install it


separately.

Must import it into the program we want to use.

Using import math to implement the functions to perform


mathematical operations.
Hence, a file operation can be done in the following
order.

•Open a file

•Read or write - Performing operation

•Close the file


Opening a file
Python provides an open() function that accepts two
arguments, file name and access mode in which the file is
accessed.

The function returns a file object which can be used to


perform various operations like reading, writing, etc.

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

It is a good practice to always close the file when you


are done with it.
“w” - It will overwrite the file if any file exists

demo
demo
application of python in linguistics

Artificial Intelligence and Machine Learning

Who uses Python today?

What companies use Python?

How is Python used in the real world?


1. Data Analytics
2. 2. Data Science/”AI”

3. application of python in E-commerce system


4. Introduction to data and Data Science
5. E-commerce applications of Data Science

You might also like