Uploaded by Cotaxe

python unit 1

advertisement
What is an algorithm? Explain characteristics of an
algorithm.
An algorithm is a step-by-step procedure that defines a set of instructions that must be carried out in a
specific order to produce the desired result. Algorithms are generally developed independently of
underlying languages, which means that an algorithm can be implemented in more than
one programming language. Unambiguity, fineness, effectiveness, and language independence are
some of the characteristics of an algorithm. The scalability and performance of an algorithm are the
primary factors that contribute to its importance.
What is an Algorithm?

An algorithm is a set of commands that must be followed for a computer to perform
calculations or other problem-solving operations.

According to its formal definition, an algorithm is a finite set of instructions carried out in
a specific order to perform a particular task.

It is not the entire program or code; it is simple logic to a problem represented as an
informal description in the form of a flowchart or pseudocode.

Problem: A problem can be defined as a real-world problem or real-world instance
problem for which you need to develop a program or set of instructions. An algorithm is a
set of instructions.

Algorithm: An algorithm is defined as a step-by-step process that will be designed for a
problem.

Input: After designing an algorithm, the algorithm is given the necessary and desired
inputs.

Processing unit: The input will be passed to the processing unit, producing the desired
output.

Output: The outcome or result of the program is referred to as the output.
After defining what an algorithm is, you will now look at algorithm characteristics.
Characteristics of an Algorithm

Input: An algorithm requires some input values. An algorithm can be given a value other
than 0 as input.

Output: At the end of an algorithm, you will have one or more outcomes.

Unambiguity: A perfect algorithm is defined as unambiguous, which means that its
instructions should be clear and straightforward.

Finiteness: An algorithm must be finite. Finiteness in this context means that the algorithm
should have a limited number of instructions, i.e., the instructions should be countable.

Effectiveness: Because each instruction in an algorithm affects the overall process, it
should be adequate.

Language independence: An algorithm must be language-independent, which means that
its instructions can be implemented in any language and produce the same results.
Why Do You Need an Algorithm?
Scalability
It aids in your understanding of scalability. When you have a sizable real-world problem, you
must break it down into small steps to analyze it quickly.
Performance
The real world is challenging to break down into smaller steps. If a problem can be easily
divided into smaller steps, it indicates that the problem is feasible.
How to Write an Algorithm?

There are no well-defined standards for writing algorithms. It is, however, a problem that
is resource-dependent. Algorithms are never written with a specific programming
language in mind.

As you all know, basic code constructs such as loops like do, for, while, all programming
languages share flow control such as if-else, and so on. An algorithm can be written using
these common constructs.

Algorithms are typically written in a step-by-step fashion, but this is not always the case.
Algorithm writing is a process that occurs after the problem domain has been welldefined. That is, you must be aware of the problem domain for which you are developing
a solution.
What are the features and applications of Python?
Python is a dynamic, high-level, free open source, and interpreted
programming language. It supports object-oriented programming as well
as procedural-oriented programming. In Python, we don’t need to declare the
type of variable because it is a dynamically typed language. For example, x =
10 Here, x can be anything such as String, int, etc.
Features in Python
1. Free and Open Source
2. Easy to code: Python is a high-level programming language. Python is
very easy to learn the language as compared to other languages like C, C#,
Javascript, Java, etc. It is very easy to code in the Python language. It is also
a developer-friendly language.
3. Easy to Read: Python’s syntax is really straightforward. The code block
is defined by the indentations rather than by semicolons or brackets.
4. Object-Oriented Language: Python supports object-oriented language
and concepts of classes, object encapsulation, etc.
5. GUI Programming Support: Graphical User interfaces can be made
using a module such as PyQt5, PyQt4, wxPython, or Tk in python. PyQt5 is
the most popular option for creating graphical apps with Python.
6. High-Level Language: Python is a high-level language. When we write
programs in Python, we do not need to remember the system architecture, nor
do we need to manage the memory.
7. Extensible feature: Python is an Extensible language. We can write
some Python code into C or C++ language and also we can compile that code
in C/C++ language.
8. Easy to Debug: Excellent information for mistake tracing. You will be
able to quickly identify and correct the majority of your program’s issues
once you understand how to interpret Python’s error traces. Simply by
glancing at the code, you can determine what it is designed to perform.
9. Portable language: Python language is also a portable language. For
example, if we have Python code for windows and if we want to run this code
on other platforms such as Linux, Unix, and Mac then we do not need to
change it, we can run this code on any platform.
10. Integrated language: Python is also an Integrated language because
we can easily integrate Python with other languages like C, C++, etc.
11. Interpreted Language: Python is an Interpreted Language because
Python code is executed line by line at a time. like other languages C,
C++, Java, etc. there is no need to compile Python code this makes it easier
to debug our code. The source code of Python is converted into an
immediate form called bytecode.
12. Large Standard Library : Python has a large standard library that
provides a rich set of modules and functions so you do not have to write your
own code for every single thing.
13. Dynamically Typed Language: Python is a dynamically-typed
language. That means the type (for example- int, double, long, etc.) for a
variable is decided at run time not in advance because of this feature we don’t
need to specify the type of variable.
14. Frontend and backend development: With a new project py script,
you can run and write Python codes in HTML with the help of some simple
tags <py-script>, <py-env>, etc. This will help you do frontend development
work in Python like javascript. Backend is the strong forte of Python it’s
extensively used for this work cause of its frameworks like Django and Flask.
15. Allocating Memory Dynamically: In Python, the variable data type
does not need to be specified. The memory is automatically allocated to a
variable at runtime when it is given a value. Developers do not need to write
int y = 18 if the integer value 15 is set to y. You may just type y=18.
Explain about the input and output statements or
methods with example.
Input and Output Functions
A program needs to interact with the user to accomplish the desired task; this can
be achieved using Input-Output functions. The input() function helps to enter
data at run time by the user and the output function print() is used to display the
result of the program on the screen after execution.
1. The print() function
In Python, the print() function is used to display result on the screen. The syntax
for print() is as follows:
Example
print (“string to be displayed as output ” )
print (variable )
print (“String to be displayed as output ”, variable)
print (“String1 ”, variable, “String 2”, variable, “String 3” ……)
The print ( ) evaluates the expression before printing it on the monitor. The print
() displays an entire statement which is specified within print ( ). Comma ( , ) is
used as a separator in print ( ) to print more than one item.
2. input() function
In Python, input( ) function is used to accept data as input at run time. The
syntax for input() function is,
Variable = input (“prompt string”)
Where, prompt string in the syntax is a statement or message to the user, to
know what input can be given.
If a prompt string is used, it is displayed on the monitor; the user can provide
expected data from the input device. The input( ) takes whatever is typed from
the keyboard and stores the entered data in the given variable. If prompt string
is not given in input( ) no message is displayed on the screen, thus, the user will
not know what is to be typed as input.
Example 1:input( ) with prompt string
>>>city=input (“Enter Your City: ”)
Enter Your City:Madurai
>>>print (“I am from “, city)
I am from Madurai
Example 2:input( ) without prompt string
>>> city=input()
Rajarajan
>>> print (I am from", city)
I am from Rajarajan
Python Data Types
Variables can hold values, and every value has a data-type. Python is a dynamically
typed language; hence we do not need to define the type of the variable while
declaring it. The interpreter implicitly binds the value with its type.
1. a = 5
The variable a holds integer value five and we did not define its type. Python
interpreter will automatically interpret variables a as an integer type.
Python enables us to check the type of the variable used in the program. Python
provides us the type() function, which returns the type of the variable passed.
1. a=10
2. b="Hi Python"
3. c = 10.5
4. print(type(a))
5. print(type(b))
6. print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
Standard data types
A variable can hold different types of values. For example, a person's name must be
stored as a string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each
of them. The data types defined in Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numbers
Number stores numeric values. The integer, float, and complex values belong to a
Python Numbers data-type. Python provides the type() function to know the datatype of the variable. Similarly, the isinstance() function is used to check an object
belongs to a particular class.
Python creates Number objects when a number is assigned to a variable. For example;
1. a = 5
2. print("The type of a", type(a))
3.
4. b = 40.5
5. print("The type of b", type(b))
6.
7. c = 1+3j
8. print("The type of c", type(c))
9. print(" c is a complex number", isinstance(1+3j,complex))
Output:
The type of a <class
The type of b <class
The type of c <class
c is complex number:
'int'>
'float'>
'complex'>
True
Python supports three types of numeric data.
1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. Python
has no restriction on the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is
accurate upto 15 decimal points.
3. complex - A complex number contains an ordered pair, i.e., x + iy where x and y denote
the real and imaginary parts, respectively. The complex numbers like 2.14j, 2.0 + 2.3j,
etc.
Sequence Type
String
The string can be defined as the sequence of characters represented in the quotation
marks. In Python, we can use single, double, or triple quotes to define a string.
String handling in Python is a straightforward task since Python provides built-in
functions and operators to perform operations in the string.
In the case of string handling, the operator + is used to concatenate two strings as the
operation "hello"+" python" returns "hello python".
The operator * is known as a repetition operator as the operation "Python" *2 returns
'Python Python'.
The following example illustrates the string in Python.
Example - 1
1. str = "string using double quotes"
2. print(str)
3. s = '''''A multiline
4. string'''
5. print(s)
Output:
string using double quotes
A multiline
string
Consider the following example of string handling.
Example - 2
1. str1 = 'hello javatpoint' #string str1
2. str2 = ' how are you' #string str2
3. print (str1[0:2]) #printing first two character using slice operator
4. print (str1[4]) #printing 4th character of the string
5. print (str1*2) #printing the string twice
6. print (str1 + str2) #printing the concatenation of str1 and str2
Output:
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
List
Python Lists are similar to arrays in C. However, the list can contain data of different
types. The items stored in the list are separated with a comma (,) and enclosed within
square brackets [].
We can use slice [:] operators to access the data of the list. The concatenation operator
(+) and repetition operator (*) works with the list in the same way as they were working
with the strings.
Consider the following example.
1. list1 = [1, "hi", "Python", 2]
2. #Checking type of given list
3. print(type(list1))
4.
5. #Printing the list1
6. print (list1)
7.
8. # List slicing
9. print (list1[3:])
10.
11. # List slicing
12. print (list1[0:2])
13.
14. # List Concatenation using + operator
15. print (list1 + list1)
16.
17. # List repetation using * operator
18. print (list1 * 3)
Output:
[1,
[2]
[1,
[1,
[1,
'hi', 'Python', 2]
'hi']
'hi', 'Python', 2, 1, 'hi', 'Python', 2]
'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection
of the items of different data types. The items of the tuple are separated with a comma
(,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of the items
of a tuple.
Let's see a simple example of the tuple.
1. tup = ("hi", "Python", 2)
2. # Checking type of tup
3. print (type(tup))
4.
5. #Printing the tuple
6. print (tup)
7.
8. # Tuple slicing
9. print (tup[1:])
10. print (tup[0:1])
11.
12. # Tuple concatenation using + operator
13. print (tup + tup)
14.
15. # Tuple repatation using * operator
16. print (tup * 3)
17.
18. # Adding value to tup. It will throw an error.
19. t[2] = "hi"
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Traceback (most recent call last):
File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an associative array
or a hash table where each key stores a specific value. Key can hold any primitive data
type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the curly
braces {}.
Consider the following example.
1. d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
2.
3. # Printing dictionary
4. print (d)
5.
6. # Accesing value using keys
7. print("1st name is "+d[1])
8. print("2nd name is "+ d[4])
9.
10. print (d.keys())
11. print (d.values())
Output:
1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
Boolean
Boolean type provides two built-in values, True and False. These values are used to
determine the given statement true or false. It denotes by the class bool. True can be
represented by any non-zero value or 'T' whereas false can be represented by the 0 or
'F'. Consider the following example.
1. # Python program to check the boolean type
2. print(type(True))
3. print(type(False))
4. print(false)
Output:
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can
modify after creation), and has unique elements. In set, the order of the elements is
undefined; it may return the changed sequence of the element. The set is created by
using a built-in function set(), or a sequence of elements is passed in the curly braces
and separated by the comma. It can contain various types of values. Consider the
following example.
1. # Creating Empty set
2. set1 = set()
3.
4. set2 = {'James', 2, 3,'Python'}
5.
6. #Printing Set value
7. print(set2)
8.
9. # Adding element to the set
10.
11. set2.add(10)
12. print(set2)
13.
14. #Removing element from the set
15. set2.remove(2)
16. print(set2)
Output:
{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
What is Indentation? Explain with example
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
Indentation is a very important concept of Python because without properly
indenting the Python code, you will end up seeing IndentationError and the
code will not get compiled.
Python indentation refers to adding white space before a statement to a
particular block of code. In another word, all the statements with the same
space to the right, belong to the same code block.
Define Flowchart and explain symbols used in
flowchart with example.
Flowcharts graphically represent the flow of a program. There are four basic shapes
used in a flow chart. Each shape has a specific use:

oval: start / end

parallelogram: input / output

rectangle: calculations

diamond: selection structures
Arrows connect the basic shapes in a flowchart. The shapes and arrows of a flowchart
describe the flow of a program from start to end. Flowcharts typically flow from
the top to the bottom or flow from the left to the right. Below is the description of a
simple program:
The program starts. Then the program prints out "Output!". Finally, the program
ends.
The Python code that corresponds to this flowchart is:
# start
print("Output!")
# end
A description of a program that includes a calculation is below:
The program starts. Next, the program asks a user for a number. Two is added to the
number. Next, the resulting sum is printed. Finally, the program ends.
A flowchart that describes this program is is shown.
Enumerate the list and its methods with example
Python has a set of built-in methods that you can use on lists/arrays.
Elucidate the string and its methods with example
What is dictionary? Explain the methods available in
dictionary.
In Python, the dictionary refers to a collection (unordered) of various data
types. We use these for storing data values such as maps, and unlike other
data types capable of holding only one value in the form of an element, a
dictionary can hold the key: value pair. The key value in a dictionary is
present to make it comparatively more optimized.Let's see an example,
If we want to store information about countries and their capitals, we can
create a dictionary with country names as keys and capitals as values.
Difference Between List, Tuple, Set, and Dictionary in Python
Parameters
List
Tuple
Set
Dictionary
Basics
A list is basically
like a dynamically
sized array that
gets declared in
other languages
(Arraylist in the
case of Java,
vector in the case
of C++).
The tuples refer to
the collections of
various objects of
Python separated
by commas
between them.
The sets are an
unordered
collection of data
types. These are
mutable, iterable,
and do not
consist of any
duplicate
elements.
In Python, the
dictionary refers
to a collection
(unordered) of
various data
types. We use
these for storing
data values such
as maps, and
unlike other data
types capable of
holding only one
value in the form
of an element, a
dictionary can
hold the key:
value pair.
Homogeneity
A list refers to a
data structure of
a nonhomogenous
type that
functions to store
various elements
in columns,
multiple rows,
and single rows.
A tuple also refers
to a data structure
of the nonhomogenous type
that functions to
store various
elements in
columns, multiple
rows, and single
rows.
A set also refers
to a data
structure of the
non-homogenous
type, but it stores
various elements
in a single row.
A dictionary also
refers to a data
structure of the
non-homogenous
type that
functions to store
key-value pairs.
Representation
We can represent
a List by [ ]
We can represent a
Tuple by ( )
We can represent
a Set by { }
We can represent
a Dictionary by { }
Duplicate
elements
It allows various
duplicate
elements.
It allows various
duplicate elements.
It does not allow
any duplicate
elements.
The keys are not
at all duplicated.
Nested Among
All
It can be utilized
in a List.
It can be utilized in
a Tuple.
It can be utilized
in a Set.
It can be utilized
in a Dictionary.
Example
[6, 7, 8, 9, 10]
(6, 7, 8, 9, 10)
{6, 7, 8, 9, 10}
{6, 7, 8, 9, 10}
Function for
Creation
We can create a
list using
the list() function.
We can create a
tuple using
the tuple() function.
We can create a
set using
the set() function.
We can create a
dictionary using
the dict() function.
Mutation
It is mutable. It
means that a user
can make any
changes to a list.
It is immutable. It
means that a user
can’t make any
changes to a tuple.
It is mutable. It
means that a user
can make any
changes to a set.
It is mutable, but
the keys are not at
all duplicated.
Order
It is ordered in
nature.
It is ordered in
nature.
It is unordered in
nature.
It is ordered in
nature.
Empty
Elements
If we want to
create an empty
list, we use:
If we want to create
an empty tuple, we
use:
If we want to
create an empty
set, we use:
l=[]
t=()
a=set()
If we want to
create an empty
dictionary, we
use:
b=set(a)
d={}
:
Define Variable and mention rules for choosing names
of Variable.
Python Variable is containers which store values. Python is not “statically
typed”. We do not need to declare variables before using them or declare
their type. A variable is created the moment we first assign a value to it. A
Python variable is a name given to a memory location. It is the basic unit of
storage in a program.
Rules for Python variables:




A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three
different variables)
Download