Python Answers
[Link]
interpreter/
Compiler interpreter
Steps of Programming: Steps of Programming:
Program Creation. Program Creation.
Analysis of language by the
Linking of files or generation
compiler and throws errors in
of Machine Code is not
case of any incorrect
required by Interpreter.
statement.
In case of no error, the Execution of source
Compiler converts the source statements one by one.
code to Machine Code.
Linking of various code files
into a runnable program.
Finally runs a Program.
Compiled codes run faster Interpreted codes run slower
Linking-Loading Model is the basic The Interpretation Model is the
working model of the Compiler. basic working model of the
Interpreter.
The compiler generates an output The interpreter does not generate
in the form of (.exe). any output.
Errors are displayed in Compiler Errors are displayed in every single
after Compiling together at the line.
current time.
It does not require source code for It requires source code for later
later execution. execution.
Execution of the program takes Execution of the program happens
place only after the whole program after every line is checked or
is compiled. evaluated.
Compilers more often take a large In comparison,Interpreters take less
amount of time for analysing the time for analysing the source code.
source code.
CPU utilization is more in the case CPU utilization is less in the case of
of a Compiler. a Interpreter.
The use of Compilers mostly The use of Interpreters is mostly in
happens in Production Programming and Development
Environment. Environments.
C, C++, C#, etc are programming Python, Ruby, MATLAB, etc are
languages that are compiler- programming languages that are
based. interpreter-based.
Python functions
[Link]
Python Functions is a block of statements that return the specific task.
The idea is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again
for different inputs, we can do the function calls to reuse code contained
in it over and over again.
Some Benefits of Using Functions
Increase Code Readability
Increase Code Reusability
Python Function Declaration
The syntax to declare a function is:
Syntax of Python Function Declaration
Types of Functions in Python
Below are the different types of functions in Python:
Built-in library function: These are Standard functions in Python
that are available to use.
User-defined function: We can create our own functions based on
our requirements.
LIST
Lists are just like the arrays, declared in other languages. Lists need
not be homogeneous always which makes it the most powerful
tool in Python. A single list may contain Datatypes like Integers,
Strings, as well as Objects.
Lists are mutable, and hence, they can be altered even after their
creation. List in Python are ordered and have a definite count.
The elements in a list are indexed according to a definite
sequence and the indexing of a list is done with 0 being the first
index.
Each element in the list has its definite place in the list, which
allows duplicating of elements in the list, with each element
having its own distinct place and credibility
Python list() Function Syntax
Syntax: list(iterable)
Parameter:
iterable: an object that could be a sequence (string, tuples) or
collection (set, dictionary) or any iterator object.
list() Function in Python
We can create a Python list by using list() function. Below are the
ways by which we can use list() function in Python:
To create a list from a string
To create a list from a tuple
To create a list from set and dictionary
Taking user input as a list
Python offers the following list functions:
[Link]
sort(): Sorts the list in ascending order.
type(list): It returns the class type of an object.
append(): Adds a single element to a list.
extend(): Adds multiple elements to a list.
index(): Returns the first appearance of the specified value.
max(list): It returns an item from the list with max value.
min(list): It returns an item from the list with min value.
len(list): It gives the total length of the list.
list(seq): Converts a tuple into a list.
cmp(list1, list2): It compares elements of both lists list1 and list2.
filter(fun,list): filter the list using the Python function.