Professional Documents
Culture Documents
PYTHON INTERPRETER
Part-A
1. Define two modes of python?
Python was two basic modes: Normal and Interactive
Normal mode is the mode where the scripted and finished. Python files
are run in the python interpreter. This mode is also called as script
mode.
Interactive mode is a command line shell which gives immediate
feedback for each statement, while running previously fed statement in
active memory. A new lines are fed into the interpreter, the fed program
as evaluated both in part and in whole.
Execution Function
definition
Call Execution
flow
Return
Suspended
Execution
Execution
Operator Example
In X in Y
not in X not in Y
Part-B
1. Explain data type in python in details?
Data types:
i. Integer
ii. Float
iii. Boolean
iv. String
v. List
A data type is a category for values and every value belongs to exactly
one data type. All data values in python are encapsulated in relevant
object classes. The most common data types in python are listed in
below.
Integers = -2,-1,0,1,2,3,4,5
The value -2 and 30 for example are said to be integer value. The integer
data types indicate values that are whole number. Numbers with a
decimal point, such as 2.14 are called floating point value 44.0 would be
floating point numbers
Boolean:
Boolean is a data type named after George Boole (1815-1864). A
Boolean variable can take only two values True or False. The main of
this type is in logical expressions.
Example:
a= True
b= 30>45 #b gets the value false
Boolean expressions are after used in conjunction with the if statement
Boolean operators:
Example:
>>>3>4
False
>>>test= (3>4)
>>>test
False
>>>type (test)
<type ‘bool’>
Strings:
Characters:
Example:
Operator of strings:
The concatenated string with the “f” operation and create multiply
concatenated copies of a string with the “*”operator
And augmented assignment (+=and*=) also work
Example:
>>> ‘horse’ + ‘and’+ ‘dog’
‘Horse and dog’
>>> “H”*40
‘#############’
>>>
>>>s1= ‘flower’
>>>s1+ =‘s’
>>>s1
‘Flower’
Lists:
Modules:
Modules are quite easy to create. They are simply python files, like your
regular scripts. To create a module, write one or more functions in a text
file. Then save it with a.py extension. Let’s do that now with an example
open up a new file your text editor or IDE, and make a function. I’m
going to continue with the example of a shopping card from earlies and
make a function to calculate tax on the products
Importing modules:
To use modules, we can use either the import or from the keyword
import is the simplest and most common. So let’s try what first. You
then specify the name of the module, which is simply the file name,
without the .py extension. For example, with our finance module in the
same folder as our script we could write
Import finance
Built-in modules:
There are planty of built in module, just as there are with built in
functions. This is where python really excels. It takes what’s called the
batteries included approach.
Because python has such an extensive range of built-in modular, there’s
no way to cover them all in one lesson. Some are quit obscure, and
there’s no print teaching a load of information you might never have a
use for instead. Let’s cover several of the most useful ones
i. Random
ii. Math
iii. Os
iv. Data time
v. Urllib2
1) Random:
A good one to start with is random, because it’s easy to understand and
useful in most scripts you will write. As you expert this module lets you
generate random numbers. For example, when using python to create a
website. They can be used to making the password database more secure
or powering a random page feature. It we wanted a random integer, we
can use the randint function, like so
Import random
Print (random.randint(0,5))
2) Math
The math module provides access to mathematical constant and
functions
1. Import math
2.
3. Math .pi# pi, 3.14….
4. Math .e # Euler’s number, 2, 71….
5.
6. Math. degrees (2) # 2 radians = 114.59 degrees
7. Math. Radians (60) # 60 degrees= 1.04 radians
8.
9. Math.sin (2) # sin of 2 radians
10. Math.cos(0.5) # cosine of 0.5 radians
11. Math.tan (0.23) #tangent of 0.23 radians
12.
13. Math. factorial (5) # 1*2*3*4*5=120
14. Math. Sqrt (49) #square root of 49=1
There are lots more function in the modules, but these are some of the
most useful.
3) Data time:
If you need to work with dates and times, then the data time modules is
your friend. This one is a very useful one to know about for web
development.
Let’s import the module as useful
i. Import data time
4) Operating system (OS):
The next module we are looking at is OS, which the underlying
operating system that python is running on be that windows, Mac or
Linux well focus on the path sub module
These timestamps can be converted to useable string using the data time
module you can see how you can combine modules
1. From OS import path
2. Path.getsize ("user/giles/desktop/boot”)
#Return the size of a file in bytes-for this file
It was 314400 bytes, or 314kb
5) Urllib2:
To finish our tour of python’s standard library, we’re going to have a
brief look at Urllib2. This module lets you interface with the web, so it’s
obviously quite relevant to us. The most useful function it provides it
urlopen, which downloads a page
Import urllib2
Urllib2,urlopen("http://net.in/python.com”)
It can obviously swap the URL string for any site this will download the
HTML content of the page. This won’t return a string through so we
need to read data to get that out
Import urllip2
Urllip2,urlopen("http://net.infopython.com”).read(1
00)
Functions:
A function is a normal contains for a block of code, there are two types
of functions. The ones that user defined function and x library
function/PREDEFIND function
Example:
01. #our two costs to add up
02. Cost 1=15
03. Cost2=20
04.
05.
06. Def sum cart();
07. Total cost=cost 1+cost 2
08. Print total cost
09.
10. Sum cart()
Arguments:
Returning values:
Function Built-in:
There are some operators that are carried out often enough that python
has included them for use in any program. The way you call them is
exactly the same as calling your own, but you don’t have to define them
first
Str ():
First, let’s look at one of the most useful function in python. The string
conversion function.
1. Number =10
2. Print (' The number is ‘ +number)
Len ():
Another common task for strings is to be able to find the length of them
again. Python has a built in function for this. Let’s take the string ‘ Hello
word’ and try to find is length to need the built in len () function
1. String = “Hello word”
2. Print (len (string))
Int ():
Moving on often you are given a number like 10.6 or 3.396, which is not
an integer, but you need an integer from them the built in function to
convert to an integer is called int () function
1. Number =10.6
2. Print (int (number))
Range ():
Range accept an integer as the parameter and in this case, we’ll pass it
11. The function, then starts at zero, and makes a list counting upwards
until it less then the number you put in. so were if we put in 11 it will
store the numbers 0-10 in the list numbers
1. Number = range (11)
2. Print ( number )
Pulling together the code fragments from the previous section the whole
program looks like this
Def print_lyrics();
Print (“I’m a number jack, and I’m okay.”)
Print (“I sleep all night and I work all day.”)
Def repeat_lyrics ();
Print_lyrics ()
Print_lyrics ()
repeat_lyrics ()
Flow of execution:
5. Write program?
i. To circulate value of n variable
Program:
a=[1, 2, 3, 4]
a[1:]+a[:1]
a=[1, 2, 3, 4]
a[2:]+a[:2]
a=[1, 2, 3, 4]
a[3:]+a[:3]
def circulate (a, n);
return a [n:1]+a[1:n]
Output:
>>>leap (2012)
is a leap_year
>>>leap (2017)
Is a not leap year
ii. Find distance between two points
For example:
Y
A (9,7)
B (3,2)
Import math
Print (“enter value of x1”)
x1 = int (input ())
Print (“enter value of x2”)
X2 = int (input ())
Print (“enter value of y1”)
y1 = int (input ())
Print (“enter value of y2”)
y2 = int (input ())
dist = math.sqrt ((x2-x1)**2+(y2-y1)**2)
print (“The distance between two point is,”dist)
6. Precedence of operator?
In this python we are going to look at operation precedence in python.
We will be focusing on the operators we have already learned like **, *,
1, //, %, + and -. It is important to understand what operator will run first
Operator precedence:
The list below will show which operator has more procedure over the
operators. So the first operation in the list will run before the second
operator below
We can see in the above table more than one operator exits in the same
group
Associativity is the order in which an expression is evaluated that has
multiple operators of the same precedence. Almost all the operators
have left to right associativity
#left-right associativity
#output:3
Print (5*2//3)